@@ -2,20 +2,20 @@ var Buffer = require('safe-buffer').Buffer
22var Client = require ( '../' )
33var common = require ( './common' )
44var test = require ( 'tape' )
5- // var wrtc = require('electron-webrtc')()
5+ var wrtc = require ( 'electron-webrtc' ) ( )
66
7- // var wrtcReady = false
8- // wrtc.electronDaemon.once('ready', function () {
9- // wrtcReady = true
10- // })
7+ var wrtcReady = false
8+ wrtc . electronDaemon . once ( 'ready' , function ( ) {
9+ wrtcReady = true
10+ } )
1111
1212var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
1313var peerId = Buffer . from ( '01234567890123456789' )
1414var peerId2 = Buffer . from ( '12345678901234567890' )
1515var peerId3 = Buffer . from ( '23456789012345678901' )
1616
1717function serverTest ( t , serverType , serverFamily ) {
18- t . plan ( 32 )
18+ t . plan ( 36 )
1919
2020 var hostname = serverFamily === 'inet6'
2121 ? '[::1]'
@@ -30,16 +30,19 @@ function serverTest (t, serverType, serverFamily) {
3030 }
3131
3232 common . createServer ( t , opts , function ( server ) {
33+ // Not using announceUrl param from `common.createServer()` since we
34+ // want to control IPv4 vs IPv6.
3335 var port = server [ serverType ] . address ( ) . port
3436 var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
3537
3638 var client1 = new Client ( {
3739 infoHash : infoHash ,
3840 announce : [ announceUrl ] ,
3941 peerId : peerId ,
40- port : 6881
41- // wrtc: wrtc
42+ port : 6881 ,
43+ wrtc : wrtc
4244 } )
45+ if ( serverType === 'ws' ) common . mockWebsocketTracker ( client1 )
4346
4447 client1 . start ( )
4548
@@ -88,62 +91,68 @@ function serverTest (t, serverType, serverFamily) {
8891
8992 client1 . once ( 'scrape' , function ( data ) {
9093 t . equal ( data . announce , announceUrl )
91- t . equal ( typeof data . complete , 'number' )
92- t . equal ( typeof data . incomplete , 'number' )
94+ t . equal ( data . complete , 1 )
95+ t . equal ( data . incomplete , 0 )
9396 t . equal ( typeof data . downloaded , 'number' )
9497
9598 var client2 = new Client ( {
9699 infoHash : infoHash ,
97100 announce : [ announceUrl ] ,
98101 peerId : peerId2 ,
99- port : 6882
100- // wrtc: wrtc
102+ port : 6882 ,
103+ wrtc : wrtc
101104 } )
105+ if ( serverType === 'ws' ) common . mockWebsocketTracker ( client2 )
102106
103107 client2 . start ( )
104108
105109 server . once ( 'start' , function ( ) {
106110 t . pass ( 'got start message from client2' )
107111 } )
108112
109- client2 . once ( 'peer' , function ( addr ) {
110- t . ok ( addr === hostname + ':6881' || addr === hostname + ':6882' || addr . id === peerId . toString ( 'hex' ) )
113+ client2 . once ( 'update' , function ( data ) {
114+ t . equal ( data . announce , announceUrl )
115+ t . equal ( data . complete , 1 )
116+ t . equal ( data . incomplete , 1 )
111117
112- swarm . peers . once ( 'evict' , function ( evicted ) {
113- t . equals ( evicted . value . peerId , peerId . toString ( 'hex' ) )
114- } )
115118 var client3 = new Client ( {
116119 infoHash : infoHash ,
117120 announce : [ announceUrl ] ,
118121 peerId : peerId3 ,
119- port : 6880
120- // wrtc: wrtc
122+ port : 6880 ,
123+ wrtc : wrtc
121124 } )
125+ if ( serverType === 'ws' ) common . mockWebsocketTracker ( client3 )
126+
122127 client3 . start ( )
123128
124129 server . once ( 'start' , function ( ) {
125130 t . pass ( 'got start message from client3' )
126131 } )
127132
128- client3 . once ( 'update' , function ( ) {
133+ client3 . once ( 'update' , function ( data ) {
134+ t . equal ( data . announce , announceUrl )
135+ t . equal ( data . complete , 1 )
136+ t . equal ( data . incomplete , 2 )
137+
129138 client2 . stop ( )
130139 client2 . once ( 'update' , function ( data ) {
131140 t . equal ( data . announce , announceUrl )
132141 t . equal ( data . complete , 1 )
133142 t . equal ( data . incomplete , 1 )
134- client2 . destroy ( )
135-
136- client3 . stop ( )
137- client3 . once ( 'update' , function ( data ) {
138- t . equal ( data . announce , announceUrl )
139- t . equal ( data . complete , 1 )
140- t . equal ( data . incomplete , 0 )
141143
142- client3 . destroy ( function ( ) {
143- client1 . destroy ( function ( ) {
144- server . close ( )
144+ client2 . destroy ( function ( ) {
145+ client3 . stop ( )
146+ client3 . once ( 'update' , function ( data ) {
147+ t . equal ( data . announce , announceUrl )
148+ t . equal ( data . complete , 1 )
149+ t . equal ( data . incomplete , 0 )
150+
151+ client3 . destroy ( function ( ) {
152+ client1 . destroy ( function ( ) {
153+ server . close ( )
154+ } )
145155 } )
146- // if (serverType === 'ws') wrtc.close()
147156 } )
148157 } )
149158 } )
@@ -156,17 +165,6 @@ function serverTest (t, serverType, serverFamily) {
156165 } )
157166}
158167
159- // test('websocket server', function (t) {
160- // if (wrtcReady) {
161- // runTest()
162- // } else {
163- // wrtc.electronDaemon.once('ready', runTest)
164- // }
165- // function runTest () {
166- // serverTest(t, 'ws', 'inet')
167- // }
168- // })
169-
170168test ( 'http ipv4 server' , function ( t ) {
171169 serverTest ( t , 'http' , 'inet' )
172170} )
@@ -178,3 +176,17 @@ test('http ipv6 server', function (t) {
178176test ( 'udp server' , function ( t ) {
179177 serverTest ( t , 'udp' , 'inet' )
180178} )
179+
180+ test ( 'ws server' , function ( t ) {
181+ if ( wrtcReady ) {
182+ runTest ( )
183+ } else {
184+ wrtc . electronDaemon . once ( 'ready' , runTest )
185+ }
186+ function runTest ( ) {
187+ t . once ( 'end' , function ( ) {
188+ wrtc . close ( )
189+ } )
190+ serverTest ( t , 'ws' , 'inet' )
191+ }
192+ } )
0 commit comments