1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lightnode_test
import (
"context"
"reflect"
"testing"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/topology/lightnode"
)
func TestContainer(t *testing.T) {
t.Run("new container is empty container", func(t *testing.T) {
c := lightnode.NewContainer()
var empty topology.BinInfo
if !reflect.DeepEqual(empty, c.PeerInfo()) {
t.Errorf("expected %v, got %v", empty, c.PeerInfo())
}
})
t.Run("can add peers to container", func(t *testing.T) {
c := lightnode.NewContainer()
c.Connected(context.Background(), p2p.Peer{Address: swarm.NewAddress([]byte("123"))})
c.Connected(context.Background(), p2p.Peer{Address: swarm.NewAddress([]byte("456"))})
peerCount := len(c.PeerInfo().ConnectedPeers)
if peerCount != 2 {
t.Errorf("expected %d connected peer, got %d", 2, peerCount)
}
})
t.Run("empty container after peer disconnect", func(t *testing.T) {
c := lightnode.NewContainer()
peer := p2p.Peer{Address: swarm.NewAddress([]byte("123"))}
c.Connected(context.Background(), peer)
c.Disconnected(peer)
discPeerCount := len(c.PeerInfo().DisconnectedPeers)
if discPeerCount != 1 {
t.Errorf("expected %d connected peer, got %d", 1, discPeerCount)
}
connPeerCount := len(c.PeerInfo().ConnectedPeers)
if connPeerCount != 0 {
t.Errorf("expected %d connected peer, got %d", 0, connPeerCount)
}
})
}