-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathServiceRegistryClient.test.ts
More file actions
285 lines (245 loc) · 9.65 KB
/
ServiceRegistryClient.test.ts
File metadata and controls
285 lines (245 loc) · 9.65 KB
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
import { beforeAll, afterEach, expect, jest, test } from "@jest/globals";
import { ServiceRegistryClient } from "../sdk/clients/service-registry";
import { orkesConductorClient } from "../sdk/createConductorClient";
import { ServiceType } from "../open-api";
import * as fs from "fs";
import * as path from "path";
import { describeForOrkesV5 } from "./utils/customJestDescribe";
// Conductor must be able to fetch this URL for discovery; use CONDUCTOR_TEST_SERVICE_URI
// when testing against a remote cluster (e.g. a public Swagger URL it can reach).
const TEST_SERVICE_URI =
process.env.CONDUCTOR_TEST_SERVICE_URI ??
"http://httpbin-server:8081/api-docs";
describeForOrkesV5("ServiceRegistryClient", () => {
const clientPromise = orkesConductorClient();
let serviceRegistryClient: ServiceRegistryClient;
const testServicesToCleanup: string[] = [];
beforeAll(async () => {
const client = await clientPromise;
serviceRegistryClient = new ServiceRegistryClient(client);
});
afterEach(async () => {
// Clean up any services created during tests
for (const serviceName of testServicesToCleanup) {
try {
await serviceRegistryClient.removeService(serviceName);
} catch (e) {
// Ignore cleanup errors - service might already be deleted or not exist
console.debug(`Failed to cleanup service ${serviceName}:`, e);
}
}
testServicesToCleanup.length = 0;
});
jest.setTimeout(15000);
test("Should add and retrieve a service registry", async () => {
// Create a test service registry
const testServiceRegistry = {
name: `jsSdkTest-test_service_registry${Date.now()}`,
type: ServiceType.HTTP,
serviceURI: TEST_SERVICE_URI,
config: {
circuitBreakerConfig: {
failureRateThreshold: 50.0,
slidingWindowSize: 100,
minimumNumberOfCalls: 100,
waitDurationInOpenState: 1000,
permittedNumberOfCallsInHalfOpenState: 100,
slowCallRateThreshold: 50.0,
slowCallDurationThreshold: 100,
automaticTransitionFromOpenToHalfOpenEnabled: true,
maxWaitDurationInHalfOpenState: 1,
},
},
};
// Add service to cleanup list
testServicesToCleanup.push(testServiceRegistry.name);
// Register the service registry
await expect(
serviceRegistryClient.addOrUpdateService(testServiceRegistry)
).resolves.not.toThrow();
// Retrieve the service registry from the API
const retrievedServiceRegistry = await serviceRegistryClient.getService(
testServiceRegistry.name
);
// Verify the service registry properties
if (!retrievedServiceRegistry) {
throw new Error("Retrieved service registry is undefined");
}
expect(retrievedServiceRegistry.name).toEqual(testServiceRegistry.name);
expect(retrievedServiceRegistry.type).toEqual(testServiceRegistry.type);
expect(retrievedServiceRegistry.serviceURI).toEqual(
testServiceRegistry.serviceURI
);
// Verify circuit breaker config
const expectedConfig = testServiceRegistry.config.circuitBreakerConfig;
const actualConfig = retrievedServiceRegistry.config?.circuitBreakerConfig;
expect(actualConfig?.failureRateThreshold).toEqual(
expectedConfig.failureRateThreshold
);
expect(actualConfig?.slidingWindowSize).toEqual(
expectedConfig.slidingWindowSize
);
expect(actualConfig?.minimumNumberOfCalls).toEqual(
expectedConfig.minimumNumberOfCalls
);
expect(actualConfig?.waitDurationInOpenState).toEqual(
expectedConfig.waitDurationInOpenState
);
expect(actualConfig?.permittedNumberOfCallsInHalfOpenState).toEqual(
expectedConfig.permittedNumberOfCallsInHalfOpenState
);
expect(actualConfig?.slowCallRateThreshold).toEqual(
expectedConfig.slowCallRateThreshold
);
expect(actualConfig?.slowCallDurationThreshold).toEqual(
expectedConfig.slowCallDurationThreshold
);
expect(actualConfig?.automaticTransitionFromOpenToHalfOpenEnabled).toEqual(
expectedConfig.automaticTransitionFromOpenToHalfOpenEnabled
);
expect(actualConfig?.maxWaitDurationInHalfOpenState).toEqual(
expectedConfig.maxWaitDurationInHalfOpenState
);
});
test("Should add and remove a service registry", async () => {
// Create a test service registry
const testServiceRegistry = {
name: `jsSdkTest-test_service_registry_to_remove-${Date.now()}`,
type: ServiceType.HTTP,
serviceURI: TEST_SERVICE_URI,
};
// Register the service registry
await expect(
serviceRegistryClient.addOrUpdateService(testServiceRegistry)
).resolves.not.toThrow();
// Verify it was added
await expect(
serviceRegistryClient.getService(testServiceRegistry.name)
).resolves.not.toBeNull();
// Remove the service registry
await expect(
serviceRegistryClient.removeService(testServiceRegistry.name)
).resolves.not.toThrow();
// Verify it was removed - should throw an error when trying to get it
await expect(
serviceRegistryClient.getService(testServiceRegistry.name)
).resolves.toBeUndefined();
});
test("Should add a service method to a registry", async () => {
// Create a test service registry
const testServiceRegistry = {
name: `jsSdkTest-test_service_registry_with_method-${Date.now()}`,
type: ServiceType.HTTP,
serviceURI: TEST_SERVICE_URI,
};
// Add service to cleanup list
testServicesToCleanup.push(testServiceRegistry.name);
// Register the service registry
await expect(
serviceRegistryClient.addOrUpdateService(testServiceRegistry)
).resolves.not.toThrow();
// Create a test service method
const testServiceMethod = {
operationName: "testOperation",
methodName: "testMethod",
methodType: "GET",
inputType: "application/json",
outputType: "application/json",
exampleInput: {
key1: "value1",
key2: "value2",
},
};
// Add the service method
await expect(
serviceRegistryClient.addOrUpdateServiceMethod(
testServiceRegistry.name,
testServiceMethod
)
).resolves.not.toThrow();
// Get the service registry to verify method was added
const retrievedServiceRegistry = await serviceRegistryClient.getService(
testServiceRegistry.name
);
if (!retrievedServiceRegistry) {
throw new Error("Retrieved service registry is undefined");
}
// Check if methods array exists and contains our method
expect(retrievedServiceRegistry.methods).toBeDefined();
// Find our method in the array
const foundMethod = retrievedServiceRegistry.methods?.find(
(method) => method.methodName === testServiceMethod.methodName
);
expect(foundMethod).toBeDefined();
expect(foundMethod?.operationName).toEqual(testServiceMethod.operationName);
expect(foundMethod?.methodType).toEqual(testServiceMethod.methodType);
expect(foundMethod?.inputType).toEqual(testServiceMethod.inputType);
expect(foundMethod?.outputType).toEqual(testServiceMethod.outputType);
});
test("Should discover methods from a http service", async () => {
// Create a test service registry for discovery
const testServiceRegistry = {
name: `jsSdkTest-test_service_registry_discovery-${Date.now()}`,
type: ServiceType.HTTP,
serviceURI: TEST_SERVICE_URI,
};
// Add service to cleanup list
testServicesToCleanup.push(testServiceRegistry.name);
// Register the service registry
await serviceRegistryClient.addOrUpdateService(testServiceRegistry);
// Attempt to discover methods - this will fail the test if discovery fails
const discoveredMethods = await serviceRegistryClient.discover(
testServiceRegistry.name,
true
);
// Verify that we discovered methods
expect(discoveredMethods).toBeDefined();
if (!discoveredMethods) {
throw new Error("Discovered methods are undefined");
}
expect(Array.isArray(discoveredMethods)).toBe(true);
expect(discoveredMethods.length).toBeGreaterThan(0);
if (discoveredMethods.length > 0) {
// Check that the discovered methods have the expected properties
const firstMethod = discoveredMethods[0];
expect(firstMethod.methodName).toBeDefined();
expect(firstMethod.methodType).toBeDefined();
}
});
test("Should discover methods from a gRPC service", async () => {
// Create a test service registry for discovery
const testServiceRegistry = {
name: `jsSdkTest-test_gRPC_service_registry_discovery-${Date.now()}`,
type: ServiceType.gRPC,
serviceURI: "grpcbin:50051",
};
// Add service to cleanup list
testServicesToCleanup.push(testServiceRegistry.name);
// Register the service registry
await serviceRegistryClient.addOrUpdateService(testServiceRegistry);
const filePath = path.join(__dirname, "metadata", "compiled.bin");
const fileBuffer = fs.readFileSync(filePath);
const blob = new Blob([fileBuffer], { type: "application/octet-stream" });
// Set proto data
await serviceRegistryClient.setProtoData(
testServiceRegistry.name,
"compiled.bin",
blob
);
const serviceMethods = await serviceRegistryClient.getService(
testServiceRegistry.name
);
if (!serviceMethods) {
throw new Error("Service methods are undefined");
}
const methods = serviceMethods.methods;
expect(serviceMethods).toBeDefined();
expect(methods?.length).toBeGreaterThan(0);
expect(Array.isArray(methods)).toBe(true);
if (methods) {
const firstMethod = methods[0];
expect(firstMethod.methodName).toBeDefined();
expect(firstMethod.methodType).toBeDefined();
}
});
});