github.com/decred/dcrlnd@v0.7.6/channeldb/peers_test.go (about)

     1  package channeldb
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/decred/dcrlnd/routing/route"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // TestFlapCount tests lookup and writing of flap count to disk.
    12  func TestFlapCount(t *testing.T) {
    13  	db, cleanup, err := MakeTestDB()
    14  	require.NoError(t, err)
    15  	defer cleanup()
    16  
    17  	// Try to read flap count for a peer that we have no records for.
    18  	_, err = db.ReadFlapCount(testPub)
    19  	require.Equal(t, ErrNoPeerBucket, err)
    20  
    21  	var (
    22  		testPub2       = route.Vertex{2, 2, 2}
    23  		peer1FlapCount = &FlapCount{
    24  			Count:    20,
    25  			LastFlap: time.Unix(100, 23),
    26  		}
    27  		peer2FlapCount = &FlapCount{
    28  			Count:    39,
    29  			LastFlap: time.Unix(200, 23),
    30  		}
    31  	)
    32  
    33  	peers := map[route.Vertex]*FlapCount{
    34  		testPub:  peer1FlapCount,
    35  		testPub2: peer2FlapCount,
    36  	}
    37  
    38  	err = db.WriteFlapCounts(peers)
    39  	require.NoError(t, err)
    40  
    41  	// Lookup flap count for our first pubkey.
    42  	count, err := db.ReadFlapCount(testPub)
    43  	require.NoError(t, err)
    44  	require.Equal(t, peer1FlapCount, count)
    45  
    46  	// Lookup our flap count for the second peer.
    47  	count, err = db.ReadFlapCount(testPub2)
    48  	require.NoError(t, err)
    49  	require.Equal(t, peer2FlapCount, count)
    50  }