github.com/ethersphere/bee/v2@v2.2.0/pkg/topology/kademlia/internal/waitnext/waitnext_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package metrics provides service for collecting various metrics about peers.
     6  // It is intended to be used with the kademlia where the metrics are collected.
     7  package waitnext_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/swarm"
    14  	"github.com/ethersphere/bee/v2/pkg/topology/kademlia/internal/waitnext"
    15  )
    16  
    17  func TestSet(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	waitNext := waitnext.New()
    21  
    22  	addr := swarm.RandAddress(t)
    23  
    24  	waitNext.Set(addr, time.Now().Add(time.Millisecond*10), 2)
    25  
    26  	if !waitNext.Waiting(addr) {
    27  		t.Fatal("should be waiting")
    28  	}
    29  
    30  	time.Sleep(time.Millisecond * 11)
    31  
    32  	if waitNext.Waiting(addr) {
    33  		t.Fatal("should not be waiting")
    34  	}
    35  
    36  	if attempts := waitNext.Attempts(addr); attempts != 2 {
    37  		t.Fatalf("want 2, got %d", attempts)
    38  	}
    39  
    40  	waitNext.SetTryAfter(addr, time.Now().Add(time.Millisecond*10))
    41  
    42  	if !waitNext.Waiting(addr) {
    43  		t.Fatal("should be waiting")
    44  	}
    45  
    46  	time.Sleep(time.Millisecond * 11)
    47  
    48  	if waitNext.Waiting(addr) {
    49  		t.Fatal("should not be waiting")
    50  	}
    51  
    52  	if attempts := waitNext.Attempts(addr); attempts != 2 {
    53  		t.Fatalf("want 2, got %d", attempts)
    54  	}
    55  }