Skip to content

Commit 507e984

Browse files
authored
check client offline state and test (#6579)
1 parent fe871ce commit 507e984

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/firestore/src/core/firestore_client.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { toByteStreamReader } from '../platform/byte_stream_reader';
4040
import { newSerializer, newTextEncoder } from '../platform/serializer';
4141
import { Datastore } from '../remote/datastore';
4242
import {
43+
canUseNetwork,
4344
RemoteStore,
4445
remoteStoreDisableNetwork,
4546
remoteStoreEnableNetwork,
@@ -507,9 +508,26 @@ export function firestoreClientRunAggregationQuery(
507508
client: FirestoreClient,
508509
query: AggregateQuery
509510
): Promise<AggregateQuerySnapshot> {
510-
return client.asyncQueue.enqueue(() => {
511-
return getAggregate(query);
511+
const deferred = new Deferred<AggregateQuerySnapshot>();
512+
client.asyncQueue.enqueueAndForget(async () => {
513+
const remoteStore = await getRemoteStore(client);
514+
if (!canUseNetwork(remoteStore)) {
515+
deferred.reject(
516+
new FirestoreError(
517+
Code.UNAVAILABLE,
518+
'Failed to get aggregate result because the client is offline.'
519+
)
520+
);
521+
} else {
522+
try {
523+
const result = await getAggregate(query);
524+
deferred.resolve(result);
525+
} catch (e) {
526+
deferred.reject(e as Error);
527+
}
528+
}
512529
});
530+
return deferred.promise;
513531
}
514532

515533
async function readDocumentFromCache(

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { expect } from 'chai';
1919

2020
import {
2121
countQuery,
22+
disableNetwork,
2223
getAggregateFromServerDirect,
2324
query
2425
} from '../util/firebase_export';
@@ -38,4 +39,21 @@ apiDescribe('Aggregation query', (persistence: boolean) => {
3839
expect(snapshot.getCount()).to.equal(3);
3940
});
4041
});
42+
43+
it('getAggregateFromServerDirect fails if user is offline', () => {
44+
const testDocs = {};
45+
return withTestCollection(
46+
persistence,
47+
testDocs,
48+
async (collection, firestore) => {
49+
await disableNetwork(firestore);
50+
const countQuery_ = countQuery(collection);
51+
await expect(
52+
getAggregateFromServerDirect(countQuery_)
53+
).to.be.eventually.rejectedWith(
54+
'Failed to get aggregate result because the client is offline'
55+
);
56+
}
57+
);
58+
});
4159
});

0 commit comments

Comments
 (0)