Skip to content
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
merged 23 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/util/mock-servers/mock-server.ts
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);
});
}
}
34 changes: 34 additions & 0 deletions src/util/mock-servers/mock-service.ts
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);
}
}
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;
}
132 changes: 132 additions & 0 deletions test/errors.ts
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(() => {});
});
});
65 changes: 65 additions & 0 deletions test/util/mock-server.ts
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', () => {
Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

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);
});
});
});
});