Skip to content

Commit

Permalink
Extend tests for routing
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jul 2, 2024
1 parent 569f0a5 commit 55d04a4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
29 changes: 29 additions & 0 deletions pkg/routing/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,37 @@ import (
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"k8s.io/client-go/kubernetes/fake"
)

func TestKubernetesBootstra(t *testing.T) {
t.Parallel()

addr := "/ip4/10.244.1.2/tcp/5001"
peerID := "12D3KooWEkFzmb2PhhgxZv4ubV3aPfnTAhAmhmfTGuwvH4MHyPTV"
id := addr + "/p2p/" + peerID

cs := fake.NewSimpleClientset()
bs := NewKubernetesBootstrapper(cs, "spegel", "leader")

ctx, cancel := context.WithCancel(context.Background())
g, gCtx := errgroup.WithContext(ctx)
g.Go(func() error {
return bs.Run(gCtx, id)
})

peerInfo, err := bs.Get()
require.NoError(t, err)
require.Len(t, peerInfo.Addrs, 1)
require.Equal(t, addr, peerInfo.Addrs[0].String())
require.Equal(t, peerID, peerInfo.ID.String())

cancel()
err = g.Wait()
require.NoError(t, err)
}

func TestHTTPBootstrap(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion pkg/routing/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m *MemoryRouter) Resolve(ctx context.Context, key string, allowSelf bool,
m.mx.RLock()
peers, ok := m.resolver[key]
m.mx.RUnlock()
// If not peers exist close the channel to stop any consumer.
// If no peers exist close the channel to stop any consumer.
if !ok {
close(peerCh)
return peerCh, nil
Expand Down
15 changes: 15 additions & 0 deletions pkg/routing/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/netip"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -31,4 +32,18 @@ func TestMemoryRouter(t *testing.T) {
peers = append(peers, peer)
}
require.Len(t, peers, 2)
peers, ok := r.Lookup("foo")
require.True(t, ok)
require.Len(t, peers, 2)

peerCh, err = r.Resolve(ctx, "bar", false, 1)
require.NoError(t, err)
time.Sleep(1 * time.Second)
select {
case <-peerCh:
default:
t.Error("expected peer channel to be closed")
}
_, ok = r.Lookup("bar")
require.False(t, ok)
}
8 changes: 8 additions & 0 deletions pkg/routing/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,11 @@ func TestIsIp6(t *testing.T) {
require.NoError(t, err)
require.False(t, isIp6(m))
}

func TestCreateCid(t *testing.T) {
t.Parallel()

c, err := createCid("foobar")
require.NoError(t, err)
require.Equal(t, "bafkreigdvoh7cnza5cwzar65hfdgwpejotszfqx2ha6uuolaofgk54ge6i", c.String())
}

0 comments on commit 55d04a4

Please sign in to comment.