Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add BulkWriter integration tests, java backport changes, delete fix #1117

Merged
merged 8 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
25 changes: 23 additions & 2 deletions dev/src/write-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,33 @@ export class WriteBatch implements firestore.WriteBatch {
api.BatchWriteResponse
>('batchWrite', request, tag);

return (response.writeResults || []).map((result, i) => {
const writeResults = response.writeResults || [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind removing GCF_IDLE_TIMEOUT_MS (the unrelated lint complaint above)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's being used further down in _shouldCreateTransaction().

let latestTimestamp = Timestamp.fromMillis(0);
writeResults.forEach(result => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Prefer using for ... of loops when possible, as this makes for far easier debugging than inline lambdas.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n/a anymore

if (
result.updateTime &&
Timestamp.fromProto(result.updateTime) > latestTimestamp
) {
latestTimestamp = Timestamp.fromProto(result.updateTime);
}
});

return writeResults.map((result, i) => {
const status = response.status[i];
const error = new GoogleError(status.message || undefined);
error.code = status.code as Status;

// Since delete operations currently do not have write times, use the
// latest update time for deletes, or Timestamp(0,0) if the entire batch
// consists of deletes.
const isSuccessfulDelete =
schmidt-sebastian marked this conversation as resolved.
Show resolved Hide resolved
result.updateTime === null && error.code === Status.OK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result.updateTime === null && error.code === Status.OK;
const DELETE_TIMESTAMP_SENTINEL : api.ITimestamp = {}
const updateTime = error.code === Status.OK
? Timestamp.fromProto(result.updateTime || DELETE_TIMESTAMP_SENTINEL)
: null;

For brevity.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so crisp, so clean.

return new BatchWriteResult(
result.updateTime ? Timestamp.fromProto(result.updateTime) : null,
isSuccessfulDelete
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what is going on here :) Can you rewrite this to make it obvious? At the very least, this needs parentheses.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this style from your Transaction PR b/c I thought it seemed really cool! And then I went back and saw you changed it :(

? latestTimestamp
: result.updateTime
? Timestamp.fromProto(result.updateTime)
: null,
error
);
});
Expand Down
77 changes: 73 additions & 4 deletions dev/system-test/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
verifyInstance,
} from '../test/util/helpers';
import IBundleElement = firestore.IBundleElement;
import {BulkWriter} from '../src/bulk-writer';

use(chaiAsPromised);

Expand Down Expand Up @@ -2329,23 +2330,91 @@ describe('QuerySnapshot class', () => {
describe('BulkWriter class', () => {
let firestore: Firestore;
let randomCol: CollectionReference;
let writer: BulkWriter;

beforeEach(() => {
firestore = new Firestore({});
writer = firestore._bulkWriter();
randomCol = getTestRoot(firestore);
});

afterEach(() => verifyInstance(firestore));

// TODO(BulkWriter): Enable this test once protos are public.
it.skip('can terminate once BulkWriter is closed', async () => {
it('has create() method', async () => {
const ref = randomCol.doc('doc1');
const promise = writer.create(ref, {foo: 'bar'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/promise/singleOp/ ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

await writer.close();
const result = await ref.get();
expect(result.data()).to.deep.equal({foo: 'bar'});
const writeTime = (await promise).writeTime;
expect(writeTime).to.not.be.null;
});

it('has set() method', async () => {
const ref = randomCol.doc('doc1');
const promise = writer.set(ref, {foo: 'bar'});
await writer.close();
const result = await ref.get();
expect(result.data()).to.deep.equal({foo: 'bar'});
const writeTime = (await promise).writeTime;
expect(writeTime).to.not.be.null;
});

it('has update() method', async () => {
const ref = randomCol.doc('doc1');
await ref.set({foo: 'bar'});
const promise = writer.update(ref, {foo: 'bar2'});
await writer.close();
const result = await ref.get();
expect(result.data()).to.deep.equal({foo: 'bar2'});
const writeTime = (await promise).writeTime;
expect(writeTime).to.not.be.null;
});

it('has delete() method', async () => {
const ref = randomCol.doc('doc1');
await ref.set({foo: 'bar'});
const promise = writer.delete(ref);
await writer.close();
const result = await ref.get();
expect(result.exists).to.be.false;
// TODO(b/158502664): Remove this check once we can get write times.
const deleteResult = await promise;
expect(deleteResult.writeTime).to.deep.equal(new Timestamp(0, 0));
});

// TODO(b/158502664): Remove this test once we can get write times.
it('delete uses the latest update time of other operations', async () => {
const ref = randomCol.doc('doc1');
const ref2 = randomCol.doc('doc2');
await ref.set({foo: 'bar'});
await ref2.set({foo: 'bar'});
// Update a different doc so that the writes are sent in the same batch.
const updatePromise = writer.update(randomCol.doc('doc2'), {foo: 'bar1'});
const deletePromise = writer.delete(ref);
await writer.close();
const deleteResult = await deletePromise;
const updateResult = await updatePromise;
expect(deleteResult.writeTime).to.deep.equal(updateResult.writeTime);
});

it('can terminate once BulkWriter is closed', async () => {
const ref = randomCol.doc('doc1');
const writer = firestore._bulkWriter();
writer.set(ref, {foo: 'bar'});
writer.set(ref, {foo: 'bar2'});
await writer.close();
return firestore.terminate();
});

it('writes to the same document in order', async () => {
const ref = randomCol.doc('doc1');
await ref.set({foo: 'bar0'});
writer.set(ref, {foo: 'bar1'});
writer.set(ref, {foo: 'bar2'});
writer.set(ref, {foo: 'bar3'});
await writer.flush();
const res = await ref.get();
expect(res.data()).to.deep.equal({foo: 'bar3'});
});
});

describe('Client initialization', () => {
Expand Down
18 changes: 9 additions & 9 deletions dev/test/bulk-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ describe('BulkWriter', () => {
};
}

function successResponse(seconds: number): api.IBatchWriteResponse {
function successResponse(updateTimeSeconds: number): api.IBatchWriteResponse {
return {
writeResults: [
{
updateTime: {
nanos: 0,
seconds,
seconds: updateTimeSeconds,
},
},
],
status: [{code: Status.OK}],
};
}

function failResponse(): api.IBatchWriteResponse {
function failedResponse(): api.IBatchWriteResponse {
return {
writeResults: [
{
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('BulkWriter', () => {
const bulkWriter = await instantiateInstance([
{
request: createRequest([setOp('doc', 'bar')]),
response: failResponse(),
response: failedResponse(),
},
]);

Expand Down Expand Up @@ -332,11 +332,11 @@ describe('BulkWriter', () => {
const bulkWriter = await instantiateInstance([
{
request: createRequest([setOp('doc', 'bar')]),
response: successResponse(0),
response: successResponse(1),
},
{
request: createRequest([updateOp('doc', 'bar1')]),
response: successResponse(1),
response: successResponse(2),
},
]);

Expand All @@ -355,7 +355,7 @@ describe('BulkWriter', () => {
const bulkWriter = await instantiateInstance([
{
request: createRequest([setOp('doc1', 'bar'), updateOp('doc2', 'bar')]),
response: mergeResponses([successResponse(0), successResponse(1)]),
response: mergeResponses([successResponse(1), successResponse(2)]),
},
]);

Expand Down Expand Up @@ -404,7 +404,7 @@ describe('BulkWriter', () => {
const bulkWriter = await instantiateInstance([
{
request: createRequest([setOp('doc', 'bar')]),
response: successResponse(0),
response: successResponse(1),
},
{
request: createRequest([
Expand Down Expand Up @@ -447,9 +447,9 @@ describe('BulkWriter', () => {
createOp('doc3', 'bar'),
]),
response: mergeResponses([
successResponse(0),
successResponse(1),
successResponse(2),
successResponse(3),
]),
},
{
Expand Down