-
Notifications
You must be signed in to change notification settings - Fork 149
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
Changes from 4 commits
fd92ddf
b792f43
89910a0
5193146
eace950
9188e4e
d149ee0
f46b6c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 || []; | ||||||||||||||
let latestTimestamp = Timestamp.fromMillis(0); | ||||||||||||||
writeResults.forEach(result => { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Prefer using There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
For brevity. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||||||
); | ||||||||||||||
}); | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ import { | |
verifyInstance, | ||
} from '../test/util/helpers'; | ||
import IBundleElement = firestore.IBundleElement; | ||
import {BulkWriter} from '../src/bulk-writer'; | ||
|
||
use(chaiAsPromised); | ||
|
||
|
@@ -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'}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/promise/singleOp/ ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
|
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.
Do you mind removing
GCF_IDLE_TIMEOUT_MS
(the unrelated lint complaint above)?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.
it's being used further down in
_shouldCreateTransaction()
.