@@ -7,9 +7,15 @@ pnpm test ./service.test.ts
7
7
*/
8
8
9
9
import { callConatService , createConatService } from "@cocalc/conat/service" ;
10
+ import {
11
+ createServiceClient ,
12
+ createServiceHandler ,
13
+ } from "@cocalc/conat/service/typed" ;
10
14
import { once } from "@cocalc/util/async-utils" ;
11
15
import { before , after } from "@cocalc/backend/conat/test/setup" ;
12
16
import { wait } from "@cocalc/backend/conat/test/util" ;
17
+ import { is_date as isDate } from "@cocalc/util/misc" ;
18
+ import { delay } from "awaiting" ;
13
19
14
20
beforeAll ( before ) ;
15
21
@@ -54,4 +60,62 @@ describe("verify that you can create a service AFTER calling it and things to st
54
60
} ) ;
55
61
} ) ;
56
62
63
+ describe ( "create and test a more complicated service" , ( ) => {
64
+ let client , service ;
65
+ it ( "defines the service" , async ( ) => {
66
+ interface Api {
67
+ add : ( a : number , b : number ) => Promise < number > ;
68
+ concat : ( a : Buffer , b : Buffer ) => Promise < Buffer > ;
69
+ now : ( ) => Promise < Date > ;
70
+ big : ( n : number ) => Promise < string > ;
71
+ len : ( s : string ) => Promise < number > ;
72
+ }
73
+
74
+ const name = "my-service" ;
75
+ service = await createServiceHandler < Api > ( {
76
+ service : name ,
77
+ subject : name ,
78
+ description : "My Service" ,
79
+ impl : {
80
+ // put any functions here that take/return MsgPack'able values
81
+ add : async ( a , b ) => a + b ,
82
+ concat : async ( a , b ) => Buffer . concat ( [ a , b ] ) ,
83
+ now : async ( ) => {
84
+ await delay ( 5 ) ;
85
+ return new Date ( ) ;
86
+ } ,
87
+ big : async ( n : number ) => "x" . repeat ( n ) ,
88
+ len : async ( s : string ) => s . length ,
89
+ } ,
90
+ } ) ;
91
+
92
+ client = createServiceClient < Api > ( {
93
+ service : name ,
94
+ subject : name ,
95
+ } ) ;
96
+ } ) ;
97
+
98
+ it ( "tests the service" , async ( ) => {
99
+ // these calls are all type checked using typescript
100
+ expect ( await client . add ( 2 , 3 ) ) . toBe ( 5 ) ;
101
+
102
+ const a = Buffer . from ( "hello" ) ;
103
+ const b = Buffer . from ( " conat" ) ;
104
+ expect ( await client . concat ( a , b ) ) . toEqual ( Buffer . concat ( [ a , b ] ) ) ;
105
+
106
+ const d = await client . now ( ) ;
107
+ expect ( isDate ( d ) ) . toBe ( true ) ;
108
+ expect ( Math . abs ( d . valueOf ( ) - Date . now ( ) ) ) . toBeLessThan ( 100 ) ;
109
+
110
+ const n = 10 * 1e6 ;
111
+ expect ( ( await client . big ( n ) ) . length ) . toBe ( n ) ;
112
+
113
+ expect ( await client . len ( "x" . repeat ( n ) ) ) . toBe ( n ) ;
114
+ } ) ;
115
+
116
+ it ( "cleans up" , ( ) => {
117
+ service . close ( ) ;
118
+ } ) ;
119
+ } ) ;
120
+
57
121
afterAll ( after ) ;
0 commit comments