Skip to content

Commit 3be0938

Browse files
author
Michael Lehenbauer
committed
Fix bug and add tests.
1 parent c7e1ec9 commit 3be0938

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

packages/firestore/src/remote/remote_store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export class RemoteStore implements TargetMetadataProvider {
248248
if (objUtils.isEmpty(this.listenTargets)) {
249249
if (this.watchStream.isOpen()) {
250250
this.watchStream.markIdle();
251-
} else {
251+
} else if (this.canUseNetwork()) {
252252
// Revert to OnlineState.Unknown if the watch stream is not open and we
253253
// have no listeners, since without any listens to send we cannot
254254
// confirm if the stream is healthy and upgrade to OnlineState.Online.

packages/firestore/test/integration/api/database.test.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
import * as chai from 'chai';
18+
import * as chaiAsPromised from 'chai-as-promised';
19+
1720
import { expect } from 'chai';
1821
import * as firestore from '@firebase/firestore-types';
1922

@@ -29,6 +32,8 @@ import {
2932
} from '../util/helpers';
3033
import { query } from '../../util/api_helpers';
3134

35+
chai.use(chaiAsPromised);
36+
3237
const Timestamp = firebase.firestore.Timestamp;
3338

3439
apiDescribe('Database', persistence => {
@@ -856,26 +861,25 @@ apiDescribe('Database', persistence => {
856861
});
857862
});
858863

859-
it('can get documents while offline', () => {
860-
return withTestDoc(persistence, docRef => {
864+
it('can get documents while offline', async () => {
865+
await withTestDoc(persistence, async docRef => {
861866
const firestore = docRef.firestore;
862867

863-
return firestore.disableNetwork().then(() => {
864-
const writePromise = docRef.set({ foo: 'bar' });
865-
866-
return docRef
867-
.get()
868-
.then(doc => {
869-
expect(doc.metadata.fromCache).to.be.true;
870-
return firestore.enableNetwork();
871-
})
872-
.then(() => writePromise)
873-
.then(() => docRef.get())
874-
.then(doc => {
875-
expect(doc.metadata.fromCache).to.be.false;
876-
expect(doc.data()).to.deep.equal({ foo: 'bar' });
877-
});
878-
});
868+
await firestore.disableNetwork();
869+
await expect(docRef.get()).to.eventually.be.rejectedWith(
870+
'Failed to get document because the client is offline.'
871+
);
872+
873+
const writePromise = docRef.set({ foo: 'bar' });
874+
const doc = await docRef.get();
875+
expect(doc.metadata.fromCache).to.be.true;
876+
877+
await firestore.enableNetwork();
878+
await writePromise;
879+
880+
const doc2 = await docRef.get();
881+
expect(doc2.metadata.fromCache).to.be.false;
882+
expect(doc2.data()).to.deep.equal({ foo: 'bar' });
879883
});
880884
});
881885

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,21 @@ describeSpec('Offline:', [], () => {
215215
);
216216
}
217217
);
218+
219+
specTest('Queries return from cache when network disabled', [], () => {
220+
const query = Query.atPath(path('collection'));
221+
return (
222+
spec()
223+
.disableNetwork()
224+
.userListens(query)
225+
.expectEvents(query, { fromCache: true })
226+
.userUnlistens(query)
227+
228+
// There was once a bug where removing the last listener accidentally
229+
// reverted us to OnlineState.Unknown, so make sure it works a second time
230+
.userListens(query)
231+
.expectEvents(query, { fromCache: true })
232+
.userUnlistens(query)
233+
);
234+
});
218235
});

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ abstract class TestRunner {
370370
[targetId: number]: { query: SpecQuery; resumeToken: string };
371371
};
372372

373+
private networkEnabled = true;
374+
373375
private datastore: Datastore;
374376
private localStore: LocalStore;
375377
private remoteStore: RemoteStore;
@@ -603,7 +605,7 @@ abstract class TestRunner {
603605
);
604606
}
605607

606-
if (this.isPrimaryClient) {
608+
if (this.isPrimaryClient && this.networkEnabled) {
607609
// Open should always have happened after a listen
608610
await this.connection.waitForWatchOpen();
609611
}
@@ -847,6 +849,7 @@ abstract class TestRunner {
847849
}
848850

849851
private async doDisableNetwork(): Promise<void> {
852+
this.networkEnabled = false;
850853
// Make sure to execute all writes that are currently queued. This allows us
851854
// to assert on the total number of requests sent before shutdown.
852855
await this.remoteStore.fillWritePipeline();
@@ -858,6 +861,7 @@ abstract class TestRunner {
858861
}
859862

860863
private async doEnableNetwork(): Promise<void> {
864+
this.networkEnabled = true;
861865
await this.syncEngine.enableNetwork();
862866
}
863867

@@ -1015,7 +1019,7 @@ abstract class TestRunner {
10151019
}
10161020

10171021
private async validateActiveTargets(): Promise<void> {
1018-
if (!this.isPrimaryClient) {
1022+
if (!this.isPrimaryClient || !this.networkEnabled) {
10191023
expect(this.connection.activeTargets).to.be.empty;
10201024
return;
10211025
}

0 commit comments

Comments
 (0)