2018-01-31 08:27:08 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2018-03-09 15:55:25 +00:00
|
|
|
"os"
|
2018-01-31 08:27:08 +00:00
|
|
|
"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"
|
2018-03-09 15:55:25 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
log "github.com/go-kit/kit/log"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-31 08:27:08 +00:00
|
|
|
)
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func TestRegisterPeer(t *testing.T) {
|
|
|
|
s := newTestServer()
|
|
|
|
go s.run()
|
2018-02-04 19:54:51 +00:00
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
assert.NotZero(t, s.id)
|
|
|
|
assert.Zero(t, s.PeerCount())
|
2018-02-02 10:02:25 +00:00
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
lenPeers := 10
|
|
|
|
for i := 0; i < lenPeers; i++ {
|
|
|
|
s.register <- newTestPeer()
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
assert.Equal(t, lenPeers, s.PeerCount())
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func TestUnregisterPeer(t *testing.T) {
|
|
|
|
s := newTestServer()
|
|
|
|
go s.run()
|
2018-02-02 10:02:25 +00:00
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
peer := newTestPeer()
|
|
|
|
s.register <- peer
|
|
|
|
s.register <- newTestPeer()
|
|
|
|
s.register <- newTestPeer()
|
|
|
|
assert.Equal(t, 3, s.PeerCount())
|
2018-02-02 10:02:25 +00:00
|
|
|
|
2018-03-10 12:04:06 +00:00
|
|
|
s.unregister <- peerDrop{peer, nil}
|
2018-03-09 15:55:25 +00:00
|
|
|
assert.Equal(t, 2, s.PeerCount())
|
2018-02-02 10:02:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
type testNode struct{}
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func (t testNode) version() *payload.Version {
|
|
|
|
return &payload.Version{}
|
|
|
|
}
|
2018-01-31 19:11:08 +00:00
|
|
|
|
2018-03-10 12:04:06 +00:00
|
|
|
func (t testNode) handleProto(msg *Message, p Peer) error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-09 15:55:25 +00:00
|
|
|
|
|
|
|
func newTestServer() *Server {
|
|
|
|
return &Server{
|
|
|
|
logger: log.NewLogfmtLogger(os.Stderr),
|
|
|
|
id: util.RandUint32(1000000, 9999999),
|
|
|
|
quit: make(chan struct{}, 1),
|
|
|
|
register: make(chan Peer),
|
2018-03-10 12:04:06 +00:00
|
|
|
unregister: make(chan peerDrop),
|
2018-03-09 15:55:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
type testPeer struct {
|
|
|
|
done chan struct{}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func newTestPeer() testPeer {
|
|
|
|
return testPeer{
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 15:55:25 +00:00
|
|
|
func (p testPeer) Version() *payload.Version {
|
|
|
|
return &payload.Version{}
|
2018-01-31 19:11:08 +00:00
|
|
|
}
|
2018-03-09 15:55:25 +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
|
2018-01-31 08:27:08 +00:00
|
|
|
}
|
2018-03-10 12:04:06 +00:00
|
|
|
|
|
|
|
func (p testPeer) Disconnect(err error) {
|
|
|
|
|
|
|
|
}
|