Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Jan 2, 2020
1 parent e5737b8 commit aed7387
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 80 deletions.
2 changes: 1 addition & 1 deletion dev/src/index.ts
Expand Up @@ -259,7 +259,7 @@ export class Firestore {
private _settings: Settings = {};

/**
* Settings for the exponential backoff used by the Streaming endpoints.
* Settings for the exponential backoff used by the streaming endpoints.
* @private
*/
private _backoffSettings: ExponentialBackoffSetting;
Expand Down
4 changes: 2 additions & 2 deletions dev/src/util.ts
Expand Up @@ -108,8 +108,8 @@ export function isPermanentRpcError(
): boolean {
if (err.code !== undefined) {
const serviceConfigName = methodName[0].toUpperCase() + methodName.slice(1);
const retryCodeNames = config.methods[serviceConfigName]!.retry_codes_name;
const retryCodes = config.retry_codes[retryCodeNames].map(
const retryCodeNames = config.methods[serviceConfigName]!.retry_codes_name!;
const retryCodes = config.retry_codes![retryCodeNames].map(
errorName => Status[errorName as keyof typeof Status]
);
return retryCodes.indexOf(err.code) === -1;
Expand Down
2 changes: 1 addition & 1 deletion dev/src/watch.ts
Expand Up @@ -453,7 +453,7 @@ abstract class Watch {
this.closeStream(Error('Unexpected target ID sent by server'));
}
} else if (change.targetChangeType === 'REMOVE') {
let code = 13;
let code = Status.INTERNAL;
let message = 'internal error';
if (change.cause) {
code = change.cause.code!;
Expand Down
4 changes: 2 additions & 2 deletions dev/test/document.ts
Expand Up @@ -44,7 +44,7 @@ import {
writeResult,
} from './util/helpers';

import {GoogleError} from 'google-gax';
import {GoogleError, Status} from 'google-gax';

const PROJECT_ID = 'test-project';

Expand Down Expand Up @@ -651,7 +651,7 @@ describe('get document', () => {
const overrides: ApiOverride = {
batchGetDocuments: () => {
const error = new GoogleError('RPC Error');
error.code = 7; // PERMISSION_DENIED
error.code = Status.PERMISSION_DENIED;
return stream(error);
},
};
Expand Down
4 changes: 2 additions & 2 deletions dev/test/transaction.ts
Expand Up @@ -15,7 +15,7 @@
import {expect, use} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as extend from 'extend';
import {GoogleError} from 'google-gax';
import {GoogleError, Status} from 'google-gax';
import * as through2 from 'through2';

import * as proto from '../protos/firestore_v1_proto_api';
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('failed transactions', () => {

it('limits the retry attempts', () => {
const err = new GoogleError('Server disconnect');
err.code = 14; // Unavailable
err.code = Status.UNAVAILABLE;

return expect(
runTransaction(
Expand Down
132 changes: 60 additions & 72 deletions dev/test/watch.ts
Expand Up @@ -16,34 +16,31 @@ const duplexify = require('duplexify');

import {expect} from 'chai';
import * as extend from 'extend';
import {GoogleError} from 'google-gax';
import {GoogleError, Status} from 'google-gax';
import {Transform} from 'stream';
import * as through2 from 'through2';

import {google} from '../protos/firestore_v1_proto_api';

import {
CollectionReference,
FieldPath,
Firestore,
GeoPoint,
setLogFunction,
Timestamp,
} from '../src';
import {
DocumentData,
DocumentReference,
DocumentSnapshot,
FieldPath,
Firestore,
GeoPoint,
Query,
QueryDocumentSnapshot,
QuerySnapshot,
setLogFunction,
Timestamp,
} from '../src';
import {MAX_RETRY_ATTEMPTS, setTimeoutHandler} from '../src/backoff';
import {DocumentSnapshotBuilder} from '../src/document';
import {DocumentChangeType} from '../src/document-change';
import {Serializer} from '../src/serializer';
import {createInstance, InvalidApiUsage, verifyInstance} from './util/helpers';

import api = google.firestore.v1;

// Change the argument to 'console.log' to enable debug output.
Expand Down Expand Up @@ -326,7 +323,7 @@ class StreamHelper {
destroyStream(err?: GoogleError): void {
if (!err) {
err = new GoogleError('Server disconnect');
err.code = 14; // Unavailable
err.code = Status.UNAVAILABLE;
}
this.readStream!.destroy(err);
}
Expand Down Expand Up @@ -821,7 +818,7 @@ describe('Query watch', () => {

it('stops attempts after maximum retry attempts', () => {
const err = new GoogleError('GRPC Error');
err.code = Number(10 /* ABORTED */);
err.code = Status.ABORTED;
return watchHelper.runFailedTest(
collQueryJSON(),
async () => {
Expand Down Expand Up @@ -863,71 +860,62 @@ describe('Query watch', () => {
});
});

it('retries based on error code', () => {
const expectRetry: {[k: number]: boolean} = {
/* Cancelled */ 1: true,
/* Unknown */ 2: true,
/* InvalidArgument */ 3: false,
/* DeadlineExceeded */ 4: true,
/* NotFound */ 5: false,
/* AlreadyExists */ 6: false,
/* PermissionDenied */ 7: false,
/* ResourceExhausted */ 8: true,
/* FailedPrecondition */ 9: false,
/* Aborted */ 10: true,
/* OutOfRange */ 11: false,
/* Unimplemented */ 12: false,
/* Internal */ 13: true,
/* Unavailable */ 14: true,
/* DataLoss */ 15: false,
/* Unauthenticated */ 16: true,
};

let result = Promise.resolve();

for (const statusCode in expectRetry) {
if (expectRetry.hasOwnProperty(statusCode)) {
result = result.then(() => {
const err = new GoogleError('GRPC Error');
err.code = Number(statusCode);

if (expectRetry[statusCode]) {
return watchHelper.runTest(collQueryJSON(), () => {
watchHelper.sendAddTarget();
watchHelper.sendCurrent();
watchHelper.sendSnapshot(1, Buffer.from([0xabcd]));
return watchHelper.await('snapshot').then(() => {
it('retries based on error code', async () => {
const testCases = new Map<Status, boolean>();
testCases.set(Status.CANCELLED, true);
testCases.set(Status.UNKNOWN, true);
testCases.set(Status.INVALID_ARGUMENT, false);
testCases.set(Status.DEADLINE_EXCEEDED, true);
testCases.set(Status.NOT_FOUND, false);
testCases.set(Status.ALREADY_EXISTS, false);
testCases.set(Status.PERMISSION_DENIED, false);
testCases.set(Status.RESOURCE_EXHAUSTED, true);
testCases.set(Status.FAILED_PRECONDITION, false);
testCases.set(Status.ABORTED, true);
testCases.set(Status.OUT_OF_RANGE, false);
testCases.set(Status.UNIMPLEMENTED, false);
testCases.set(Status.INTERNAL, true);
testCases.set(Status.UNAVAILABLE, true);
testCases.set(Status.DATA_LOSS, false);
testCases.set(Status.UNAUTHENTICATED, true);

for (const [statusCode, expectRetry] of testCases) {
const err = new GoogleError('GRPC Error');
err.code = statusCode;

if (expectRetry) {
await watchHelper.runTest(collQueryJSON(), () => {
watchHelper.sendAddTarget();
watchHelper.sendCurrent();
watchHelper.sendSnapshot(1, Buffer.from([0xabcd]));
return watchHelper.await('snapshot').then(() => {
streamHelper.destroyStream(err);
return streamHelper.awaitReopen();
});
});
} else {
await watchHelper.runFailedTest(
collQueryJSON(),
() => {
watchHelper.sendAddTarget();
watchHelper.sendCurrent();
watchHelper.sendSnapshot(1, Buffer.from([0xabcd]));
return watchHelper
.await('snapshot')
.then(() => {
streamHelper.destroyStream(err);
return streamHelper.awaitReopen();
})
.then(() => {
return streamHelper.await('error');
})
.then(() => {
return streamHelper.await('close');
});
});
} else {
return watchHelper.runFailedTest(
collQueryJSON(),
() => {
watchHelper.sendAddTarget();
watchHelper.sendCurrent();
watchHelper.sendSnapshot(1, Buffer.from([0xabcd]));
return watchHelper
.await('snapshot')
.then(() => {
streamHelper.destroyStream(err);
})
.then(() => {
return streamHelper.await('error');
})
.then(() => {
return streamHelper.await('close');
});
},
'GRPC Error'
);
}
});
},
'GRPC Error'
);
}
}

return result;
}).timeout(5000);

it('retries with unknown code', () => {
Expand Down

0 comments on commit aed7387

Please sign in to comment.