@@ -162,9 +162,33 @@ var nodeDBSeedQueryNodes = []struct {
162162 node * Node
163163 pong time.Time
164164}{
165+ // This one should not be in the result set because its last
166+ // pong time is too far in the past.
165167 {
166168 node : newNode (
167- MustHexID ("0x01d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439" ),
169+ MustHexID ("0x84d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439" ),
170+ net.IP {127 , 0 , 0 , 3 },
171+ 30303 ,
172+ 30303 ,
173+ ),
174+ pong : time .Now ().Add (- 3 * time .Hour ),
175+ },
176+ // This one shouldn't be in in the result set because its
177+ // nodeID is the local node's ID.
178+ {
179+ node : newNode (
180+ MustHexID ("0x57d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439" ),
181+ net.IP {127 , 0 , 0 , 3 },
182+ 30303 ,
183+ 30303 ,
184+ ),
185+ pong : time .Now ().Add (- 4 * time .Second ),
186+ },
187+
188+ // These should be in the result set.
189+ {
190+ node : newNode (
191+ MustHexID ("0x22d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439" ),
168192 net.IP {127 , 0 , 0 , 1 },
169193 30303 ,
170194 30303 ,
@@ -173,7 +197,7 @@ var nodeDBSeedQueryNodes = []struct {
173197 },
174198 {
175199 node : newNode (
176- MustHexID ("0x02d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439 " ),
200+ MustHexID ("0x44d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439 " ),
177201 net.IP {127 , 0 , 0 , 2 },
178202 30303 ,
179203 30303 ,
@@ -182,7 +206,7 @@ var nodeDBSeedQueryNodes = []struct {
182206 },
183207 {
184208 node : newNode (
185- MustHexID ("0x03d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439 " ),
209+ MustHexID ("0xe2d9d65c4552b5eb43d5ad55a2ee3f56c6cbc1c64a5c8d659f51fcd51bace24351232b8d7821617d2b29b54b81cdefb9b3e9c37d7fd5f63270bcc9e1a6f6a439 " ),
186210 net.IP {127 , 0 , 0 , 3 },
187211 30303 ,
188212 30303 ,
@@ -192,28 +216,32 @@ var nodeDBSeedQueryNodes = []struct {
192216}
193217
194218func TestNodeDBSeedQuery (t * testing.T ) {
195- db , _ := newNodeDB ("" , Version , NodeID {} )
219+ db , _ := newNodeDB ("" , Version , nodeDBSeedQueryNodes [ 1 ]. node . ID )
196220 defer db .close ()
197221
198222 // Insert a batch of nodes for querying
199223 for i , seed := range nodeDBSeedQueryNodes {
200224 if err := db .updateNode (seed .node ); err != nil {
201225 t .Fatalf ("node %d: failed to insert: %v" , i , err )
202226 }
227+ if err := db .updateLastPong (seed .node .ID , seed .pong ); err != nil {
228+ t .Fatalf ("node %d: failed to insert lastPong: %v" , i , err )
229+ }
203230 }
231+
204232 // Retrieve the entire batch and check for duplicates
205- seeds := db .querySeeds (2 * len (nodeDBSeedQueryNodes ))
206- if len (seeds ) != len (nodeDBSeedQueryNodes ) {
207- t .Errorf ("seed count mismatch: have %v, want %v" , len (seeds ), len (nodeDBSeedQueryNodes ))
208- }
233+ seeds := db .querySeeds (len (nodeDBSeedQueryNodes )* 2 , time .Hour )
209234 have := make (map [NodeID ]struct {})
210235 for _ , seed := range seeds {
211236 have [seed .ID ] = struct {}{}
212237 }
213238 want := make (map [NodeID ]struct {})
214- for _ , seed := range nodeDBSeedQueryNodes {
239+ for _ , seed := range nodeDBSeedQueryNodes [ 2 :] {
215240 want [seed .node .ID ] = struct {}{}
216241 }
242+ if len (seeds ) != len (want ) {
243+ t .Errorf ("seed count mismatch: have %v, want %v" , len (seeds ), len (want ))
244+ }
217245 for id , _ := range have {
218246 if _ , ok := want [id ]; ! ok {
219247 t .Errorf ("extra seed: %v" , id )
@@ -224,63 +252,6 @@ func TestNodeDBSeedQuery(t *testing.T) {
224252 t .Errorf ("missing seed: %v" , id )
225253 }
226254 }
227- // Make sure the next batch is empty (seed EOF)
228- seeds = db .querySeeds (2 * len (nodeDBSeedQueryNodes ))
229- if len (seeds ) != 0 {
230- t .Errorf ("seed count mismatch: have %v, want %v" , len (seeds ), 0 )
231- }
232- }
233-
234- func TestNodeDBSeedQueryContinuation (t * testing.T ) {
235- db , _ := newNodeDB ("" , Version , NodeID {})
236- defer db .close ()
237-
238- // Insert a batch of nodes for querying
239- for i , seed := range nodeDBSeedQueryNodes {
240- if err := db .updateNode (seed .node ); err != nil {
241- t .Fatalf ("node %d: failed to insert: %v" , i , err )
242- }
243- }
244- // Iteratively retrieve the batch, checking for an empty batch on reset
245- for i := 0 ; i < len (nodeDBSeedQueryNodes ); i ++ {
246- if seeds := db .querySeeds (1 ); len (seeds ) != 1 {
247- t .Errorf ("1st iteration %d: seed count mismatch: have %v, want %v" , i , len (seeds ), 1 )
248- }
249- }
250- if seeds := db .querySeeds (1 ); len (seeds ) != 0 {
251- t .Errorf ("reset: seed count mismatch: have %v, want %v" , len (seeds ), 0 )
252- }
253- for i := 0 ; i < len (nodeDBSeedQueryNodes ); i ++ {
254- if seeds := db .querySeeds (1 ); len (seeds ) != 1 {
255- t .Errorf ("2nd iteration %d: seed count mismatch: have %v, want %v" , i , len (seeds ), 1 )
256- }
257- }
258- }
259-
260- func TestNodeDBSelfSeedQuery (t * testing.T ) {
261- // Assign a node as self to verify evacuation
262- self := nodeDBSeedQueryNodes [0 ].node .ID
263- db , _ := newNodeDB ("" , Version , self )
264- defer db .close ()
265-
266- // Insert a batch of nodes for querying
267- for i , seed := range nodeDBSeedQueryNodes {
268- if err := db .updateNode (seed .node ); err != nil {
269- t .Fatalf ("node %d: failed to insert: %v" , i , err )
270- }
271- }
272- // Retrieve the entire batch and check that self was evacuated
273- seeds := db .querySeeds (2 * len (nodeDBSeedQueryNodes ))
274- if len (seeds ) != len (nodeDBSeedQueryNodes )- 1 {
275- t .Errorf ("seed count mismatch: have %v, want %v" , len (seeds ), len (nodeDBSeedQueryNodes )- 1 )
276- }
277- have := make (map [NodeID ]struct {})
278- for _ , seed := range seeds {
279- have [seed .ID ] = struct {}{}
280- }
281- if _ , ok := have [self ]; ok {
282- t .Errorf ("self not evacuated" )
283- }
284255}
285256
286257func TestNodeDBPersistency (t * testing.T ) {
0 commit comments