-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathclient.test.ts
558 lines (509 loc) · 16.9 KB
/
client.test.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {
CommunicationProtocolEnum,
DaprClient,
LogLevel,
StateConcurrencyEnum,
StateConsistencyEnum,
} from "../../../src";
import { sleep } from "../../../src/utils/NodeJS.util";
import { LockStatus } from "../../../src/types/lock/UnlockResponse";
const daprHost = "127.0.0.1";
const daprGrpcPort = "50000";
const daprHttpPort = "3500";
const loggerSettings = {
level: LogLevel.Debug,
};
describe("common/client", () => {
let httpClient: DaprClient;
let grpcClient: DaprClient;
beforeAll(async () => {
httpClient = new DaprClient({
daprHost,
daprPort: daprHttpPort,
communicationProtocol: CommunicationProtocolEnum.HTTP,
logger: loggerSettings,
});
await httpClient.daprClient.getClient();
grpcClient = new DaprClient({
daprHost,
daprPort: daprGrpcPort,
communicationProtocol: CommunicationProtocolEnum.GRPC,
logger: loggerSettings,
});
await grpcClient.daprClient.getClient();
}, 10 * 1000);
afterAll(async () => {
await httpClient.stop();
await grpcClient.stop();
});
// Helper function to run the test for both HTTP and gRPC.
const runIt = (name: string, fn: (client: DaprClient) => void) => {
it("http/" + name, () => fn(httpClient));
it("grpc/" + name, () => fn(grpcClient));
};
describe("client", () => {
runIt("should return isInitialized is true if the sidecar has been started", async (client: DaprClient) => {
expect(client.getIsInitialized()).toBe(true);
});
});
describe("pubsub", () => {
const pubSubName = "pubsub-redis";
const topic = "test-topic";
const ce = {
specversion: "1.0",
type: "com.github.pull.create",
source: "https://github.com/cloudevents/spec/pull",
id: "A234-1234-1234",
};
runIt("should be able to publish a plain text", async (client: DaprClient) => {
const res = await client.pubsub.publish(pubSubName, topic, "Hello World");
expect(res.error).toEqual(undefined);
});
runIt("should be able to publish a JSON", async (client: DaprClient) => {
const res = await client.pubsub.publish(pubSubName, topic, { hello: "world" });
expect(res.error).toEqual(undefined);
});
runIt("should be able to publish a cloud event", async (client: DaprClient) => {
const res = await client.pubsub.publish(pubSubName, topic, ce);
expect(res.error).toEqual(undefined);
});
runIt("should be able to publish multiple plain text messages", async (client: DaprClient) => {
const messages = ["Hello World", "Hello World 2"];
const res = await client.pubsub.publishBulk(pubSubName, topic, messages);
expect(res.failedMessages.length).toEqual(0);
});
runIt("should be able to publish multiple JSON messages", async (client: DaprClient) => {
const messages = [{ hello: "world" }, { hello: "world 2" }];
const res = await client.pubsub.publishBulk(pubSubName, topic, messages);
expect(res.failedMessages.length).toEqual(0);
});
runIt("should be able to publish multiple custom bulk publish messages", async (client: DaprClient) => {
const messages = [
{
entryID: "1",
event: "Hello World",
contentType: "text/plain",
},
{
entryID: "2",
event: { hello: "world" },
contentType: "application/json",
},
{
entryID: "3",
event: { ...ce, data: "Hello World", datacontenttype: "text/plain" },
contentType: "application/cloudevents+json",
},
];
const res = await client.pubsub.publishBulk(pubSubName, topic, messages);
expect(res.failedMessages.length).toEqual(0);
});
runIt("should fail the entire request on duplicate entry IDs", async (client: DaprClient) => {
const messages = [
{
entryID: "1",
event: "Hello World",
contentType: "text/plain",
},
{
entryID: "1",
event: { hello: "world 2" },
contentType: "application/json",
},
{
entryID: "3",
event: "Hello World 3",
contentType: "text/plain",
},
];
const res = await client.pubsub.publishBulk(pubSubName, topic, messages);
expect(res.failedMessages.length).toEqual(3);
});
});
describe("distributed lock", () => {
runIt("should be able to acquire a new lock and unlock", async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
const lock = await client.lock.lock("redislock", resourceId, "owner1", 1000);
expect(lock.success).toEqual(true);
const unlock = await client.lock.unlock("redislock", resourceId, "owner1");
expect(unlock.status).toEqual(LockStatus.Success);
});
runIt("should be not be able to unlock when the lock is not acquired", async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
const unlock = await client.lock.unlock("redislock", resourceId, "owner1");
expect(unlock.status).toEqual(LockStatus.LockDoesNotExist);
});
runIt("should be able to acquire a lock after the previous lock is expired", async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
let lock = await client.lock.lock("redislock", resourceId, "owner1", 5);
expect(lock.success).toEqual(true);
await new Promise((resolve) => setTimeout(resolve, 2000));
lock = await client.lock.lock("redislock", resourceId, "owner2", 5);
expect(lock.success).toEqual(false);
});
runIt(
"should not be able to acquire a lock when the same lock is acquired by another owner",
async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
const lockOne = await client.lock.lock("redislock", resourceId, "owner1", 5);
expect(lockOne.success).toEqual(true);
const lockTwo = await client.lock.lock("redislock", resourceId, "owner2", 5);
expect(lockTwo.success).toEqual(false);
},
);
runIt(
"should be able to acquire a lock when a different lock is acquired by another owner",
async (client: DaprClient) => {
const lockOne = await client.lock.lock("redislock", crypto.randomUUID(), "owner1", 5);
expect(lockOne.success).toEqual(true);
const lockTwo = await client.lock.lock("redislock", crypto.randomUUID(), "owner2", 5);
expect(lockTwo.success).toEqual(true);
},
);
runIt(
"should not be able to acquire a lock when that lock is acquired by another owner/process",
async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
const lockOne = await client.lock.lock("redislock", resourceId, "owner3", 5);
expect(lockOne.success).toEqual(true);
const lockTwo = await client.lock.lock("redislock", resourceId, "owner4", 5);
expect(lockTwo.success).toEqual(false);
},
);
runIt(
"should not be able to unlock a lock when that lock is acquired by another owner/process",
async (client: DaprClient) => {
const resourceId = crypto.randomUUID();
const lockOne = await client.lock.lock("redislock", resourceId, "owner5", 5);
expect(lockOne.success).toEqual(true);
const unlock = await client.lock.unlock("redislock", resourceId, "owner6");
expect(unlock.status).toEqual(LockStatus.LockBelongsToOthers);
},
);
});
describe("state", () => {
const stateStoreName = "state-redis";
const stateStoreMongoDbName = "state-mongodb";
beforeEach(async () => {
await httpClient.state.delete(stateStoreName, "key-1");
await grpcClient.state.delete(stateStoreName, "key-1");
await httpClient.state.delete(stateStoreName, "key-2");
await grpcClient.state.delete(stateStoreName, "key-2");
await httpClient.state.delete(stateStoreName, "key-3");
await grpcClient.state.delete(stateStoreName, "key-3");
});
runIt("should be able to save the state", async (client: DaprClient) => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);
const res = await client.state.get(stateStoreName, "key-1");
expect(res).toEqual("value-1");
});
runIt("should be able to add metadata, etag and options", async (client: DaprClient) => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
etag: "1234",
options: {
concurrency: StateConcurrencyEnum.CONCURRENCY_FIRST_WRITE,
consistency: StateConsistencyEnum.CONSISTENCY_STRONG,
},
metadata: {
hello: "world",
ttlInSeconds: "1",
},
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);
const res1 = await client.state.get(stateStoreName, "key-1");
expect(res1).toEqual("value-1");
await sleep(2000);
const res2 = await client.state.get(stateStoreName, "key-1");
expect(res2).toBeFalsy();
});
runIt("should be able to save the state with request metadata", async (client: DaprClient) => {
await client.state.save(
stateStoreName,
[
{
key: "key-1",
value: "value-1",
metadata: {
ttlInSeconds: "1",
},
},
{
key: "key-2",
value: "value-2",
},
],
{
metadata: {
ttlInSeconds: "3", // this should override the ttl in the state item
},
},
);
const res1 = await client.state.getBulk(stateStoreName, ["key-1", "key-2"]);
expect(res1.length).toEqual(2);
expect(res1.find((r) => r.key === "key-1")?.data).toEqual("value-1");
expect(res1.find((r) => r.key === "key-2")?.data).toEqual("value-2");
// wait for the first ttl to expire
await sleep(1500);
// key-1 should still be there since its TTL is overridden by the request metadata
const res2 = await client.state.getBulk(stateStoreName, ["key-1", "key-2"]);
expect(res2.length).toEqual(2);
expect(res2.find((r) => r.key === "key-1")?.data).toEqual("value-1");
expect(res2.find((r) => r.key === "key-2")?.data).toEqual("value-2");
// wait for the second ttl to expire
await sleep(2000);
const res3 = await client.state.getBulk(stateStoreName, ["key-1", "key-2"]);
expect(res3.length).toEqual(2);
// HTTP returns undefined, gRPC returns "" for non-existent keys
expect(res3.find((r) => r.key === "key-1")?.data).toBeFalsy();
expect(res3.find((r) => r.key === "key-2")?.data).toBeFalsy();
});
runIt("should be able to get the state in bulk", async (client: DaprClient) => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);
const res = await client.state.getBulk(stateStoreName, ["key-3", "key-2"]);
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({ key: "key-2", data: "value-2" }),
expect.objectContaining({ key: "key-3", data: "value-3" }),
]),
);
});
runIt("should be able to delete a key from the state store", async (client: DaprClient) => {
await client.state.save(stateStoreName, [
{
key: "key-1",
value: "value-1",
},
{
key: "key-2",
value: "value-2",
},
{
key: "key-3",
value: "value-3",
},
]);
await client.state.delete(stateStoreName, "key-2");
const res = await client.state.get(stateStoreName, "key-2");
expect(res).toEqual("");
});
runIt(
"should be able to perform a transaction that replaces a key and deletes another",
async (client: DaprClient) => {
await client.state.transaction(stateStoreName, [
{
operation: "upsert",
request: {
key: "key-1",
value: "my-new-data-1",
},
},
{
operation: "delete",
request: {
key: "key-3",
},
},
]);
const resTransactionDelete = await client.state.get(stateStoreName, "key-3");
const resTransactionUpsert = await client.state.get(stateStoreName, "key-1");
expect(resTransactionDelete).toEqual("");
expect(resTransactionUpsert).toEqual("my-new-data-1");
},
);
// TODO: Use runIt when gRPC client supports query state.
it("should be able to query state", async () => {
// First save our data
await httpClient.state.save(stateStoreMongoDbName, [
{
key: "key-1",
value: {
person: {
id: 1036,
org: "Dev Ops",
},
city: "Seattle",
state: "WA",
},
},
{
key: "key-2",
value: {
person: {
id: 1037,
org: "Developers",
},
city: "Seattle",
state: "WA",
},
},
{
key: "key-3",
value: {
person: {
id: 1038,
org: "Developers",
},
city: "Seattle",
state: "WA",
},
},
{
key: "key-4",
value: {
person: {
id: 1039,
org: "Dev Ops",
},
city: "Spokane",
state: "WA",
},
},
{
key: "key-5",
value: {
person: {
id: 1040,
org: "Developers",
},
city: "Seattle",
state: "WA",
},
},
{
key: "key-6",
value: {
person: {
id: 1041,
org: "Dev Ops",
},
city: "Seattle",
state: "WA",
},
},
{
key: "key-7",
value: {
person: {
id: 1042,
org: "Finance",
},
city: "Brussels",
state: "Flemish-Brabant",
},
},
{
key: "key-8",
value: {
person: {
id: 1043,
org: "Finance",
},
city: "San Francisco",
state: "CA",
},
},
]);
const res = await httpClient.state.query(stateStoreMongoDbName, {
filter: {
OR: [
{
EQ: { "person.org": "Dev Ops" },
},
{
AND: [
{
EQ: { "person.org": "Finance" },
},
{
IN: { state: ["CA", "WA"] },
},
],
},
],
},
sort: [
{
key: "state",
order: "DESC",
},
],
page: {
limit: 10,
},
});
expect(res.results).toBeDefined();
expect(res.results.length).toEqual(4);
expect(res.results.map((i) => i.key).indexOf("key-1")).toBeGreaterThan(-1);
expect(res.results.map((i) => i.key).indexOf("key-4")).toBeGreaterThan(-1);
expect(res.results.map((i) => i.key).indexOf("key-6")).toBeGreaterThan(-1);
expect(res.results.map((i) => i.key).indexOf("key-8")).toBeGreaterThan(-1);
for (let i = 1; i <= 8; i++) {
await httpClient.state.delete(stateStoreMongoDbName, `key-${i}`);
}
});
it("should return an empty object when result is empty", async () => {
const result = await httpClient.state.query(stateStoreMongoDbName, {
filter: { EQ: { state: "statenotfound" } },
sort: [
{
key: "state",
order: "DESC",
},
],
page: {
limit: 10,
},
});
expect(result).toEqual({ results: [] });
});
});
});