33
44const { expect } = require ( 'aegir/utils/chai' )
55const { AbortController } = require ( 'native-abort-controller' )
6+ const uint8ArrayFromString = require ( 'uint8arrays/from-string' )
7+ const defer = require ( 'p-defer' )
68
79const f = require ( './utils/factory' ) ( )
810
911describe ( '.pubsub' , function ( ) {
1012 this . timeout ( 20 * 1000 )
1113 describe ( '.subscribe' , ( ) => {
14+ /** @type {import('ipfs-core-types').IPFS } */
1215 let ipfs
16+ /** @type {any } */
1317 let ctl
1418
1519 beforeEach ( async function ( ) {
@@ -27,8 +31,7 @@ describe('.pubsub', function () {
2731 it ( '.onError when connection is closed' , async ( ) => {
2832 const topic = 'gossipboom'
2933 let messageCount = 0
30- let onError
31- const error = new Promise ( resolve => { onError = resolve } )
34+ const onError = defer ( )
3235
3336 await ipfs . pubsub . subscribe ( topic , message => {
3437 messageCount ++
@@ -38,47 +41,44 @@ describe('.pubsub', function () {
3841 ctl . stop ( ) . catch ( )
3942 }
4043 } , {
41- onError
44+ onError : onError . resolve
4245 } )
4346
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' ) )
4649
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 )
4851 } )
4952
5053 it ( 'does not call onError when aborted' , async ( ) => {
5154 const controller = new AbortController ( )
5255 const topic = 'gossipabort'
5356 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 ( )
5959
6060 await ipfs . pubsub . subscribe ( topic , message => {
6161 messages . push ( message )
6262 if ( messages . length === 2 ) {
63- onReceived ( )
63+ onReceived . resolve ( )
6464 }
6565 } , {
66- onError,
66+ onError : onError . resolve ,
6767 signal : controller . signal
6868 } )
6969
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' ) )
7272
73- await received
73+ await onReceived . promise
7474 controller . abort ( )
7575
7676 // Stop the daemon
7777 await ctl . stop ( )
7878 // Just to make sure no error is caused by above line
79- setTimeout ( onError , 200 , 'aborted' )
79+ setTimeout ( onError . resolve , 200 , 'aborted' )
8080
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' )
8282 } )
8383 } )
8484} )
0 commit comments