neoneo-go/pkg/network/server_test.go

93 lines
1.8 KiB
Go
Raw Normal View History

package network
import (
"os"
"testing"
2018-01-31 19:11:08 +00:00
2018-02-01 18:54:23 +00:00
"github.com/CityOfZion/neo-go/pkg/network/payload"
"github.com/CityOfZion/neo-go/pkg/util"
log "github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
)
func TestRegisterPeer(t *testing.T) {
s := newTestServer()
go s.run()
assert.NotZero(t, s.id)
assert.Zero(t, s.PeerCount())
lenPeers := 10
for i := 0; i < lenPeers; i++ {
s.register <- newTestPeer()
}
assert.Equal(t, lenPeers, s.PeerCount())
}
func TestUnregisterPeer(t *testing.T) {
s := newTestServer()
go s.run()
peer := newTestPeer()
s.register <- peer
s.register <- newTestPeer()
s.register <- newTestPeer()
assert.Equal(t, 3, s.PeerCount())
s.unregister <- peerDrop{peer, nil}
assert.Equal(t, 2, s.PeerCount())
}
type testNode struct{}
2018-01-31 19:11:08 +00:00
func (t testNode) version() *payload.Version {
return &payload.Version{}
}
2018-01-31 19:11:08 +00:00
func (t testNode) handleProto(msg *Message, p Peer) error {
return nil
}
func newTestServer() *Server {
return &Server{
logger: log.NewLogfmtLogger(os.Stderr),
id: util.RandUint32(1000000, 9999999),
quit: make(chan struct{}, 1),
register: make(chan Peer),
unregister: make(chan peerDrop),
badAddrOp: make(chan func(map[string]bool)),
badAddrOpDone: make(chan struct{}),
peerOp: make(chan func(map[Peer]bool)),
peerOpDone: make(chan struct{}),
proto: testNode{},
2018-01-31 19:11:08 +00:00
}
}
type testPeer struct {
done chan struct{}
2018-01-31 19:11:08 +00:00
}
func newTestPeer() testPeer {
return testPeer{
done: make(chan struct{}),
}
2018-01-31 19:11:08 +00:00
}
func (p testPeer) Version() *payload.Version {
return &payload.Version{}
2018-01-31 19:11:08 +00:00
}
func (p testPeer) Endpoint() util.Endpoint {
return util.Endpoint{}
}
func (p testPeer) Send(msg *Message) {}
func (p testPeer) Done() chan struct{} {
return p.done
}
func (p testPeer) Disconnect(err error) {
}