3
3
4
4
const { expect } = require ( 'aegir/utils/chai' )
5
5
const { AbortController } = require ( 'native-abort-controller' )
6
+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
7
+ const defer = require ( 'p-defer' )
6
8
7
9
const f = require ( './utils/factory' ) ( )
8
10
9
11
describe ( '.pubsub' , function ( ) {
10
12
this . timeout ( 20 * 1000 )
11
13
describe ( '.subscribe' , ( ) => {
14
+ /** @type {import('ipfs-core-types').IPFS } */
12
15
let ipfs
16
+ /** @type {any } */
13
17
let ctl
14
18
15
19
beforeEach ( async function ( ) {
@@ -27,8 +31,7 @@ describe('.pubsub', function () {
27
31
it ( '.onError when connection is closed' , async ( ) => {
28
32
const topic = 'gossipboom'
29
33
let messageCount = 0
30
- let onError
31
- const error = new Promise ( resolve => { onError = resolve } )
34
+ const onError = defer ( )
32
35
33
36
await ipfs . pubsub . subscribe ( topic , message => {
34
37
messageCount ++
@@ -38,47 +41,44 @@ describe('.pubsub', function () {
38
41
ctl . stop ( ) . catch ( )
39
42
}
40
43
} , {
41
- onError
44
+ onError : onError . resolve
42
45
} )
43
46
44
- await ipfs . pubsub . publish ( topic , 'hello' )
45
- await ipfs . pubsub . publish ( topic , 'bye' )
47
+ await ipfs . pubsub . publish ( topic , uint8ArrayFromString ( 'hello' ) )
48
+ await ipfs . pubsub . publish ( topic , uint8ArrayFromString ( 'bye' ) )
46
49
47
- await expect ( error ) . to . eventually . be . fulfilled ( ) . and . to . be . instanceOf ( Error )
50
+ await expect ( onError . promise ) . to . eventually . be . fulfilled ( ) . and . to . be . instanceOf ( Error )
48
51
} )
49
52
50
53
it ( 'does not call onError when aborted' , async ( ) => {
51
54
const controller = new AbortController ( )
52
55
const topic = 'gossipabort'
53
56
const messages = [ ]
54
- let onError
55
- let onReceived
56
-
57
- const received = new Promise ( resolve => { onReceived = resolve } )
58
- const error = new Promise ( resolve => { onError = resolve } )
57
+ const onError = defer ( )
58
+ const onReceived = defer ( )
59
59
60
60
await ipfs . pubsub . subscribe ( topic , message => {
61
61
messages . push ( message )
62
62
if ( messages . length === 2 ) {
63
- onReceived ( )
63
+ onReceived . resolve ( )
64
64
}
65
65
} , {
66
- onError,
66
+ onError : onError . resolve ,
67
67
signal : controller . signal
68
68
} )
69
69
70
- await ipfs . pubsub . publish ( topic , 'hello' )
71
- await ipfs . pubsub . publish ( topic , 'bye' )
70
+ await ipfs . pubsub . publish ( topic , uint8ArrayFromString ( 'hello' ) )
71
+ await ipfs . pubsub . publish ( topic , uint8ArrayFromString ( 'bye' ) )
72
72
73
- await received
73
+ await onReceived . promise
74
74
controller . abort ( )
75
75
76
76
// Stop the daemon
77
77
await ctl . stop ( )
78
78
// Just to make sure no error is caused by above line
79
- setTimeout ( onError , 200 , 'aborted' )
79
+ setTimeout ( onError . resolve , 200 , 'aborted' )
80
80
81
- await expect ( error ) . to . eventually . be . fulfilled ( ) . and . to . equal ( 'aborted' )
81
+ await expect ( onError . promise ) . to . eventually . be . fulfilled ( ) . and . to . equal ( 'aborted' )
82
82
} )
83
83
} )
84
84
} )
0 commit comments