1- import { beforeAll , describe , expect , jest , test } from "@jest/globals" ;
1+ import { beforeAll , afterEach , describe , expect , jest , test } from "@jest/globals" ;
22import { ServiceRegistryClient } from "../serviceRegistryClient" ;
33import { orkesConductorClient } from "../../orkes" ;
44import { ServiceType } from "../../common/open-api/models/ServiceRegistryModels" ;
@@ -8,20 +8,34 @@ import * as path from 'path';
88describe ( "ServiceRegistryClient" , ( ) => {
99 const clientPromise = orkesConductorClient ( { useEnvVars : true } ) ;
1010 let serviceRegistryClient : ServiceRegistryClient ;
11+ const testServicesToCleanup : string [ ] = [ ] ;
1112
1213 beforeAll ( async ( ) => {
1314 const client = await clientPromise ;
1415 serviceRegistryClient = new ServiceRegistryClient ( client ) ;
1516 } ) ;
1617
18+ afterEach ( async ( ) => {
19+ // Clean up any services created during tests
20+ for ( const serviceName of testServicesToCleanup ) {
21+ try {
22+ await serviceRegistryClient . removeService ( serviceName ) ;
23+ } catch ( e ) {
24+ // Ignore cleanup errors - service might already be deleted or not exist
25+ console . debug ( `Failed to cleanup service ${ serviceName } :` , e ) ;
26+ }
27+ }
28+ testServicesToCleanup . length = 0 ;
29+ } ) ;
30+
1731 jest . setTimeout ( 15000 ) ;
1832
1933 test ( "Should add and retrieve a service registry" , async ( ) => {
2034 // Create a test service registry
2135 const testServiceRegistry = {
2236 name : "test_service_registry" ,
2337 type : ServiceType . HTTP ,
24- serviceURI : "http://localhost :8081/api-docs" ,
38+ serviceURI : "http://httpbin :8081/api-docs" ,
2539 config : {
2640 circuitBreakerConfig : {
2741 failureRateThreshold : 50.0 ,
@@ -37,6 +51,9 @@ describe("ServiceRegistryClient", () => {
3751 }
3852 } ;
3953
54+ // Add service to cleanup list
55+ testServicesToCleanup . push ( testServiceRegistry . name ) ;
56+
4057 // Register the service registry
4158 await expect (
4259 serviceRegistryClient . addOrUpdateService ( testServiceRegistry )
@@ -72,9 +89,12 @@ describe("ServiceRegistryClient", () => {
7289 const testServiceRegistry = {
7390 name : "test_service_registry_to_remove" ,
7491 type : ServiceType . HTTP ,
75- serviceURI : "http://localhost :8081"
92+ serviceURI : "http://httpbin :8081/api-docs "
7693 } ;
7794
95+ // Add service to cleanup list
96+ testServicesToCleanup . push ( testServiceRegistry . name ) ;
97+
7898 // Register the service registry
7999 await expect (
80100 serviceRegistryClient . addOrUpdateService ( testServiceRegistry )
@@ -101,9 +121,12 @@ describe("ServiceRegistryClient", () => {
101121 const testServiceRegistry = {
102122 name : "test_service_registry_with_method" ,
103123 type : ServiceType . HTTP ,
104- serviceURI : "http://localhost:8082 "
124+ serviceURI : "http://httpbin:8081/api-docs "
105125 } ;
106126
127+ // Add service to cleanup list
128+ testServicesToCleanup . push ( testServiceRegistry . name ) ;
129+
107130 // Register the service registry
108131 await expect (
109132 serviceRegistryClient . addOrUpdateService ( testServiceRegistry )
@@ -145,102 +168,73 @@ describe("ServiceRegistryClient", () => {
145168 expect ( foundMethod ?. methodType ) . toEqual ( testServiceMethod . methodType ) ;
146169 expect ( foundMethod ?. inputType ) . toEqual ( testServiceMethod . inputType ) ;
147170 expect ( foundMethod ?. outputType ) . toEqual ( testServiceMethod . outputType ) ;
148-
149- // Clean up
150- await serviceRegistryClient . removeService ( testServiceRegistry . name ) ;
151171 } ) ;
152172
153173 test ( "Should discover methods from a http service" , async ( ) => {
154174 // Create a test service registry for discovery
155- // Note: This should point to a real service that supports discovery
156- // For HTTP services, it should point to a service with a Swagger/OpenAPI doc
157- // For gRPC services, it should point to a running gRPC service with reflection
158175 const testServiceRegistry = {
159176 name : "test_service_registry_discovery" ,
160177 type : ServiceType . HTTP ,
161- serviceURI : "http://localhost :8081/api-docs"
178+ serviceURI : "http://httpbin :8081/api-docs"
162179 } ;
163180
181+ // Add service to cleanup list
182+ testServicesToCleanup . push ( testServiceRegistry . name ) ;
183+
164184 // Register the service registry
165- await expect (
166- serviceRegistryClient . addOrUpdateService ( testServiceRegistry )
167- ) . resolves . not . toThrowError ( ) ;
185+ await serviceRegistryClient . addOrUpdateService ( testServiceRegistry ) ;
168186
169- try {
170- // Attempt to discover methods without creating them
171- const discoveredMethods = await serviceRegistryClient . discover (
172- testServiceRegistry . name ,
173- true
174- ) ;
175-
176- // Verify that we discovered at least one method
177- expect ( discoveredMethods ) . toBeDefined ( ) ;
178- expect ( Array . isArray ( discoveredMethods ) ) . toBe ( true ) ;
179-
180- // Check that we got at least one method
181- // If the service URI is valid, this should pass
182- expect ( discoveredMethods . length ) . toBeGreaterThan ( 0 ) ;
183-
184- if ( discoveredMethods . length > 0 ) {
185- // Check that the discovered methods have the expected properties
186- const firstMethod = discoveredMethods [ 0 ] ;
187- expect ( firstMethod . methodName ) . toBeDefined ( ) ;
188- expect ( firstMethod . methodType ) . toBeDefined ( ) ;
189- }
190- } catch ( error ) {
191- // If the discovery endpoint fails (e.g., if the petstore API is down),
192- // we'll log the error but not fail the test
193- console . warn ( "Discovery test failed, possibly due to external service unavailability:" , error ) ;
194- } finally {
195- // Clean up
196- await serviceRegistryClient . removeService ( testServiceRegistry . name ) ;
187+ // Attempt to discover methods - this will fail the test if discovery fails
188+ const discoveredMethods = await serviceRegistryClient . discover (
189+ testServiceRegistry . name ,
190+ true
191+ ) ;
192+
193+ // Verify that we discovered methods
194+ expect ( discoveredMethods ) . toBeDefined ( ) ;
195+ expect ( Array . isArray ( discoveredMethods ) ) . toBe ( true ) ;
196+ expect ( discoveredMethods . length ) . toBeGreaterThan ( 0 ) ;
197+
198+ if ( discoveredMethods . length > 0 ) {
199+ // Check that the discovered methods have the expected properties
200+ const firstMethod = discoveredMethods [ 0 ] ;
201+ expect ( firstMethod . methodName ) . toBeDefined ( ) ;
202+ expect ( firstMethod . methodType ) . toBeDefined ( ) ;
197203 }
198204 } ) ;
199205
200206 test ( "Should discover methods from a gRPC service" , async ( ) => {
201207 // Create a test service registry for discovery
202- // Note: This should point to a real service that supports discovery
203- // For HTTP services, it should point to a service with a Swagger/OpenAPI doc
204- // For gRPC services, it should point to a running gRPC service with reflection
205208 const testServiceRegistry = {
206209 name : "test_gRPC_service_registry_discovery" ,
207210 type : ServiceType . gRPC ,
208- serviceURI : "localhost :50051"
211+ serviceURI : "grpcbin :50051"
209212 } ;
210213
214+ // Add service to cleanup list
215+ testServicesToCleanup . push ( testServiceRegistry . name ) ;
216+
211217 // Register the service registry
212- await expect (
213- serviceRegistryClient . addOrUpdateService ( testServiceRegistry )
214- ) . resolves . not . toThrowError ( ) ;
218+ await serviceRegistryClient . addOrUpdateService ( testServiceRegistry ) ;
215219
216220 const filePath = path . join ( __dirname , 'metadata' , 'compiled.bin' ) ;
217221 const fileBuffer = fs . readFileSync ( filePath ) ;
218222 const blob = new Blob ( [ fileBuffer ] , { type : 'application/octet-stream' } ) ;
219223
220- // Register the service registry
221- await expect (
222- serviceRegistryClient . setProtoData ( testServiceRegistry . name , 'compiled.bin' , blob )
223- ) . resolves . not . toThrowError ( ) ;
224+ // Set proto data
225+ await serviceRegistryClient . setProtoData ( testServiceRegistry . name , 'compiled.bin' , blob ) ;
224226
225- try {
226- const serviceMethods = await serviceRegistryClient . getService ( testServiceRegistry . name ) . then ( ) ;
227- const methods = serviceMethods . methods ;
228- expect ( serviceMethods ) . toBeDefined ( ) ;
229- expect ( methods ?. length ) . toBeGreaterThan ( 0 ) ;
230- expect ( Array . isArray ( methods ) ) . toBe ( true ) ;
231-
232- if ( methods ) {
233- const firstMethod = methods [ 0 ] ;
234- expect ( firstMethod . methodName ) . toBeDefined ( ) ;
235- expect ( firstMethod . methodType ) . toBeDefined ( ) ;
236- }
237- } catch ( error ) {
238- // If the discovery endpoint fails (e.g., if the petstore API is down),
239- // we'll log the error but not fail the test
240- console . warn ( "Discovery test failed, possibly due to external service unavailability:" , error ) ;
241- } finally {
242- // Clean up
243- await serviceRegistryClient . removeService ( testServiceRegistry . name ) ;
227+ const serviceMethods = await serviceRegistryClient . getService ( testServiceRegistry . name ) ;
228+ const methods = serviceMethods . methods ;
229+
230+ expect ( serviceMethods ) . toBeDefined ( ) ;
231+ expect ( methods ?. length ) . toBeGreaterThan ( 0 ) ;
232+ expect ( Array . isArray ( methods ) ) . toBe ( true ) ;
233+
234+ if ( methods ) {
235+ const firstMethod = methods [ 0 ] ;
236+ expect ( firstMethod . methodName ) . toBeDefined ( ) ;
237+ expect ( firstMethod . methodType ) . toBeDefined ( ) ;
244238 }
245239 } ) ;
246240} ) ;
0 commit comments