Skip to content
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

Firestore: query.test.ts: use a different Firestore instance instead of a WriteBatch #7486

Conversation

dconeybe
Copy link
Contributor

@dconeybe dconeybe commented Jul 20, 2023

Use a different Firestore instance instead of a WriteBatch to avoid affecting the local cache in query.test.ts.

I had originally thought that WriteBatch sidestepped the local cache, just like a transaction did; however, that turned out to be incorrect and using a WriteBatch nullified the test cases that were modified to use it. This is a follow-up to #7415.

This PR was ported to the Android SDK in firebase/firebase-android-sdk#5184 and to the iOS SDK in firebase/firebase-ios-sdk#11615.

…of a WriteBatch to avoid affecting the local cache.

This fixes the bug introduced by #7415 where WriteBatch affects the local cache (which I didn't realize).
@dconeybe dconeybe self-assigned this Jul 20, 2023
@changeset-bot
Copy link

changeset-bot bot commented Jul 20, 2023

⚠️ No Changeset found

Latest commit: c59f042

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dconeybe dconeybe changed the title Firestore: query.test.ts: use a different Firestore instance instead … Firestore: query.test.ts: use a different Firestore instance instead of a WriteBatch Jul 21, 2023
@google-oss-bot
Copy link
Contributor

@google-oss-bot
Copy link
Contributor

@dconeybe dconeybe marked this pull request as ready for review July 21, 2023 12:32
@dconeybe dconeybe requested review from a team as code owners July 21, 2023 12:32
@dconeybe dconeybe requested a review from milaGGL July 21, 2023 12:32
@DellaBitta
Copy link
Contributor

Checking to see if this should be in the release notes.

Is this a change related to a user issue? Should users expect a behavior change?

@dconeybe
Copy link
Contributor Author

Checking to see if this should be in the release notes.

Is this a change related to a user issue? Should users expect a behavior change?

No, this is just a fix to an integration test. No user behavior is affected.

@DellaBitta
Copy link
Contributor

Ah, I see. My bad! Thanks.

deletedDocumentIds.add(documentToDelete.id);
}
await writeBatchForDocumentDeletes.commit();
await withTestDb(PERSISTENCE_MODE_UNSPECIFIED, async db2 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this be returning the same Firestore instance(db === db2)? How it would help us avoid affecting cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question (I had to look into this too). But the answer is no because withTestDb() creates and returns a newly-created, distinct Firestore instance. Therefore, db and db2 have distinct caches and do not share anything, and using db2 to delete/update documents does not affect the local cache of db.

Here is the definition of withTestDb():

export function withTestDb(
fn: (db: Firestore) => void | Promise<void>
): Promise<void> {
return withTestDbSettings(DEFAULT_PROJECT_ID, DEFAULT_SETTINGS, fn);
}

which calls withTestDbSettings():

export async function withTestDbSettings(
projectId: string,
settings: FirestoreSettings,
fn: (db: Firestore) => void | Promise<void>
): Promise<void> {
const app = initializeApp(
{ apiKey: 'fake-api-key', projectId },
'test-app-' + appCount++
);
const firestore = initializeFirestore(app, settings);
return fn(firestore);
}

which creates a brand new FirestoreApp with a unique name (the appCount counter increments for each invocation) and then creates a brand new Firestore object from it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. So they are instances to a same firestore, but from different apps.

    { apiKey: 'fake-api-key', projectId },
    'test-app-' + appCount++
  );``` 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, exactly.

@dconeybe dconeybe requested a review from milaGGL July 24, 2023 16:33
Copy link
Contributor

@milaGGL milaGGL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dconeybe dconeybe merged commit a5bf94c into master Jul 24, 2023
@dconeybe dconeybe deleted the dconeybe/BloomFilterTestUseDifferentInstanceInsteadOfWriteBatch branch July 24, 2023 17:26
dconeybe added a commit to firebase/firebase-ios-sdk that referenced this pull request Jul 26, 2023
@firebase firebase locked and limited conversation to collaborators Aug 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants