Skip to content

Commit

Permalink
Fix some test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed Feb 9, 2023
1 parent 48d5077 commit 7d0b186
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 28 deletions.
1 change: 1 addition & 0 deletions packages/firestore/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export {
IndexedDbMultipleTabManager,
indexedDbLocalCache,
indexedDbMultipleTabManager,
IndexedDbSettings,
indexedDbSingleTabManager,
IndexedDbSingleTabManager,
memoryLocalCache,
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/api/cache_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MemoryLocalCacheImpl implements MemoryLocalCache {
this._offlineComponentProvider = new MemoryOfflineComponentProvider();
}

toJSON() {
toJSON(): {} {
return { kind: this.kind };
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ class IndexedDbLocalCacheImpl implements IndexedDbLocalCache {
this._offlineComponentProvider = tabManager._offlineComponentProvider!;
}

toJSON() {
toJSON(): {} {
return { kind: this.kind };
}
}
Expand Down Expand Up @@ -110,7 +110,7 @@ class SingleTabManagerImpl implements IndexedDbSingleTabManager {

constructor(private forceOwnership?: boolean) {}

toJSON() {
toJSON(): {} {
return { kind: this.kind };
}

Expand Down Expand Up @@ -139,7 +139,7 @@ class MultiTabManagerImpl implements IndexedDbMultipleTabManager {
_onlineComponentProvider?: OnlineComponentProvider;
_offlineComponentProvider?: OfflineComponentProvider;

toJSON() {
toJSON(): {} {
return { kind: this.kind };
}

Expand Down
11 changes: 9 additions & 2 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export function configureFirestore(firestore: Firestore): void {
settings.cache?._onlineComponentProvider
) {
firestore._firestoreClient.uninitializedComponentsProvider = {
offlineKind: settings.cache.kind,
offline: settings.cache._offlineComponentProvider,
online: settings.cache._onlineComponentProvider
};
Expand Down Expand Up @@ -327,7 +328,10 @@ export function enableIndexedDbPersistence(

const client = ensureFirestoreConfigured(firestore);
if (client.uninitializedComponentsProvider) {
throw new FirestoreError(Code.INVALID_ARGUMENT, 'Already specified.');
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'SDK cache is already specified.'
);
}

const settings = firestore._freezeSettings();
Expand Down Expand Up @@ -376,7 +380,10 @@ export function enableMultiTabIndexedDbPersistence(

const client = ensureFirestoreConfigured(firestore);
if (client.uninitializedComponentsProvider) {
throw new FirestoreError(Code.INVALID_ARGUMENT, 'Already specified.');
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'SDK cache is already specified.'
);
}

const settings = firestore._freezeSettings();
Expand Down
17 changes: 8 additions & 9 deletions packages/firestore/src/api/index_configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

import { getLocalStore } from '../core/firestore_client';
import { firestoreClientSetIndexConfiguration } from '../core/firestore_client';
import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader';
import { localStoreConfigureFieldIndexes } from '../local/local_store_impl';
import {
FieldIndex,
IndexKind,
Expand Down Expand Up @@ -151,17 +150,17 @@ export function setIndexConfiguration(
): Promise<void> {
firestore = cast(firestore, Firestore);
const client = ensureFirestoreConfigured(firestore);

// PORTING NOTE: We don't return an error if the user has not enabled
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
if (!client.offlineComponents?.indexBackfillerScheduler) {
if (
!client.uninitializedComponentsProvider ||
client.uninitializedComponentsProvider?.offlineKind === 'memory'
) {
// PORTING NOTE: We don't return an error if the user has not enabled
// persistence since `enableIndexeddbPersistence()` can fail on the Web.
logWarn('Cannot enable indexes when persistence is disabled');
return Promise.resolve();
}
const parsedIndexes = parseIndexes(jsonOrConfiguration);
return getLocalStore(client).then(localStore =>
localStoreConfigureFieldIndexes(localStore, parsedIndexes)
);
return firestoreClientSetIndexConfiguration(client, parsedIndexes);
}

export function parseIndexes(
Expand Down
15 changes: 15 additions & 0 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { User } from '../auth/user';
import { Query as LiteQuery } from '../lite-api/reference';
import { LocalStore } from '../local/local_store';
import {
localStoreConfigureFieldIndexes,
localStoreExecuteQuery,
localStoreGetNamedQuery,
localStoreHandleUserChange,
Expand All @@ -39,6 +40,7 @@ import {
import { Persistence } from '../local/persistence';
import { Document } from '../model/document';
import { DocumentKey } from '../model/document_key';
import { FieldIndex } from '../model/field_index';
import { Mutation } from '../model/mutation';
import { toByteStreamReader } from '../platform/byte_stream_reader';
import { newSerializer, newTextEncoder } from '../platform/serializer';
Expand Down Expand Up @@ -114,6 +116,7 @@ export class FirestoreClient {
) => Promise<void> = () => Promise.resolve();
uninitializedComponentsProvider?: {
offline: OfflineComponentProvider;
offlineKind: 'memory' | 'indexeddb';
online: OnlineComponentProvider;
};

Expand Down Expand Up @@ -768,3 +771,15 @@ function createBundleReader(
}
return newBundleReader(toByteStreamReader(content), serializer);
}

export function firestoreClientSetIndexConfiguration(
client: FirestoreClient,
indexes: FieldIndex[]
): Promise<void> {
return client.asyncQueue.enqueue(async () => {
return localStoreConfigureFieldIndexes(
await getLocalStore(client),
indexes
);
});
}
1 change: 0 additions & 1 deletion packages/firestore/src/lite-api/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
import { LRU_MINIMUM_CACHE_SIZE_BYTES } from '../local/lru_garbage_collector_impl';
import { Code, FirestoreError } from '../util/error';
import { validateIsNotUsedTogether } from '../util/input_validation';
import { logWarn } from '../util/log';

// settings() defaults:
export const DEFAULT_HOST = 'firestore.googleapis.com';
Expand Down
1 change: 1 addition & 0 deletions packages/firestore/test/integration/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ apiDescribe('Database', (persistence: boolean) => {
'cannot clear persistence if the client has been initialized',
async () => {
await withTestDoc(persistence, async (docRef, firestore) => {
await setDoc(docRef, {});
const expectedError =
'Persistence can only be cleared before a Firestore instance is ' +
'initialized or after it is terminated.';
Expand Down
4 changes: 1 addition & 3 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,7 @@ apiDescribe('Validation:', (persistence: boolean) => {
doc(db, 'foo/bar');
}
expect(() => enableIndexedDbPersistence(db)).to.throw(
'Firestore has already been started and persistence can no ' +
'longer be enabled. You can only enable persistence before ' +
'calling any other methods on a Firestore object.'
'SDK cache is already specified.'
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { apiDescribe, withTestDb } from '../util/helpers';
import { asyncQueue } from '../util/internal_helpers';

apiDescribe('Idle Timeout', (persistence: boolean) => {
it.only('can write document after idle timeout', () => {
it('can write document after idle timeout', () => {
return withTestDb(persistence, db => {
const docRef = doc(collection(db, 'test-collection'));
return setDoc(docRef, { foo: 'bar' })
Expand Down
12 changes: 4 additions & 8 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@

import { isIndexedDBAvailable } from '@firebase/util';

import {
indexedDbLocalCache,
memoryLocalCache
} from '../../../src/api/cache_config';
import { logWarn } from '../../../src/util/log';

import {
collection,
doc,
DocumentReference,
Firestore,
terminate,
indexedDbLocalCache,
clearIndexedDbPersistence,
enableIndexedDbPersistence,
CollectionReference,
Expand Down Expand Up @@ -191,10 +186,11 @@ export async function withTestDbsSettings(

for (let i = 0; i < numDbs; i++) {
// logWarn(`set persistence from helper: ${persistence}`);
const newSettings = { ...settings };
if (persistence) {
settings.cache = indexedDbLocalCache();
newSettings.cache = indexedDbLocalCache();
}
const db = newTestFirestore(newTestApp(projectId), settings);
const db = newTestFirestore(newTestApp(projectId), newSettings);
dbs.push(db);
}

Expand Down

0 comments on commit 7d0b186

Please sign in to comment.