-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpostgres-pubsub.test.js
201 lines (172 loc) · 6.33 KB
/
postgres-pubsub.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Adapted from https://github.com/apollographql/graphql-subscriptions/blob/master/src/test/tests.ts
const { isAsyncIterable } = require("iterall");
const { Client } = require("pg");
const { PostgresPubSub } = require("./postgres-pubsub");
let client;
describe("PostgresPubSub", () => {
beforeEach(async () => {
client = new Client();
await client.connect();
});
test("PostgresPubSub can subscribe when instantiated without a client", function(done) {
const ps = new PostgresPubSub();
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).toBe(true);
});
});
test("PostgresPubSub can subscribe and is called when events happen", function(done) {
const ps = new PostgresPubSub({ client });
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).toBe(true);
});
});
test("PostgresPubSub can subscribe when instantiated with connection options but without a client", function(done) {
const ps = new PostgresPubSub({
connectionString: process.env.DATABASE_URL
});
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
done();
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).toBe(true);
});
});
test("should send notification event after calling publish", done => {
const ps = new PostgresPubSub({ client });
client.on("notification", ({ payload }) => {
expect(payload).toEqual("test");
done();
});
ps.subscribe("a", payload => {
expect(payload).toEqual("test");
}).then(() => {
const succeed = ps.publish("a", "test");
expect(succeed).toBe(true);
});
});
test("PostgresPubSub can unsubscribe", function(done) {
const ps = new PostgresPubSub({ client });
ps.subscribe("a", payload => {
expect(false).toBe(true); // Should not reach this point
}).then(subId => {
ps.unsubscribe(subId);
const succeed = ps.publish("a", "test");
expect(succeed).toBe(true); // True because publish success is not
// indicated by trigger having subscriptions
done(); // works because pubsub is synchronous
});
});
test("Should emit error when payload exceeds Postgres 8000 character limit", done => {
const ps = new PostgresPubSub({ client });
ps.subscribe("a", () => {
expect(false).toBe(true); // Should not reach this point
done();
});
ps.subscribe("error", err => {
expect(err.message).toEqual("payload string too long");
done();
}).then(() => {
const succeed = ps.publish("a", "a".repeat(9000));
expect(succeed).toBe(true);
});
});
test("AsyncIterator should expose valid asyncIterator for a specific event", () => {
const eventName = "test";
const ps = new PostgresPubSub({ client });
const iterator = ps.asyncIterator(eventName);
expect(iterator).not.toBeUndefined();
expect(isAsyncIterable(iterator)).toBe(true);
});
test("AsyncIterator should trigger event on asyncIterator when published", done => {
const eventName = "test";
const ps = new PostgresPubSub({ client });
const iterator = ps.asyncIterator(eventName);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).not.toBeUndefined();
expect(result.done).not.toBeUndefined();
done();
});
ps.publish(eventName, { test: true });
});
test("AsyncIterator should not trigger event on asyncIterator when publishing other event", done => {
const eventName = "test2";
const ps = new PostgresPubSub({ client });
const iterator = ps.asyncIterator("test");
const spy = jest.fn();
iterator.next().then(spy);
ps.publish(eventName, { test: true });
expect(spy).not.toHaveBeenCalled();
done();
});
test("AsyncIterator should register to multiple events", done => {
const eventName = "test2";
const ps = new PostgresPubSub({ client });
const iterator = ps.asyncIterator(["test", "test2"]);
const spy = jest.fn();
iterator.next().then(() => {
spy();
expect(spy).toHaveBeenCalled();
done();
});
ps.publish(eventName, { test: true });
});
test("AsyncIterator transforms messages using commonMessageHandler", done => {
const eventName = "test";
const commonMessageHandler = message => ({ transformed: message });
const ps = new PostgresPubSub({ client, commonMessageHandler });
const iterator = ps.asyncIterator(eventName);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).toEqual({ transformed: { test: true } });
expect(result.done).toBe(false);
done();
});
ps.publish(eventName, { test: true });
});
test("PostgresPubSub transforms messages using commonMessageHandler", function(done) {
const commonMessageHandler = message => ({ transformed: message });
const ps = new PostgresPubSub({ client, commonMessageHandler });
ps.subscribe("transform", payload => {
expect(payload).toEqual({ transformed: { test: true } });
done();
}).then(() => {
const succeed = ps.publish("transform", { test: true });
expect(succeed).toBe(true);
});
});
// This test does not clean up after it ends. It breaks the test that follows after it.
// It won't break any tests if it's the last. https://imgflip.com/i/2lmlgm
// TODO: Fix it properly
test("AsyncIterator should not trigger event on asyncIterator already returned", async done => {
const eventName = "test";
const ps = new PostgresPubSub({ client });
const iterator = ps.asyncIterator(eventName);
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).not.toBeUndefined();
expect(result.done).toBe(false);
});
ps.publish(eventName, { test: true });
await delay(0);
iterator.next().then(result => {
expect(result).not.toBeUndefined();
expect(result.value).toBeUndefined();
expect(result.done).toBe(true);
done();
});
await delay(0);
iterator.return();
ps.publish(eventName, { test: true });
});
});