github.com/celestiaorg/celestia-node@v0.15.0-beta.1/share/p2p/discovery/backoff_test.go (about)

     1  package discovery
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/libp2p/go-libp2p/core/host"
     9  	"github.com/libp2p/go-libp2p/p2p/discovery/backoff"
    10  	mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestBackoff_ConnectPeer(t *testing.T) {
    15  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    16  	t.Cleanup(cancel)
    17  	m, err := mocknet.FullMeshLinked(2)
    18  	require.NoError(t, err)
    19  	b := newBackoffConnector(m.Hosts()[0], backoff.NewFixedBackoff(time.Minute))
    20  	info := host.InfoFromHost(m.Hosts()[1])
    21  	require.NoError(t, b.Connect(ctx, *info))
    22  }
    23  
    24  func TestBackoff_ConnectPeerFails(t *testing.T) {
    25  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    26  	t.Cleanup(cancel)
    27  	m, err := mocknet.FullMeshLinked(2)
    28  	require.NoError(t, err)
    29  	b := newBackoffConnector(m.Hosts()[0], backoff.NewFixedBackoff(time.Minute))
    30  	info := host.InfoFromHost(m.Hosts()[1])
    31  	require.NoError(t, b.Connect(ctx, *info))
    32  
    33  	require.Error(t, b.Connect(ctx, *info))
    34  }
    35  
    36  func TestBackoff_ResetBackoffPeriod(t *testing.T) {
    37  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
    38  	t.Cleanup(cancel)
    39  	m, err := mocknet.FullMeshLinked(2)
    40  	require.NoError(t, err)
    41  	b := newBackoffConnector(m.Hosts()[0], backoff.NewFixedBackoff(time.Minute))
    42  	info := host.InfoFromHost(m.Hosts()[1])
    43  	require.NoError(t, b.Connect(ctx, *info))
    44  	nexttry := b.cacheData[info.ID].nexttry
    45  	b.Backoff(info.ID)
    46  	require.True(t, b.cacheData[info.ID].nexttry.After(nexttry))
    47  }