-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: Errors from gax layer #1090
Merged
danieljbruce
merged 23 commits into
googleapis:main
from
danieljbruce:errors-from-gax-layer
Jul 4, 2022
Merged
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e368301
Getting grpc set up for tests
danieljbruce eb94439
test for error sent through gax
danieljbruce 95a281c
Trying other things
danieljbruce cdf8149
Group everything into describe blocks.
danieljbruce 091b08c
Add Google header
danieljbruce 7d5fa9c
Added more tests
danieljbruce 1698902
Create mock service files
danieljbruce 47eb8d8
refactor a check
danieljbruce b9d82f8
mock server tests
danieljbruce 3256cdc
undo tests
danieljbruce b25068a
Pass metadata through
danieljbruce 4bdd4bb
Merge branch 'main' of https://github.com/googleapis/nodejs-bigtable …
danieljbruce 6ef1bd4
build fix
danieljbruce 8ae117a
work in progress
danieljbruce e8cd776
Add service error check and change test
danieljbruce 148bd42
Remove TODO and add done hook.
danieljbruce d3d9fb6
Logs for debugging
danieljbruce 7153399
use the tcp-port-used library instead
danieljbruce 8db378b
Merge branch 'main' of https://github.com/googleapis/nodejs-bigtable …
danieljbruce 3247820
use await
danieljbruce 1bb0c10
Merge branch 'main' into errors-from-gax-layer
danieljbruce 22314fe
Merge branch 'main' into errors-from-gax-layer
danieljbruce 6ae439e
Merge branch 'main' into errors-from-gax-layer
bcoe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ** This file is automatically generated by gapic-generator-typescript. ** | ||
// ** https://github.com/googleapis/gapic-generator-typescript ** | ||
// ** All changes to this file may be overwritten. ** | ||
|
||
import {grpc} from 'google-gax'; | ||
|
||
const DEFAULT_PORT = 1234; | ||
|
||
export class MockServer { | ||
port: string; | ||
services: Set<grpc.ServiceDefinition> = new Set(); | ||
server: grpc.Server; | ||
|
||
constructor( | ||
callback?: (port: string) => void, | ||
port?: string | number | undefined | ||
) { | ||
const portString = Number(port ? port : DEFAULT_PORT).toString(); | ||
this.port = portString; | ||
const server = new grpc.Server(); | ||
this.server = server; | ||
server.bindAsync( | ||
`localhost:${this.port}`, | ||
grpc.ServerCredentials.createInsecure(), | ||
() => { | ||
server.start(); | ||
callback ? callback(portString) : undefined; | ||
} | ||
); | ||
} | ||
|
||
setService( | ||
service: grpc.ServiceDefinition, | ||
implementation: grpc.UntypedServiceImplementation | ||
) { | ||
if (this.services.has(service)) { | ||
this.server.removeService(service); | ||
} else { | ||
this.services.add(service); | ||
} | ||
this.server.addService(service, implementation); | ||
} | ||
|
||
shutdown(callback: (err?: Error) => void) { | ||
this.server.tryShutdown((err?: Error) => { | ||
callback(err); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ** This file is automatically generated by gapic-generator-typescript. ** | ||
// ** https://github.com/googleapis/gapic-generator-typescript ** | ||
// ** All changes to this file may be overwritten. ** | ||
|
||
import {grpc} from 'google-gax'; | ||
|
||
import {MockServer} from './mock-server'; | ||
|
||
export abstract class MockService { | ||
abstract service: grpc.ServiceDefinition; | ||
server: MockServer; | ||
|
||
constructor(server: MockServer) { | ||
this.server = server; | ||
} | ||
|
||
setService(implementation: grpc.UntypedServiceImplementation) { | ||
this.server.setService(this.service, implementation); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/util/mock-servers/service-implementations/bigtable-client-mock-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ** This file is automatically generated by gapic-generator-typescript. ** | ||
// ** https://github.com/googleapis/gapic-generator-typescript ** | ||
// ** All changes to this file may be overwritten. ** | ||
|
||
import grpc = require('@grpc/grpc-js'); | ||
import jsonProtos = require('../../../../protos/protos.json'); | ||
import protoLoader = require('@grpc/proto-loader'); | ||
import {MockService} from '../mock-service'; | ||
|
||
const packageDefinition = protoLoader.fromJSON(jsonProtos, { | ||
keepCase: true, | ||
longs: String, | ||
enums: String, | ||
defaults: true, | ||
oneofs: true, | ||
}); | ||
const proto = grpc.loadPackageDefinition(packageDefinition); | ||
|
||
export class BigtableClientMockService extends MockService { | ||
service = | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
proto.google.bigtable.v2.Bigtable.service; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ** This file is automatically generated by gapic-generator-typescript. ** | ||
// ** https://github.com/googleapis/gapic-generator-typescript ** | ||
// ** All changes to this file may be overwritten. ** | ||
|
||
import {before, describe, it} from 'mocha'; | ||
import {Bigtable} from '../src'; | ||
import * as assert from 'assert'; | ||
|
||
import {GoogleError} from 'google-gax'; | ||
import {MockServer} from '../src/util/mock-servers/mock-server'; | ||
import {BigtableClientMockService} from '../src/util/mock-servers/service-implementations/bigtable-client-mock-service'; | ||
import {MockService} from '../src/util/mock-servers/mock-service'; | ||
|
||
describe('Bigtable/Errors', () => { | ||
let server: MockServer; | ||
let bigtable: Bigtable; | ||
let table: any; | ||
|
||
before(done => { | ||
server = new MockServer(() => { | ||
bigtable = new Bigtable({ | ||
apiEndpoint: `localhost:${server.port}`, | ||
}); | ||
table = bigtable.instance('fake-instance').table('fake-table'); | ||
done(); | ||
}); | ||
}); | ||
|
||
describe('with the bigtable data client', () => { | ||
let service: MockService; | ||
before(async () => { | ||
service = new BigtableClientMockService(server); | ||
}); | ||
|
||
describe('sends errors through a streaming request', () => { | ||
const errorDetails = | ||
'Table not found: projects/my-project/instances/my-instance/tables/my-table'; | ||
const emitTableNotExistsError = (stream: any) => { | ||
// TODO: Replace stream with type | ||
stream.emit('error', { | ||
code: 5, | ||
details: errorDetails, | ||
}); | ||
}; | ||
function checkTableNotExistError(err: GoogleError) { | ||
const {code, statusDetails, message} = err; | ||
assert.strictEqual(statusDetails, errorDetails); | ||
assert.strictEqual(code, 5); | ||
assert.strictEqual( | ||
message, | ||
'5 NOT_FOUND: Table not found: projects/my-project/instances/my-instance/tables/my-table' | ||
); | ||
} | ||
describe('with ReadRows service', () => { | ||
before(async () => { | ||
service.setService({ | ||
ReadRows: emitTableNotExistsError, | ||
}); | ||
}); | ||
it('should produce human readable error when passing through gax', done => { | ||
const readStream = table.createReadStream({}); | ||
readStream.on('error', (err: GoogleError) => { | ||
checkTableNotExistError(err); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('with mutateRows service through insert', () => { | ||
before(async () => { | ||
service.setService({ | ||
mutateRows: emitTableNotExistsError, | ||
}); | ||
}); | ||
it('should produce human readable error when passing through gax', async () => { | ||
const timestamp = new Date(); | ||
const rowsToInsert = [ | ||
{ | ||
key: 'r2', | ||
data: { | ||
cf1: { | ||
c1: { | ||
value: 'test-value2', | ||
labels: [], | ||
timestamp, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
try { | ||
await table.insert(rowsToInsert); | ||
assert.fail(); | ||
} catch (err) { | ||
checkTableNotExistError(err); | ||
} | ||
}); | ||
}); | ||
describe('with sampleRowKeys', () => { | ||
before(async () => { | ||
service.setService({ | ||
sampleRowKeys: emitTableNotExistsError, | ||
}); | ||
}); | ||
it('should produce human readable error when passing through gax', async () => { | ||
try { | ||
await table.sampleRowKeys({}); | ||
assert.fail(); | ||
} catch (err) { | ||
checkTableNotExistError(err); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
after(async () => { | ||
server.shutdown(() => {}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// ** This file is automatically generated by gapic-generator-typescript. ** | ||
// ** https://github.com/googleapis/gapic-generator-typescript ** | ||
// ** All changes to this file may be overwritten. ** | ||
|
||
import {describe, it} from 'mocha'; | ||
import {MockServer} from '../../src/util/mock-servers/mock-server'; | ||
import * as net from 'net'; | ||
import * as assert from 'assert'; | ||
|
||
interface ServerError { | ||
code: string; | ||
} | ||
|
||
// TODO: Test server shuts down | ||
|
||
describe('Bigtable/Mock-Server', () => { | ||
const inputPort = '1234'; | ||
let server: MockServer; | ||
function checkPort(port: string, inUse: boolean, callback: () => void) { | ||
const netServer = net.createServer(); | ||
netServer.once('error', (err: ServerError) => { | ||
if (inUse) { | ||
assert.strictEqual(err.code, 'EADDRINUSE'); | ||
} else { | ||
assert.notEqual(err.code, 'EADDRINUSE'); | ||
} | ||
netServer.close(); | ||
callback(); | ||
}); | ||
netServer.once('listening', () => { | ||
// close the server if listening doesn't fail | ||
netServer.close(); | ||
}); | ||
// netServer.listen(port); | ||
netServer.listen(`localhost:${port}`); | ||
} | ||
describe('Ensure server shuts down properly when destroyed', () => { | ||
it('should start a mock server', done => { | ||
server = new MockServer(port => { | ||
checkPort(port, true, done); | ||
}, inputPort); | ||
}); | ||
}); | ||
after(done => { | ||
checkPort(server.port, true, () => { | ||
server.shutdown((err?: Error) => { | ||
assert.deepStrictEqual(err, undefined); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I add a before hook with the following in it:
checkPort(inputPort, false, done);
the
assert.notEqual(err.code, 'EADDRINUSE');
fails, but I am not sure why at this time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the way this worked entirely to use a third party dev dependency.