Skip to content

Commit eda4c31

Browse files
author
Brian Chen
committed
Wait for writeStream to open
1 parent b739763 commit eda4c31

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

packages/firestore/test/unit/specs/spec_test_runner.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ class MockConnection implements Connection {
174174
/** A Deferred that is resolved once watch opens. */
175175
watchOpen = new Deferred<void>();
176176

177+
/** A Deferred that is resolved once the write stream opens. */
178+
writeOpen = new Deferred<void>();
179+
177180
invokeRPC<Req>(rpcName: string, request: Req): never {
178181
throw new Error('Not implemented!');
179182
}
@@ -196,6 +199,10 @@ class MockConnection implements Connection {
196199
return this.watchOpen.promise;
197200
}
198201

202+
waitForWriteOpen(): Promise<void> {
203+
return this.writeOpen.promise;
204+
}
205+
199206
ackWrite(commitTime?: string, mutationResults?: api.WriteResult[]): void {
200207
this.writeStream!.callOnMessage({
201208
// Convert to base64 string so it can later be parsed into ByteString.
@@ -215,6 +222,7 @@ class MockConnection implements Connection {
215222
private resetAndCloseWriteStream(err?: FirestoreError): void {
216223
this.writeSendBarriers = [];
217224
this.earlyWrites = [];
225+
this.writeOpen = new Deferred<void>();
218226
this.writeStream!.callOnClose(err);
219227
this.writeStream = null;
220228
}
@@ -280,11 +288,16 @@ class MockConnection implements Connection {
280288
this.resetAndCloseWriteStream();
281289
}
282290
});
283-
this.queue.enqueueAndForget(async () => {
284-
if (this.writeStream === writeStream) {
285-
writeStream.callOnOpen();
286-
}
287-
});
291+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
292+
this.queue
293+
.enqueue(async () => {
294+
if (this.writeStream === writeStream) {
295+
writeStream.callOnOpen();
296+
}
297+
})
298+
.then(() => {
299+
this.writeOpen.resolve();
300+
});
288301
this.writeStream = writeStream;
289302
// Replace 'any' with conditional types.
290303
return writeStream as any; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -313,12 +326,16 @@ class MockConnection implements Connection {
313326
}
314327
});
315328
// Call on open immediately after returning
316-
this.queue.enqueueAndForget(async () => {
317-
if (this.watchStream === watchStream) {
318-
watchStream.callOnOpen();
329+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
330+
this.queue
331+
.enqueue(async () => {
332+
if (this.watchStream === watchStream) {
333+
watchStream.callOnOpen();
334+
}
335+
})
336+
.then(() => {
319337
this.watchOpen.resolve();
320-
}
321-
});
338+
});
322339
this.watchStream = watchStream;
323340
// Replace 'any' with conditional types.
324341
return this.watchStream as any; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -931,6 +948,9 @@ abstract class TestRunner {
931948
private async doEnableNetwork(): Promise<void> {
932949
this.networkEnabled = true;
933950
await this.syncEngine.enableNetwork();
951+
if (this.connection.writeStream !== null) {
952+
await this.connection.waitForWriteOpen();
953+
}
934954
}
935955

936956
private async doShutdown(): Promise<void> {

0 commit comments

Comments
 (0)