swarm/pss: test panic fix, client cancel rm

This commit is contained in:
lash 2017-06-22 12:30:27 +02:00
parent 10adb6e917
commit e94b1e6bfa
3 changed files with 16 additions and 23 deletions

View file

@ -92,13 +92,13 @@ func (rw *pssRPCRW) WriteMsg(msg p2p.Msg) error {
} }
func NewClient(ctx context.Context, cancel func(), rpcurl string) (*Client, error) { func NewClient(ctx context.Context, rpcurl string) (*Client, error) {
rpcclient, err := rpc.Dial(rpcurl) rpcclient, err := rpc.Dial(rpcurl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
client, err := NewClientWithRPC(ctx, cancel, rpcclient) client, err := NewClientWithRPC(ctx, rpcclient)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -107,8 +107,8 @@ func NewClient(ctx context.Context, cancel func(), rpcurl string) (*Client, erro
// Constructor for test implementations // Constructor for test implementations
// The 'rpcclient' parameter allows passing a in-memory rpc client to act as the remote websocket RPC. // The 'rpcclient' parameter allows passing a in-memory rpc client to act as the remote websocket RPC.
func NewClientWithRPC(ctx context.Context, cancel func(), rpcclient *rpc.Client) (*Client, error) { func NewClientWithRPC(ctx context.Context, rpcclient *rpc.Client) (*Client, error) {
client := newClient(ctx, cancel) client := newClient(ctx)
client.rpc = rpcclient client.rpc = rpcclient
err := client.rpc.CallContext(client.ctx, &client.BaseAddr, "pss_baseAddr") err := client.rpc.CallContext(client.ctx, &client.BaseAddr, "pss_baseAddr")
if err != nil { if err != nil {
@ -117,28 +117,20 @@ func NewClientWithRPC(ctx context.Context, cancel func(), rpcclient *rpc.Client)
return client, nil return client, nil
} }
func newClient(ctx context.Context, cancel func()) (client *Client) { func newClient(ctx context.Context) (client *Client) {
if ctx == nil { if ctx == nil {
ctx = context.Background() ctx = context.Background()
} }
if cancel == nil {
cancel = func() {}
}
client = &Client{ client = &Client{
msgC: make(chan pss.APIMsg), msgC: make(chan pss.APIMsg),
quitC: make(chan struct{}), quitC: make(chan struct{}),
peerPool: make(map[pss.Topic]map[pot.Address]*pssRPCRW), peerPool: make(map[pss.Topic]map[pot.Address]*pssRPCRW),
protos: make(map[pss.Topic]*p2p.Protocol), protos: make(map[pss.Topic]*p2p.Protocol),
ctx: ctx, ctx: ctx,
cancel: cancel,
} }
return return
} }
func (self *Client) shutdown() {
self.cancel()
}
// Mounts a new devp2p protcool on the pss connection // Mounts a new devp2p protcool on the pss connection
// the protocol is aliased as a "pss topic" // the protocol is aliased as a "pss topic"
// uses normal devp2p Send and incoming message handler routines from the p2p/protocols package // uses normal devp2p Send and incoming message handler routines from the p2p/protocols package
@ -171,7 +163,6 @@ func (self *Client) RunProtocol(proto *p2p.Protocol) error {
self.peerPool[topic][addr].msgC <- msg.Msg self.peerPool[topic][addr].msgC <- msg.Msg
}() }()
case <-self.quitC: case <-self.quitC:
self.shutdown()
return return
} }
} }

View file

@ -30,7 +30,7 @@ func TestRunProtocol(t *testing.T) {
C: make(chan struct{}), C: make(chan struct{}),
} }
proto := newProtocol(ping) proto := newProtocol(ping)
_, err := baseTester(t, proto, ps, nil, nil, quitC) _, err := baseTester(t, proto, ps, nil, quitC)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
@ -40,13 +40,13 @@ func TestRunProtocol(t *testing.T) {
func TestIncoming(t *testing.T) { func TestIncoming(t *testing.T) {
quitC := make(chan struct{}) quitC := make(chan struct{})
ps := pss.NewTestPss(nil) ps := pss.NewTestPss(nil)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
var addr []byte var addr []byte
ping := &pss.Ping{ ping := &pss.Ping{
C: make(chan struct{}), C: make(chan struct{}),
} }
proto := newProtocol(ping) proto := newProtocol(ping)
client, err := baseTester(t, proto, ps, ctx, cancel, quitC) client, err := baseTester(t, proto, ps, ctx, quitC)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
@ -81,7 +81,7 @@ func TestIncoming(t *testing.T) {
func TestOutgoing(t *testing.T) { func TestOutgoing(t *testing.T) {
quitC := make(chan struct{}) quitC := make(chan struct{})
ps := pss.NewTestPss(nil) ps := pss.NewTestPss(nil)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*250) ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*250)
var addr []byte var addr []byte
var potaddr pot.Address var potaddr pot.Address
@ -89,7 +89,7 @@ func TestOutgoing(t *testing.T) {
C: make(chan struct{}), C: make(chan struct{}),
} }
proto := newProtocol(ping) proto := newProtocol(ping)
client, err := baseTester(t, proto, ps, ctx, cancel, quitC) client, err := baseTester(t, proto, ps, ctx, quitC)
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }
@ -115,10 +115,10 @@ func TestOutgoing(t *testing.T) {
quitC <- struct{}{} quitC <- struct{}{}
} }
func baseTester(t *testing.T, proto *p2p.Protocol, ps *pss.Pss, ctx context.Context, cancel func(), quitC chan struct{}) (*Client, error) { func baseTester(t *testing.T, proto *p2p.Protocol, ps *pss.Pss, ctx context.Context, quitC chan struct{}) (*Client, error) {
var err error var err error
client := newTestclient(t, ctx, cancel, quitC) client := newTestclient(t, ctx, quitC)
err = client.RunProtocol(proto) err = client.RunProtocol(proto)
@ -143,7 +143,7 @@ func newProtocol(ping *pss.Ping) *p2p.Protocol {
} }
} }
func newTestclient(t *testing.T, ctx context.Context, cancel func(), quitC chan struct{}) *Client { func newTestclient(t *testing.T, ctx context.Context, quitC chan struct{}) *Client {
ps := pss.NewTestPss(nil) ps := pss.NewTestPss(nil)
srv := rpc.NewServer() srv := rpc.NewServer()
@ -166,7 +166,7 @@ func newTestclient(t *testing.T, ctx context.Context, cancel func(), quitC chan
sock.Close() sock.Close()
}() }()
pssclient, err := NewClient(ctx, cancel, "ws://localhost:8546") pssclient, err := NewClient(ctx, "ws://localhost:8546")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatalf(err.Error())
} }

View file

@ -131,6 +131,8 @@ func (self *Pss) Protocols() []p2p.Protocol {
// Starts the PssMsg protocol // Starts the PssMsg protocol
func (self *Pss) Run(p *p2p.Peer, rw p2p.MsgReadWriter) error { func (self *Pss) Run(p *p2p.Peer, rw p2p.MsgReadWriter) error {
self.lock.Lock()
defer self.lock.Unlock()
pp := protocols.NewPeer(p, rw, pssSpec) pp := protocols.NewPeer(p, rw, pssSpec)
//addr := network.NewAddrFromNodeID(id) //addr := network.NewAddrFromNodeID(id)
//potaddr := pot.NewHashAddressFromBytes(addr.OAddr) //potaddr := pot.NewHashAddressFromBytes(addr.OAddr)