@@ -12,9 +12,10 @@ var test = require('tape')
1212var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
1313var peerId = Buffer . from ( '01234567890123456789' )
1414var peerId2 = Buffer . from ( '12345678901234567890' )
15+ var peerId3 = Buffer . from ( '23456789012345678901' )
1516
1617function serverTest ( t , serverType , serverFamily ) {
17- t . plan ( 30 )
18+ t . plan ( 32 )
1819
1920 var hostname = serverFamily === 'inet6'
2021 ? '[::1]'
@@ -23,7 +24,12 @@ function serverTest (t, serverType, serverFamily) {
2324 ? '::1'
2425 : '127.0.0.1'
2526
26- common . createServer ( t , serverType , function ( server ) {
27+ var opts = {
28+ serverType : serverType ,
29+ peersCacheLength : 2
30+ }
31+
32+ common . createServer ( t , opts , function ( server ) {
2733 var port = server [ serverType ] . address ( ) . port
2834 var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
2935
@@ -52,22 +58,23 @@ function serverTest (t, serverType, serverFamily) {
5258 t . equal ( Object . keys ( server . torrents ) . length , 1 )
5359 t . equal ( swarm . complete , 0 )
5460 t . equal ( swarm . incomplete , 1 )
55- t . equal ( Object . keys ( swarm . peers ) . length , 1 )
61+ t . equal ( swarm . peers . length , 1 )
5662
5763 var id = serverType === 'ws'
5864 ? peerId . toString ( 'hex' )
5965 : hostname + ':6881'
6066
61- t . equal ( swarm . peers [ id ] . type , serverType )
62- t . equal ( swarm . peers [ id ] . ip , clientIp )
63- t . equal ( swarm . peers [ id ] . peerId , peerId . toString ( 'hex' ) )
64- t . equal ( swarm . peers [ id ] . complete , false )
67+ var peer = swarm . peers . peek ( id )
68+ t . equal ( peer . type , serverType )
69+ t . equal ( peer . ip , clientIp )
70+ t . equal ( peer . peerId , peerId . toString ( 'hex' ) )
71+ t . equal ( peer . complete , false )
6572 if ( serverType === 'ws' ) {
66- t . equal ( typeof swarm . peers [ id ] . port , 'number' )
67- t . ok ( swarm . peers [ id ] . socket )
73+ t . equal ( typeof peer . port , 'number' )
74+ t . ok ( peer . socket )
6875 } else {
69- t . equal ( swarm . peers [ id ] . port , 6881 )
70- t . notOk ( swarm . peers [ id ] . socket )
76+ t . equal ( peer . port , 6881 )
77+ t . notOk ( peer . socket )
7178 }
7279
7380 client1 . complete ( )
@@ -102,22 +109,42 @@ function serverTest (t, serverType, serverFamily) {
102109 client2 . once ( 'peer' , function ( addr ) {
103110 t . ok ( addr === hostname + ':6881' || addr === hostname + ':6882' || addr . id === peerId . toString ( 'hex' ) )
104111
105- client2 . stop ( )
106- client2 . once ( 'update' , function ( data ) {
107- t . equal ( data . announce , announceUrl )
108- t . equal ( data . complete , 1 )
109- t . equal ( data . incomplete , 0 )
110- client2 . destroy ( )
112+ swarm . peers . once ( 'evict' , function ( evicted ) {
113+ t . equals ( evicted . value . peerId , peerId . toString ( 'hex' ) )
114+ } )
115+ var client3 = new Client ( {
116+ infoHash : infoHash ,
117+ announce : [ announceUrl ] ,
118+ peerId : peerId3 ,
119+ port : 6880
120+ // wrtc: wrtc
121+ } )
122+ client3 . start ( )
123+
124+ server . once ( 'start' , function ( ) {
125+ t . pass ( 'got start message from client3' )
126+ } )
111127
112- client1 . stop ( )
113- client1 . once ( 'update' , function ( data ) {
128+ client3 . once ( 'update' , function ( ) {
129+ client2 . stop ( )
130+ client2 . once ( 'update' , function ( data ) {
114131 t . equal ( data . announce , announceUrl )
115- t . equal ( data . complete , 0 )
116- t . equal ( data . incomplete , 0 )
117-
118- client1 . destroy ( function ( ) {
119- server . close ( )
120- // if (serverType === 'ws') wrtc.close()
132+ t . equal ( data . complete , 1 )
133+ 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 )
141+
142+ client3 . destroy ( function ( ) {
143+ client1 . destroy ( function ( ) {
144+ server . close ( )
145+ } )
146+ // if (serverType === 'ws') wrtc.close()
147+ } )
121148 } )
122149 } )
123150 } )
0 commit comments