Skip to content

check client offline state and test #6579

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

Merged
merged 7 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 20 additions & 2 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { toByteStreamReader } from '../platform/byte_stream_reader';
import { newSerializer, newTextEncoder } from '../platform/serializer';
import { Datastore } from '../remote/datastore';
import {
canUseNetwork,
RemoteStore,
remoteStoreDisableNetwork,
remoteStoreEnableNetwork,
Expand Down Expand Up @@ -507,9 +508,26 @@ export function firestoreClientRunAggregationQuery(
client: FirestoreClient,
query: AggregateQuery
): Promise<AggregateQuerySnapshot> {
return client.asyncQueue.enqueue(() => {
return getAggregate(query);
const deferred = new Deferred<AggregateQuerySnapshot>();
client.asyncQueue.enqueueAndForget(async () => {
const remoteStore = await getRemoteStore(client);
if (!canUseNetwork(remoteStore)) {
deferred.reject(
new FirestoreError(
Code.UNAVAILABLE,
'Failed to get aggregate result because the client is offline.'
)
);
} else {
try {
const result = await getAggregate(query);
deferred.resolve(result);
} catch (e) {
deferred.reject(e as Error);
}
}
});
return deferred.promise;
}

async function readDocumentFromCache(
Expand Down
18 changes: 18 additions & 0 deletions packages/firestore/test/integration/api/aggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { expect } from 'chai';

import {
countQuery,
disableNetwork,
getAggregateFromServerDirect,
query
} from '../util/firebase_export';
Expand All @@ -38,4 +39,21 @@ apiDescribe('Aggregation query', (persistence: boolean) => {
expect(snapshot.getCount()).to.equal(3);
});
});

it('getAggregateFromServerDirect fails if user is offline', () => {
const testDocs = {};
return withTestCollection(
persistence,
testDocs,
async (collection, firestore) => {
await disableNetwork(firestore);
const countQuery_ = countQuery(collection);
await expect(
getAggregateFromServerDirect(countQuery_)
).to.be.eventually.rejectedWith(
'Failed to get aggregate result because the client is offline'
);
}
);
});
});