2
2
3
3
DEVELOPMENT:
4
4
5
- pnpm test --forceExit service.test.ts
5
+ pnpm test ./ service.test.ts
6
6
7
7
*/
8
8
9
9
import { callNatsService , createNatsService } from "@cocalc/conat/service" ;
10
10
import { once } from "@cocalc/util/async-utils" ;
11
- import "@cocalc/backend/conat" ;
11
+ import { before , after } from "@cocalc/backend/conat/test/setup" ;
12
+ import { wait } from "@cocalc/backend/conat/test/util" ;
13
+
14
+ beforeAll ( before ) ;
12
15
13
16
describe ( "create a service and test it out" , ( ) => {
14
17
let s ;
@@ -23,16 +26,34 @@ describe("create a service and test it out", () => {
23
26
) ;
24
27
} ) ;
25
28
26
- it ( "closes the services" , async ( ) => {
29
+ it ( "closes the services and observes it doesn't work anymore " , async ( ) => {
27
30
s . close ( ) ;
28
31
29
32
let t = "" ;
30
- // expect( ...).toThrow doesn't seem to work with this:
31
- try {
33
+ await expect ( async ( ) => {
32
34
await callNatsService ( { service : "echo" , mesg : "hi" , timeout : 1000 } ) ;
33
- } catch ( err ) {
34
- t = `${ err } ` ;
35
- }
36
- expect ( t ) . toContain ( "Error: timeout" ) ;
35
+ } ) . rejects . toThrowError ( "timeout" ) ;
36
+ } ) ;
37
+ } ) ;
38
+
39
+ describe ( "verify that you can create a service AFTER calling it and things to still work fine" , ( ) => {
40
+ let result = "" ;
41
+ it ( "call a service that does not exist yet" , ( ) => {
42
+ ( async ( ) => {
43
+ result = await callNatsService ( { service : "echo3" , mesg : "hello " } ) ;
44
+ } ) ( ) ;
45
+ } ) ;
46
+
47
+ it ( "create the echo3 service and observe that it answer the request we made before the service was created" , async ( ) => {
48
+ const s = createNatsService ( {
49
+ service : "echo3" ,
50
+ handler : ( mesg ) => mesg . repeat ( 3 ) ,
51
+ } ) ;
52
+ await wait ( { until : ( ) => result } ) ;
53
+ expect ( result ) . toBe ( "hello hello hello " ) ;
54
+
55
+ s . close ( ) ;
37
56
} ) ;
38
57
} ) ;
58
+
59
+ afterAll ( after ) ;
0 commit comments