@@ -11,7 +11,24 @@ const parallel = require('async/parallel')
11
11
const GODaemon = require ( '../utils/interop-daemon-spawner/go' )
12
12
const JSDaemon = require ( '../utils/interop-daemon-spawner/js' )
13
13
14
- describe ( 'pubsub' , ( ) => {
14
+ /*
15
+ * Wait for a condition to become true. When its true, callback is called.
16
+ */
17
+ function waitFor ( predicate , callback ) {
18
+ const ttl = Date . now ( ) + ( 2 * 1000 )
19
+ const self = setInterval ( ( ) => {
20
+ if ( predicate ( ) ) {
21
+ clearInterval ( self )
22
+ callback ( )
23
+ }
24
+ if ( Date . now ( ) > ttl ) {
25
+ clearInterval ( self )
26
+ callback ( new Error ( "waitFor time expired" ) )
27
+ }
28
+ } , 500 )
29
+ }
30
+
31
+ describe ( 'pubsub' , function ( ) {
15
32
let jsD
16
33
let goD
17
34
let jsId
@@ -34,47 +51,115 @@ describe('pubsub', () => {
34
51
} )
35
52
36
53
after ( ( done ) => {
37
- series ( [
54
+ parallel ( [
38
55
( cb ) => goD . stop ( cb ) ,
39
56
( cb ) => jsD . stop ( cb )
40
57
] , done )
41
58
} )
42
59
43
60
it ( 'make connections' , ( done ) => {
44
- parallel ( [
61
+ series ( [
45
62
( cb ) => jsD . api . id ( cb ) ,
46
63
( cb ) => goD . api . id ( cb )
47
64
] , ( err , ids ) => {
48
65
expect ( err ) . to . not . exist ( )
49
66
50
- jsId = ids [ 0 ] . ID
51
- goId = ids [ 0 ] . ID
67
+ jsId = ids [ 0 ] . id
68
+ goId = ids [ 1 ] . id
52
69
53
- console . log ( 'jsId:' , jsId )
54
- console . log ( 'goId:' , goId )
70
+ const jsLocalAddr = ids [ 0 ] . addresses . find ( a => a . includes ( '127.0.0.1' ) )
71
+ const goLocalAddr = ids [ 1 ] . addresses . find ( a => a . includes ( '127.0.0.1' ) )
55
72
56
73
parallel ( [
57
- ( cb ) => jsD . api . swarm . connect ( ids [ 1 ] . addresses [ 0 ] , cb ) ,
58
- ( cb ) => goD . api . swarm . connect ( ids [ 0 ] . addresses [ 0 ] , cb )
74
+ ( cb ) => jsD . api . swarm . connect ( goLocalAddr , cb ) ,
75
+ ( cb ) => goD . api . swarm . connect ( jsLocalAddr , cb )
59
76
] , done )
60
77
} )
61
78
} )
62
79
63
- it . skip ( 'publish from JS, subscribe on Go' , ( done ) => {
64
- // TODO write this test
80
+ it ( 'publish from Go, subscribe on Go' , ( done ) => {
81
+ const topic = 'pubsub-go-go'
82
+ const data = Buffer . from ( 'hello world' )
83
+ let n = 0
84
+
85
+ function checkMessage ( msg ) {
86
+ ++ n
87
+ expect ( msg . data . toString ( ) ) . to . equal ( data . toString ( ) )
88
+ expect ( msg ) . to . have . property ( 'seqno' )
89
+ expect ( Buffer . isBuffer ( msg . seqno ) ) . to . be . eql ( true )
90
+ // TODO: expect(msg).to.have.property('topicIDs').eql([topic])
91
+ expect ( msg ) . to . have . property ( 'from' , goId )
92
+ }
93
+
94
+ series ( [
95
+ ( cb ) => goD . api . pubsub . subscribe ( topic , checkMessage , cb ) ,
96
+ ( cb ) => goD . api . pubsub . publish ( topic , data , cb ) ,
97
+ ( cb ) => waitFor ( ( ) => n === 1 , cb )
98
+ ] , done )
99
+ } )
100
+
101
+ it ( 'publish from JS, subscribe on JS' , ( done ) => {
102
+ const topic = 'pubsub-js-js'
103
+ const data = Buffer . from ( 'hello world' )
104
+ let n = 0
105
+
106
+ function checkMessage ( msg ) {
107
+ ++ n
108
+ expect ( msg . data . toString ( ) ) . to . equal ( data . toString ( ) )
109
+ expect ( msg ) . to . have . property ( 'seqno' )
110
+ expect ( Buffer . isBuffer ( msg . seqno ) ) . to . be . eql ( true )
111
+ expect ( msg ) . to . have . property ( 'topicIDs' ) . eql ( [ topic ] )
112
+ expect ( msg ) . to . have . property ( 'from' , jsId )
113
+ }
114
+
115
+ series ( [
116
+ ( cb ) => jsD . api . pubsub . subscribe ( topic , checkMessage , cb ) ,
117
+ ( cb ) => jsD . api . pubsub . publish ( topic , data , cb ) ,
118
+ ( cb ) => waitFor ( ( ) => n === 1 , cb )
119
+ ] , done )
120
+ } )
121
+
122
+ it ( 'publish from JS, subscribe on Go' , ( done ) => {
123
+ const topic = 'pubsub-js-go'
124
+ const data = Buffer . from ( 'hello world' )
125
+ let n = 0
126
+
127
+ function checkMessage ( msg ) {
128
+ console . log ( 'check message' , msg )
129
+ ++ n
130
+ expect ( msg . data . toString ( ) ) . to . equal ( data . toString ( ) )
131
+ expect ( msg ) . to . have . property ( 'seqno' )
132
+ expect ( Buffer . isBuffer ( msg . seqno ) ) . to . be . eql ( true )
133
+ expect ( msg ) . to . have . property ( 'topicIDs' ) . eql ( [ topic ] )
134
+ expect ( msg ) . to . have . property ( 'from' , jsId )
135
+ }
136
+
137
+ series ( [
138
+ ( cb ) => goD . api . pubsub . subscribe ( topic , checkMessage , cb ) ,
139
+ ( cb ) => jsD . api . pubsub . publish ( topic , data , cb ) ,
140
+ ( cb ) => waitFor ( ( ) => n === 1 , cb )
141
+ ] , done )
65
142
} )
66
143
67
- it . skip ( 'publish from Go, subscribe on JS' , ( done ) => {
144
+ it ( 'publish from Go, subscribe on JS' , ( done ) => {
68
145
const topic = 'pubsub-go-js'
69
146
const data = Buffer . from ( 'hello world' )
147
+ let n = 0
70
148
71
- function checkMessage ( ) {
72
- console . log ( 'check message' , arguments )
149
+ function checkMessage ( msg ) {
150
+ console . log ( 'check message' , msg )
151
+ ++ n
152
+ expect ( msg . data . toString ( ) ) . to . equal ( data . toString ( ) )
153
+ expect ( msg ) . to . have . property ( 'seqno' )
154
+ expect ( Buffer . isBuffer ( msg . seqno ) ) . to . be . eql ( true )
155
+ expect ( msg ) . to . have . property ( 'topicIDs' ) . eql ( [ topic ] )
156
+ expect ( msg ) . to . have . property ( 'from' , goId )
73
157
}
74
158
75
159
series ( [
76
- cb => jsD . api . pubsub . subscribe ( topic , checkMessage , cb ) ,
77
- cb => goD . api . pubsub . publish ( topic , data , cb )
160
+ ( cb ) => jsD . api . pubsub . subscribe ( topic , checkMessage , cb ) ,
161
+ ( cb ) => goD . api . pubsub . publish ( topic , data , cb ) ,
162
+ ( cb ) => waitFor ( ( ) => n === 1 , cb ) ,
78
163
] , done )
79
164
} )
80
165
} )
0 commit comments