Skip to content

Commit 65e39bf

Browse files
committed
p2p: add MsgPipe for protocol testing
1 parent e28c60c commit 65e39bf

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

‎p2p/message.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package p2p
33
import (
44
"bytes"
55
"encoding/binary"
6+
"errors"
67
"io"
78
"io/ioutil"
89
"math/big"
10+
"sync/atomic"
911

1012
"github.com/ethereum/go-ethereum/ethutil"
1113
"github.com/ethereum/go-ethereum/rlp"
@@ -153,3 +155,78 @@ func (r *postrack) ReadByte() (byte, error) {
153155
}
154156
return b, err
155157
}
158+
159+
// MsgPipe creates a message pipe. Reads on one end are matched
160+
// with writes on the other. The pipe is full-duplex, both ends
161+
// implement MsgReadWriter.
162+
func MsgPipe() (*MsgPipeRW, *MsgPipeRW) {
163+
var (
164+
c1, c2 = make(chan Msg), make(chan Msg)
165+
closing = make(chan struct{})
166+
closed = new(int32)
167+
rw1 = &MsgPipeRW{c1, c2, closing, closed}
168+
rw2 = &MsgPipeRW{c2, c1, closing, closed}
169+
)
170+
return rw1, rw2
171+
}
172+
173+
// ErrPipeClosed is returned from pipe operations after the
174+
// pipe has been closed.
175+
var ErrPipeClosed = errors.New("p2p: read or write on closed message pipe")
176+
177+
// MsgPipeRW is an endpoint of a MsgReadWriter pipe.
178+
type MsgPipeRW struct {
179+
w chan<- Msg
180+
r <-chan Msg
181+
closing chan struct{}
182+
closed *int32
183+
}
184+
185+
// WriteMsg sends a messsage on the pipe.
186+
// It blocks until the receiver has consumed the message payload.
187+
func (p *MsgPipeRW) WriteMsg(msg Msg) error {
188+
if atomic.LoadInt32(p.closed) == 0 {
189+
consumed := make(chan struct{}, 1)
190+
msg.Payload = &eofSignal{msg.Payload, int64(msg.Size), consumed}
191+
select {
192+
case p.w <- msg:
193+
if msg.Size > 0 {
194+
// wait for payload read or discard
195+
<-consumed
196+
}
197+
return nil
198+
case <-p.closing:
199+
}
200+
}
201+
return ErrPipeClosed
202+
}
203+
204+
// EncodeMsg is a convenient shorthand for sending an RLP-encoded message.
205+
func (p *MsgPipeRW) EncodeMsg(code uint64, data ...interface{}) error {
206+
return p.WriteMsg(NewMsg(code, data...))
207+
}
208+
209+
// ReadMsg returns a message sent on the other end of the pipe.
210+
func (p *MsgPipeRW) ReadMsg() (Msg, error) {
211+
if atomic.LoadInt32(p.closed) == 0 {
212+
select {
213+
case msg := <-p.r:
214+
return msg, nil
215+
case <-p.closing:
216+
}
217+
}
218+
return Msg{}, ErrPipeClosed
219+
}
220+
221+
// Close unblocks any pending ReadMsg and WriteMsg calls on both ends
222+
// of the pipe. They will return ErrPipeClosed. Note that Close does
223+
// not interrupt any reads from a message payload.
224+
func (p *MsgPipeRW) Close() error {
225+
if atomic.AddInt32(p.closed, 1) != 1 {
226+
// someone else is already closing
227+
atomic.StoreInt32(p.closed, 1) // avoid overflow
228+
return nil
229+
}
230+
close(p.closing)
231+
return nil
232+
}

‎p2p/message_test.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package p2p
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
7+
"runtime"
68
"testing"
9+
"time"
710

811
"github.com/ethereum/go-ethereum/ethutil"
912
)
@@ -68,3 +71,63 @@ func TestDecodeRealMsg(t *testing.T) {
6871
t.Errorf("incorrect code %d, want %d", msg.Code, 0)
6972
}
7073
}
74+
75+
func ExampleMsgPipe() {
76+
rw1, rw2 := MsgPipe()
77+
go func() {
78+
rw1.EncodeMsg(8, []byte{0, 0})
79+
rw1.EncodeMsg(5, []byte{1, 1})
80+
rw1.Close()
81+
}()
82+
83+
for {
84+
msg, err := rw2.ReadMsg()
85+
if err != nil {
86+
break
87+
}
88+
var data [1][]byte
89+
msg.Decode(&data)
90+
fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
91+
}
92+
// Output:
93+
// msg: 8, 0000
94+
// msg: 5, 0101
95+
}
96+
97+
func TestMsgPipeUnblockWrite(t *testing.T) {
98+
loop:
99+
for i := 0; i < 100; i++ {
100+
rw1, rw2 := MsgPipe()
101+
done := make(chan struct{})
102+
go func() {
103+
if err := rw1.EncodeMsg(1); err == nil {
104+
t.Error("EncodeMsg returned nil error")
105+
} else if err != ErrPipeClosed {
106+
t.Error("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
107+
}
108+
close(done)
109+
}()
110+
111+
// this call should ensure that EncodeMsg is waiting to
112+
// deliver sometimes. if this isn't done, Close is likely to
113+
// be executed before EncodeMsg starts and then we won't test
114+
// all the cases.
115+
runtime.Gosched()
116+
117+
rw2.Close()
118+
select {
119+
case <-done:
120+
case <-time.After(200 * time.Millisecond):
121+
t.Errorf("write didn't unblock")
122+
break loop
123+
}
124+
}
125+
}
126+
127+
// This test should panic if concurrent close isn't implemented correctly.
128+
func TestMsgPipeConcurrentClose(t *testing.T) {
129+
rw1, _ := MsgPipe()
130+
for i := 0; i < 10; i++ {
131+
go rw1.Close()
132+
}
133+
}

0 commit comments

Comments
 (0)