Skip to content

Commit 054d856

Browse files
committed
switch to firestore based events
1 parent 1839978 commit 054d856

File tree

4 files changed

+31
-53
lines changed

4 files changed

+31
-53
lines changed

integration_test/cli.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ async function writeFirebaseJson(codebase: string): Promise<void> {
7575
functions: [
7676
{
7777
source: "functions",
78-
codebase: codebase,
7978
disallowLegacyRuntimeConfig: true,
8079
ignore: [
8180
"node_modules",

integration_test/functions/src/firestore.v2.test.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,53 @@
1-
import EventEmitter from "node:events";
21
import { describe, it, beforeAll, expect } from "vitest";
3-
import { PubSub } from "@google-cloud/pubsub";
42
import { firestore } from "./utils";
53
import { GeoPoint } from "firebase-admin/firestore";
64
const RUN_ID = String(process.env.RUN_ID);
75

8-
const pubsub = new PubSub({ projectId: "cf3-integration-tests-v2-qa" });
9-
const emitter = new EventEmitter();
10-
116
function waitForEvent<T = unknown>(
127
event: string,
138
trigger: () => Promise<void>,
149
timeoutMs: number = 60_000
1510
): Promise<T> {
1611
return new Promise<T>((resolve, reject) => {
17-
emitter.on(event, (data: T) => {
18-
emitter.off(event, resolve);
19-
resolve(data);
12+
let timer: NodeJS.Timeout | null = null;
13+
14+
const unsubscribe = firestore.collection(`${RUN_ID}_snapshots`).doc(event).onSnapshot(snapshot => {
15+
if (snapshot.exists) {
16+
console.log("snapshot", snapshot.data());
17+
if (timer) clearTimeout(timer);
18+
unsubscribe();
19+
resolve(snapshot.data() as T);
20+
}
2021
});
2122

22-
setTimeout(() => {
23-
emitter.off(event, resolve);
24-
reject(new Error("Timeout waiting for event: " + event));
23+
timer = setTimeout(() => {
24+
unsubscribe();
25+
reject(new Error(`Timeout waiting for event "${event}" after ${timeoutMs}ms`));
2526
}, timeoutMs);
2627

27-
trigger().catch(reject);
28+
trigger().then().catch(reject);
2829
});
2930
}
3031

31-
beforeAll(async () => {
32-
const topic = pubsub.topic('vitest');
33-
const subscription = topic.subscription('vitest-sub');
34-
35-
subscription.on("message", (message) => {
36-
console.log("message", message.data.toString());
37-
const data = message.data.length ? JSON.parse(message.data.toString()) : null;
38-
message.ack();
39-
40-
if (!("event" in data)) {
41-
throw new Error("Invalid event data: " + JSON.stringify(data));
42-
}
43-
44-
emitter.emit(data.event, data.data);
45-
});
46-
47-
subscription.on("error", (error) => {
48-
console.error("Pubsub error", error);
49-
process.exit(1);
50-
});
51-
});
52-
5332
describe("firestore.v2", () => {
5433
describe("onDocumentCreated", () => {
5534
let data: any;
5635

5736
beforeAll(async () => {
5837
data = await waitForEvent("onDocumentCreated", async () => {
38+
console.log("triggering event", RUN_ID);
5939
await firestore
6040
.collection(RUN_ID)
6141
.doc("onDocumentCreated")
6242
.set({
6343
foo: "bar",
6444
timestamp: new Date(),
6545
geopoint: new GeoPoint(10, 20),
46+
}).then(() => {
47+
console.log("event triggered", RUN_ID);
6648
});
6749
});
50+
console.log("data", data);
6851
});
6952

7053
it("should be a CloudEvent", () => {
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
import { onDocumentCreated } from "firebase-functions/v2/firestore";
22
import { RUN_ID, serializeData } from "./utils";
3-
import { PubSub } from "@google-cloud/pubsub";
43
import { logger } from "firebase-functions";
4+
import { firestore } from "./utils";
55

6-
const pubsub = new PubSub();
7-
8-
async function sendEvent(event: string, data: unknown): Promise<void> {
9-
const topic = pubsub.topic('vitest');
10-
11-
await topic.publishMessage({
12-
data: event
13-
? Buffer.from(
14-
JSON.stringify({
15-
event,
16-
data,
17-
})
18-
)
19-
: Buffer.from(""),
20-
});
6+
async function sendEvent(event: string, data: any): Promise<void> {
7+
await firestore.collection(`${RUN_ID}_snapshots`).doc(event).set(serializeData(data));
218
}
229

2310
export const firestoreOnDocumentCreatedTrigger = onDocumentCreated(
2411
`${RUN_ID}/{documentId}`,
2512
async (event) => {
2613
logger.debug("onDocumentCreated", event);
27-
await sendEvent("onDocumentCreated", serializeData(event));
14+
await sendEvent("onDocumentCreated", event);
2815
}
2916
);
17+
18+
19+
export const foo = onDocumentCreated(
20+
`test/{documentId}`,
21+
async (event) => {
22+
logger.debug("onDocumentCreated", event);
23+
await sendEvent("onDocumentCreated", event);
24+
}
25+
);

integration_test/functions/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const firestore = adminApp.firestore();
66

77
export const RUN_ID = String(process.env.RUN_ID);
88

9-
export function serializeData(data: unknown): unknown {
9+
export function serializeData(data: any): any {
1010
if (data === null || data === undefined) {
1111
return null;
1212
}

0 commit comments

Comments
 (0)