Skip to content

Commit 5adc39e

Browse files
committed
feat: Add 120 second wait for Integration tests to assure functions are ready to receive events
1 parent a2c0451 commit 5adc39e

File tree

4 files changed

+84
-62
lines changed

4 files changed

+84
-62
lines changed

integration_tests/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ node_modules/
6868
/serviceAccount.json
6969
/functions/requirements.txt
7070
/functions/firebase_functions.tar.gz
71+
/functions/functions.iml

integration_tests/functions/v2/firestore_tests.py

+21-22
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ def firestoreOnDocumentCreatedTests(
1010
event: firestore_fn.Event[firestore_fn.DocumentSnapshot]):
1111
documentId = event.params['documentId']
1212

13-
firestore.client().collection('firestoreOnDocumentCreatedTests').document(
14-
documentId).set({
15-
'time': event.time,
16-
'id': event.id,
17-
'type': event.type,
18-
'source': event.source,
19-
})
13+
firestore.client().collection('firestoreOnDocumentCreatedTests').document(documentId).set({
14+
'time': event.time,
15+
'id': event.id,
16+
'type': event.type,
17+
'source': event.source,
18+
})
2019

2120

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

2928
firestore.client().collection('firestoreOnDocumentDeletedTests').document(
3029
documentId).set({
31-
'time': event.time,
32-
'id': event.id,
33-
'type': event.type,
34-
'source': event.source,
35-
})
30+
'time': event.time,
31+
'id': event.id,
32+
'type': event.type,
33+
'source': event.source,
34+
})
3635

3736

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

4544
firestore.client().collection('firestoreOnDocumentUpdatedTests').document(
4645
documentId).set({
47-
'time': event.time,
48-
'id': event.id,
49-
'type': event.type,
50-
'source': event.source,
51-
})
46+
'time': event.time,
47+
'id': event.id,
48+
'type': event.type,
49+
'source': event.source,
50+
})
5251

5352

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

6160
firestore.client().collection('firestoreOnDocumentWrittenTests').document(
6261
documentId).set({
63-
'time': event.time,
64-
'id': event.id,
65-
'type': event.type,
66-
'source': event.source,
67-
})
62+
'time': event.time,
63+
'id': event.id,
64+
'type': event.type,
65+
'source': event.source,
66+
})

integration_tests/run.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function generateUniqueHash(originalName: string): string {
8181
const modifiedName = `${TEST_RUN_ID}-${originalName}`;
8282
if (modifiedName.length > 100) {
8383
throw new Error(
84-
`Function name is too long. Original=${originalName}, Modified=${modifiedName}`
84+
`Function name is too long. Original=${originalName}, Modified=${modifiedName}`,
8585
);
8686
}
8787
return modifiedName;
@@ -103,7 +103,7 @@ async function discoverAndModifyEndpoints() {
103103
port,
104104
config.projectId,
105105
config.runtime,
106-
10000
106+
10000,
107107
);
108108

109109
modifiedYaml = {
@@ -115,7 +115,7 @@ async function discoverAndModifyEndpoints() {
115115
delete modifiedValue.project;
116116
delete modifiedValue.runtime;
117117
return [modifiedKey, modifiedValue];
118-
})
118+
}),
119119
),
120120
specVersion: "v1alpha1",
121121
};
@@ -189,7 +189,7 @@ function cleanFiles(): void {
189189
try {
190190
const files = fs.readdirSync(".");
191191
files.forEach((file) => {
192-
if (file.match(`firebase_functions_${TEST_RUN_ID}.tar.gz`)) {
192+
if (file.match(`firebase_functions.tar.gz`)) {
193193
fs.rmSync(file);
194194
}
195195
if (file.match("package.json")) {
@@ -214,7 +214,7 @@ function cleanFiles(): void {
214214
const spawnAsync = (
215215
command: string,
216216
args: string[],
217-
options: any
217+
options: any,
218218
): Promise<string> => {
219219
return new Promise((resolve, reject) => {
220220
const child = spawn(command, args, options);
@@ -246,7 +246,7 @@ async function runTests(): Promise<void> {
246246
...process.env,
247247
GOOGLE_APPLICATION_CREDENTIALS: path.join(
248248
__dirname,
249-
"serviceAccount.json"
249+
"serviceAccount.json",
250250
),
251251
TEST_RUN_ID,
252252
},

integration_tests/tests/v2/firestore.test.ts

+56-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import admin from "firebase-admin";
22
import { timeout } from "../utils";
33
import { initializeFirebase } from "../firebaseSetup";
44

5-
describe("Cloud Firestore (v2)", () => {
5+
describe("Cloud Firestore", () => {
66
const projectId = process.env.PROJECT_ID;
77
const testId = process.env.TEST_RUN_ID;
88

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

1313
beforeAll(async () => {
1414
await initializeFirebase();
15-
});
15+
await timeout(120_000)
16+
}, 200_000);
1617

1718
afterAll(async () => {
1819
await admin
@@ -35,6 +36,7 @@ describe("Cloud Firestore (v2)", () => {
3536
.collection("firestoreOnDocumentWrittenTests")
3637
.doc(testId)
3738
.delete();
39+
await admin.firestore().collection("tests").doc(testId).delete();
3840
});
3941

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

50-
await timeout(20000);
52+
let retry = 0;
5153

52-
const logSnapshot = await admin
53-
.firestore()
54-
.collection("firestoreOnDocumentCreatedTests")
55-
.doc(testId)
56-
.get();
57-
loggedContext = logSnapshot.data();
54+
while (retry < 10) {
55+
const logSnapshot = await admin
56+
.firestore()
57+
.collection("firestoreOnDocumentCreatedTests")
58+
.doc(testId)
59+
.get();
60+
loggedContext = logSnapshot.data();
61+
62+
if (loggedContext) break;
63+
await timeout(5000);
64+
retry++;
65+
}
5866

5967
if (!loggedContext) {
6068
throw new Error("loggedContext is undefined");
6169
}
62-
});
70+
}, 60000);
6371

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

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

7987
it("should have the correct type", () => {
8088
expect(loggedContext?.type).toEqual(
81-
"google.cloud.firestore.document.v1.created"
89+
"google.cloud.firestore.document.v1.created",
8290
);
8391
});
8492

@@ -122,21 +130,21 @@ describe("Cloud Firestore (v2)", () => {
122130
if (!loggedContext) {
123131
throw new Error("loggedContext is undefined");
124132
}
125-
});
133+
}, 60000);
126134

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

131139
it("should have well-formed source", () => {
132140
expect(loggedContext?.source).toMatch(
133-
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`
141+
`//firestore.googleapis.com/projects/${projectId}/databases/(default)`,
134142
);
135143
});
136144

137145
it("should have the correct type", () => {
138146
expect(loggedContext?.type).toEqual(
139-
"google.cloud.firestore.document.v1.deleted"
147+
"google.cloud.firestore.document.v1.deleted",
140148
);
141149
});
142150

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

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

168-
await timeout(20000);
169-
170176
// Refresh snapshot
171177
dataSnapshot = await docRef.get();
172178

173-
const logSnapshot = await admin
174-
.firestore()
175-
.collection("firestoreOnDocumentUpdatedTests")
176-
.doc(testId)
177-
.get();
178-
loggedContext = logSnapshot.data();
179+
let retry = 0;
180+
181+
while (retry < 10) {
182+
const logSnapshot = await admin
183+
.firestore()
184+
.collection("firestoreOnDocumentUpdatedTests")
185+
.doc(testId)
186+
.get();
187+
loggedContext = logSnapshot.data();
188+
189+
if (loggedContext) break;
190+
await timeout(5000);
191+
retry++;
192+
}
179193

180194
if (!loggedContext) {
181195
throw new Error("loggedContext is undefined");
182196
}
183-
});
197+
}, 60000);
184198

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

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

195209
it("should have the correct type", () => {
196210
expect(loggedContext?.type).toEqual(
197-
"google.cloud.firestore.document.v1.updated"
211+
"google.cloud.firestore.document.v1.updated",
198212
);
199213
});
200214

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

224238
await timeout(20000);
225239

226-
const logSnapshot = await admin
227-
.firestore()
228-
.collection("firestoreOnDocumentWrittenTests")
229-
.doc(testId)
230-
.get();
231-
loggedContext = logSnapshot.data();
240+
let retry = 0;
241+
242+
while (retry < 10) {
243+
const logSnapshot = await admin
244+
.firestore()
245+
.collection("firestoreOnDocumentWrittenTests")
246+
.doc(testId)
247+
.get();
248+
loggedContext = logSnapshot.data();
249+
250+
if (loggedContext) break;
251+
await timeout(5000);
252+
retry++;
253+
}
232254

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

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

253275
it("should have the correct type", () => {
254276
expect(loggedContext?.type).toEqual(
255-
"google.cloud.firestore.document.v1.written"
277+
"google.cloud.firestore.document.v1.written",
256278
);
257279
});
258280

0 commit comments

Comments
 (0)