Skip to content

Commit

Permalink
test: add rudimentary netsize test
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Dec 9, 2022
1 parent 2b17012 commit 6138b9e
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions netsize/netsize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package netsize

import (
"testing"
"time"

kbucket "github.com/libp2p/go-libp2p-kbucket"
pt "github.com/libp2p/go-libp2p/core/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ks "github.com/whyrusleeping/go-keyspace"
)

func TestNewEstimator(t *testing.T) {
bucketSize := 20

pid, err := pt.RandPeerID()
require.NoError(t, err)

rt, err := kbucket.NewRoutingTable(bucketSize, kbucket.ConvertPeerID(pid), time.Second, nil, time.Second, nil)
require.NoError(t, err)

e := NewEstimator(pid, rt, bucketSize)

assert.Equal(t, rt, e.rt)
assert.Equal(t, kbucket.ConvertPeerID(pid), e.localID)
assert.Len(t, e.measurements, bucketSize)
assert.Nil(t, e.netSizeCache)
}

func TestNormedDistance(t *testing.T) {
pid, err := pt.RandPeerID()
require.NoError(t, err)

dist := NormedDistance(pid, ks.XORKeySpace.Key([]byte(pid)))
assert.Zero(t, dist)

pid2, err := pt.RandPeerID()
require.NoError(t, err)

dist = NormedDistance(pid, ks.XORKeySpace.Key([]byte(pid2)))
assert.Greater(t, 1.0, dist)
assert.Less(t, dist, 1.0)
}

0 comments on commit 6138b9e

Please sign in to comment.