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

The new feature withFunctionTriggersDisabled not really disable the triggers #2857

Closed
hexetia opened this issue Nov 13, 2020 · 4 comments · Fixed by #2858
Closed

The new feature withFunctionTriggersDisabled not really disable the triggers #2857

hexetia opened this issue Nov 13, 2020 · 4 comments · Fixed by #2858

Comments

@hexetia
Copy link

hexetia commented Nov 13, 2020

  • Operating System version: Linux / Ubuntu 20.04
  • Browser version: Node 12
  • Firebase SDK version: 8.0.2, @firebase/rules-unit-testing: 1.1.1, firebase-admin: 9.4.0
  • Firebase Product: functions

The problem

Firebase 8.0.1 comes with a testing feature to disable functions triggers in emulator, I tried to use that new feature to clear Firestore data without triggering some triggers to make my tests more reliable, but that not worked, the triggers are called even wrapping custom firestore queries to delete the data manually

Steps to reproduce:

  1. Init a project using firebase init
  2. Set an actual project
  3. Create a bunch of docs on a single collection (I used 'affiliates')
  4. Make sure to use the real project name in initializeTestApp to allow triggers to run
  5. After wrapping the clearFirestore code make the code wait some seconds the allow triggers to run

Repository with test

https://github.com/saculbr/disableTriggersIssue

Relevant Code:

test('disable triggers test', async () => {
    await firebase.withFunctionTriggersDisabled(async () => {
        return await firebase.clearFirestoreData({ projectId: APP_ID });
    });

     // sleep three seconds to allow triggers to run
     await sleep(3000);

     const doc = await db.collection('bar').doc('bar').get();
     expect(doc.data()).toBeUndefined()
}, 10000); // increase test timeout
export const onDeleteAffiliate = functions
    .firestore.document(`affiliates/{affiliateId}`)
    .onDelete(async (docSnap, context) => {
        await sleep(100);
        await db.collection('bar').doc('bar').set({bar: 'bar'});
    });
@google-oss-bot
Copy link
Contributor

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@samtstern samtstern self-assigned this Nov 13, 2020
@samtstern
Copy link
Contributor

samtstern commented Nov 13, 2020

@saculbr you're right I was able to reproduce this! I think this is a fairly simple race condition where this happens:

  1. Functions triggers are disabled
  2. Wait for firestore delete operations to finish
  3. Function triggers are enabled
  4. Firestore emulator sends the delete triggers to the Functions emulator a fraction of a second later

I think this theory is correct because adding a sleep works:

  await firebase.withFunctionTriggersDisabled(async () => {
    await firebase.clearFirestoreData({
      projectId: 'fir-dumpster'
    });

    // This prevents the race
    await sleep(5000);
  });

@samtstern
Copy link
Contributor

Moving this issue to firebase-tools since I'm going to fix this inside the Firebase CLI

@samtstern samtstern transferred this issue from firebase/firebase-js-sdk Nov 23, 2020
@samtstern
Copy link
Contributor

@saculbr fixed in #2858 and will be released in the next version of the Firebase CLI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants