@@ -9,6 +9,10 @@ pnpm test ./dstream.test.ts
9
9
10
10
import { createDstream as create } from "./util" ;
11
11
import { dstream as createDstream } from "@cocalc/backend/conat/sync" ;
12
+ import { once } from "@cocalc/util/async-utils" ;
13
+ import { connect , before , after } from "@cocalc/backend/conat/test/setup" ;
14
+
15
+ beforeAll ( before ) ;
12
16
13
17
describe ( "create a dstream and do some basic operations" , ( ) => {
14
18
let s ;
@@ -54,7 +58,6 @@ describe("create a dstream and do some basic operations", () => {
54
58
} ) ;
55
59
} ) ;
56
60
57
- /*
58
61
describe ( "create two dstreams and observe sync between them" , ( ) => {
59
62
const name = `test-${ Math . random ( ) } ` ;
60
63
let s1 , s2 ;
@@ -93,13 +96,16 @@ describe("create two dstreams and observe sync between them", () => {
93
96
// now kick off the two saves *in parallel*
94
97
s1 . save ( ) ;
95
98
s2 . save ( ) ;
96
- await once(s1, "change");
97
- if (s2.length != s1.length) {
99
+ while ( s1 . length < 4 ) {
100
+ await once ( s1 , "change" ) ;
101
+ }
102
+ while ( s2 . length < 4 ) {
98
103
await once ( s2 , "change" ) ;
99
104
}
100
105
expect ( s1 . getAll ( ) ) . toEqual ( s2 . getAll ( ) ) ;
101
- // in fact s1,s2 is the order since we called s1.save first:
102
- expect(s1.getAll()).toEqual(["hello", "hi from s2", "s1", "s2"]);
106
+ expect ( new Set ( s1 . getAll ( ) ) ) . toEqual (
107
+ new Set ( [ "hello" , "hi from s2" , "s1" , "s2" ] ) ,
108
+ ) ;
103
109
} ) ;
104
110
} ) ;
105
111
@@ -156,7 +162,7 @@ describe("closing also saves by default, but not if autosave is off", () => {
156
162
157
163
it ( "creates stream and write a message" , async ( ) => {
158
164
// noAutosave: false is the default:
159
- s = await createDstream({ name, noAutosave: false});
165
+ s = await createDstream ( { name, noAutosave : false } ) ;
160
166
s . push ( 389 ) ;
161
167
} ) ;
162
168
@@ -210,7 +216,7 @@ describe("testing start_seq", () => {
210
216
expect ( s . start_seq ) . toEqual ( seq [ 2 ] ) ;
211
217
} ) ;
212
218
213
- it("it then pulls in the previous message, so now two messages are loaded", async () => {
219
+ it . skip ( "it then pulls in the previous message, so now two messages are loaded" , async ( ) => {
214
220
await s . load ( { start_seq : seq [ 1 ] } ) ;
215
221
expect ( s . length ) . toBe ( 2 ) ;
216
222
expect ( s . getAll ( ) ) . toEqual ( [ 2 , 3 ] ) ;
@@ -253,51 +259,52 @@ describe("dstream typescript test", () => {
253
259
} ) ;
254
260
} ) ;
255
261
256
- import { numSubscriptions } from "@cocalc/conat/client";
262
+ describe ( "ensure there isn't a really obvious subscription leak" , ( ) => {
263
+ let client ;
257
264
258
- describe("ensure there are no NATS subscription leaks", () => {
259
- // There is some slight slack at some point due to the clock stuff,
260
- // inventory, etc. It is constant and small, whereas we allocate
261
- // a large number of kv's in the test.
262
- const SLACK = 4;
265
+ it ( "create a client, which initially has only one subscription (the inbox)" , async ( ) => {
266
+ client = connect ( ) ;
267
+ expect ( client . numSubscriptions ( ) ) . toBe ( 1 ) ;
268
+ } ) ;
263
269
264
- it("creates and closes many kv's and checks there is no leak", async () => {
265
- const before = numSubscriptions();
266
- const COUNT = 20 ;
270
+ const count = 100 ;
271
+ it ( `creates and closes ${ count } streams and checks there is no leak` , async ( ) => {
272
+ const before = client . numSubscriptions ( ) ;
267
273
// create
268
274
const a : any = [ ] ;
269
- for (let i = 0; i < COUNT ; i++) {
275
+ for ( let i = 0 ; i < count ; i ++ ) {
270
276
a [ i ] = await createDstream ( {
271
277
name : `${ Math . random ( ) } ` ,
272
- noAutosave: true,
273
278
} ) ;
274
279
}
275
- for (let i = 0; i < COUNT ; i++) {
280
+ for ( let i = 0 ; i < count ; i ++ ) {
276
281
await a [ i ] . close ( ) ;
277
282
}
278
- const after = numSubscriptions();
279
- expect(Math.abs(after - before)).toBeLessThan(SLACK);
283
+ const after = client . numSubscriptions ( ) ;
284
+ expect ( after ) . toBe ( before ) ;
285
+
286
+ // also check count on server went down.
287
+ expect ( ( await client . getSubscriptions ( ) ) . size ) . toBe ( before ) ;
280
288
} ) ;
281
289
282
- it("does another leak test, but with a set operation each time", async () => {
283
- const before = numSubscriptions();
284
- const COUNT = 20;
290
+ it ( "does another leak test, but with a publish operation each time" , async ( ) => {
291
+ const before = client . numSubscriptions ( ) ;
285
292
// create
286
293
const a : any = [ ] ;
287
- for (let i = 0; i < COUNT ; i++) {
294
+ for ( let i = 0 ; i < count ; i ++ ) {
288
295
a [ i ] = await createDstream ( {
289
296
name : `${ Math . random ( ) } ` ,
290
297
noAutosave : true ,
291
298
} ) ;
292
299
a [ i ] . publish ( i ) ;
293
300
await a [ i ] . save ( ) ;
294
301
}
295
- for (let i = 0; i < COUNT; i++) {
296
- await a[i].purge();
302
+ for ( let i = 0 ; i < count ; i ++ ) {
297
303
await a [ i ] . close ( ) ;
298
304
}
299
- const after = numSubscriptions();
300
- expect(Math.abs( after - before)).toBeLessThan(SLACK );
305
+ const after = client . numSubscriptions ( ) ;
306
+ expect ( after ) . toBe ( before ) ;
301
307
} ) ;
302
308
} ) ;
303
- */
309
+
310
+ afterAll ( after ) ;
0 commit comments