|
1 | | -import { Channel } from "@grpc/grpc-js"; |
2 | | - |
3 | | -import { |
4 | | - BaseOptions, |
5 | | - DNSClusterOptions, |
6 | | - KurrentDBClient, |
7 | | -} from "@eventstore/db-client"; |
8 | | - |
9 | | -/* |
10 | | -Mocking this file breaks grpc, but allows us to check the settings passed to grpc |
11 | | -These tests need to be kept seperate to any tests that need to actually make calls |
12 | | -*/ |
13 | | -jest.mock("@grpc/grpc-js/build/src/channel.js"); |
14 | | -const ChannelMock = Channel as jest.Mock<Channel>; |
| 1 | +import { DNSClusterOptions, KurrentDBClient } from "@eventstore/db-client"; |
15 | 2 |
|
16 | 3 | describe("deadline", () => { |
17 | | - beforeEach(() => { |
18 | | - ChannelMock.mockClear(); |
19 | | - }); |
20 | | - |
21 | | - describe.each< |
22 | | - [ |
23 | | - test_name: string, |
24 | | - connection_string: string, |
25 | | - constructor_options: Partial<DNSClusterOptions>, |
26 | | - call_options: BaseOptions, |
27 | | - expected: number |
28 | | - ] |
29 | | - >([ |
30 | | - ["should default to 10_000", "esdb://host", {}, {}, 10_000], |
31 | | - [ |
32 | | - "should be settable: 1", |
33 | | - "esdb://host?defaultDeadline=1", |
34 | | - { defaultDeadline: 1 }, |
35 | | - {}, |
36 | | - 1, |
37 | | - ], |
38 | | - [ |
39 | | - "should be settable: 100000", |
40 | | - "esdb://host?defaultDeadline=100000", |
41 | | - { defaultDeadline: 10_0000 }, |
42 | | - {}, |
43 | | - 10_0000, |
44 | | - ], |
45 | | - [ |
46 | | - "passing in call options should override default", |
47 | | - "esdb://host", |
48 | | - {}, |
49 | | - { deadline: 10_0000 }, |
50 | | - 10_0000, |
51 | | - ], |
52 | | - [ |
53 | | - "passing in call options should override settings", |
54 | | - "esdb://host?defaultDeadline=100000", |
55 | | - { defaultDeadline: 10_0000 }, |
56 | | - { deadline: 10 }, |
57 | | - 10, |
58 | | - ], |
59 | | - ])("%s", (_, connectionString, constructorOptions, callOptions, expected) => { |
60 | | - test.each([ |
61 | | - [ |
62 | | - "connectionString", |
63 | | - () => KurrentDBClient.connectionString(connectionString), |
64 | | - ], |
65 | | - [ |
66 | | - "constructor", |
67 | | - () => |
68 | | - new KurrentDBClient({ |
69 | | - endpoint: "host:1234", |
70 | | - ...constructorOptions, |
71 | | - }), |
72 | | - ], |
73 | | - ])("%s", async (_, createClient) => { |
74 | | - const warnSpy = jest.spyOn(console, "warn").mockImplementation(); |
75 | | - const client = createClient(); |
76 | | - const before = Date.now(); |
77 | | - |
78 | | - try { |
79 | | - await client.restartSubsystem(callOptions); |
80 | | - } catch (_) { |
81 | | - // We're not actually connecting to anything, just triggering channel creation |
82 | | - } |
83 | | - |
84 | | - const after = Date.now(); |
85 | | - |
86 | | - const ChannelInstance = ChannelMock.mock.instances[0]; |
87 | | - const createCall = ChannelInstance.createCall as unknown as jest.Mock< |
88 | | - Channel["createCall"] |
89 | | - >; |
90 | | - |
91 | | - const deadline = createCall.mock.calls[0][1].getTime(); |
92 | | - |
93 | | - expect(deadline).toBeGreaterThanOrEqual(before + expected); |
94 | | - expect(deadline).toBeLessThanOrEqual(after + expected); |
95 | | - |
96 | | - expect(warnSpy).not.toBeCalled(); |
97 | | - warnSpy.mockRestore(); |
98 | | - }); |
99 | | - }); |
100 | | - |
101 | 4 | describe.each< |
102 | 5 | [ |
103 | 6 | test_name: string, |
|
0 commit comments