Skip to content

Commit

Permalink
feat: Add 120 second wait for Integration tests to assure functions a…
Browse files Browse the repository at this point in the history
…re ready to receive events
  • Loading branch information
exaby73 committed Jul 30, 2024
1 parent a2c0451 commit 5adc39e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
1 change: 1 addition & 0 deletions integration_tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ node_modules/
/serviceAccount.json
/functions/requirements.txt
/functions/firebase_functions.tar.gz
/functions/functions.iml
43 changes: 21 additions & 22 deletions integration_tests/functions/v2/firestore_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ def firestoreOnDocumentCreatedTests(
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
documentId = event.params['documentId']

firestore.client().collection('firestoreOnDocumentCreatedTests').document(
documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_deleted(document='tests/{documentId}',
Expand All @@ -28,11 +27,11 @@ def firestoreOnDocumentDeletedTests(

firestore.client().collection('firestoreOnDocumentDeletedTests').document(
documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_updated(document='tests/{documentId}',
Expand All @@ -44,11 +43,11 @@ def firestoreOnDocumentUpdatedTests(

firestore.client().collection('firestoreOnDocumentUpdatedTests').document(
documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})


@firestore_fn.on_document_written(document='tests/{documentId}',
Expand All @@ -60,8 +59,8 @@ def firestoreOnDocumentWrittenTests(

firestore.client().collection('firestoreOnDocumentWrittenTests').document(
documentId).set({
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
'time': event.time,
'id': event.id,
'type': event.type,
'source': event.source,
})
12 changes: 6 additions & 6 deletions integration_tests/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function generateUniqueHash(originalName: string): string {
const modifiedName = `${TEST_RUN_ID}-${originalName}`;
if (modifiedName.length > 100) {
throw new Error(
`Function name is too long. Original=${originalName}, Modified=${modifiedName}`
`Function name is too long. Original=${originalName}, Modified=${modifiedName}`,
);
}
return modifiedName;
Expand All @@ -103,7 +103,7 @@ async function discoverAndModifyEndpoints() {
port,
config.projectId,
config.runtime,
10000
10000,
);

modifiedYaml = {
Expand All @@ -115,7 +115,7 @@ async function discoverAndModifyEndpoints() {
delete modifiedValue.project;
delete modifiedValue.runtime;
return [modifiedKey, modifiedValue];
})
}),
),
specVersion: "v1alpha1",
};
Expand Down Expand Up @@ -189,7 +189,7 @@ function cleanFiles(): void {
try {
const files = fs.readdirSync(".");
files.forEach((file) => {
if (file.match(`firebase_functions_${TEST_RUN_ID}.tar.gz`)) {
if (file.match(`firebase_functions.tar.gz`)) {
fs.rmSync(file);
}
if (file.match("package.json")) {
Expand All @@ -214,7 +214,7 @@ function cleanFiles(): void {
const spawnAsync = (
command: string,
args: string[],
options: any
options: any,
): Promise<string> => {
return new Promise((resolve, reject) => {
const child = spawn(command, args, options);
Expand Down Expand Up @@ -246,7 +246,7 @@ async function runTests(): Promise<void> {
...process.env,
GOOGLE_APPLICATION_CREDENTIALS: path.join(
__dirname,
"serviceAccount.json"
"serviceAccount.json",
),
TEST_RUN_ID,
},
Expand Down
90 changes: 56 additions & 34 deletions integration_tests/tests/v2/firestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import admin from "firebase-admin";
import { timeout } from "../utils";
import { initializeFirebase } from "../firebaseSetup";

describe("Cloud Firestore (v2)", () => {
describe("Cloud Firestore", () => {
const projectId = process.env.PROJECT_ID;
const testId = process.env.TEST_RUN_ID;

Expand All @@ -12,7 +12,8 @@ describe("Cloud Firestore (v2)", () => {

beforeAll(async () => {
await initializeFirebase();
});
await timeout(120_000)
}, 200_000);

afterAll(async () => {
await admin
Expand All @@ -35,6 +36,7 @@ describe("Cloud Firestore (v2)", () => {
.collection("firestoreOnDocumentWrittenTests")
.doc(testId)
.delete();
await admin.firestore().collection("tests").doc(testId).delete();
});

describe("Document created trigger", () => {
Expand All @@ -47,19 +49,25 @@ describe("Cloud Firestore (v2)", () => {
await docRef.set({ test: testId });
dataSnapshot = await docRef.get();

await timeout(20000);
let retry = 0;

const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentCreatedTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();
while (retry < 10) {
const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentCreatedTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();

if (loggedContext) break;
await timeout(5000);
retry++;
}

if (!loggedContext) {
throw new Error("loggedContext is undefined");
}
});
}, 60000);

it("should not have event.app", () => {
expect(loggedContext?.app).toBeUndefined();
Expand All @@ -72,13 +80,13 @@ describe("Cloud Firestore (v2)", () => {

it("should have well-formed resource", () => {
expect(loggedContext?.source).toMatch(
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`,
);
});

it("should have the correct type", () => {
expect(loggedContext?.type).toEqual(
"google.cloud.firestore.document.v1.created"
"google.cloud.firestore.document.v1.created",
);
});

Expand Down Expand Up @@ -122,21 +130,21 @@ describe("Cloud Firestore (v2)", () => {
if (!loggedContext) {
throw new Error("loggedContext is undefined");
}
});
}, 60000);

it("should not have event.app", () => {
expect(loggedContext?.app).toBeUndefined();
});

it("should have well-formed source", () => {
expect(loggedContext?.source).toMatch(
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`,
);
});

it("should have the correct type", () => {
expect(loggedContext?.type).toEqual(
"google.cloud.firestore.document.v1.deleted"
"google.cloud.firestore.document.v1.deleted",
);
});

Expand Down Expand Up @@ -165,36 +173,42 @@ describe("Cloud Firestore (v2)", () => {

await docRef.update({ test: testId });

await timeout(20000);

// Refresh snapshot
dataSnapshot = await docRef.get();

const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentUpdatedTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();
let retry = 0;

while (retry < 10) {
const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentUpdatedTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();

if (loggedContext) break;
await timeout(5000);
retry++;
}

if (!loggedContext) {
throw new Error("loggedContext is undefined");
}
});
}, 60000);

it("should not have event.app", () => {
expect(loggedContext?.app).toBeUndefined();
});

it("should have well-formed resource", () => {
expect(loggedContext?.source).toMatch(
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`,
);
});

it("should have the correct type", () => {
expect(loggedContext?.type).toEqual(
"google.cloud.firestore.document.v1.updated"
"google.cloud.firestore.document.v1.updated",
);
});

Expand Down Expand Up @@ -223,12 +237,20 @@ describe("Cloud Firestore (v2)", () => {

await timeout(20000);

const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentWrittenTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();
let retry = 0;

while (retry < 10) {
const logSnapshot = await admin
.firestore()
.collection("firestoreOnDocumentWrittenTests")
.doc(testId)
.get();
loggedContext = logSnapshot.data();

if (loggedContext) break;
await timeout(5000);
retry++;
}

if (!loggedContext) {
throw new Error("loggedContext is undefined");
Expand All @@ -246,13 +268,13 @@ describe("Cloud Firestore (v2)", () => {

it("should have well-formed resource", () => {
expect(loggedContext?.source).toMatch(
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`,
);
});

it("should have the correct type", () => {
expect(loggedContext?.type).toEqual(
"google.cloud.firestore.document.v1.written"
"google.cloud.firestore.document.v1.written",
);
});

Expand Down

0 comments on commit 5adc39e

Please sign in to comment.