aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/message_test.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <obscuren@users.noreply.github.com>2014-12-15 17:22:35 +0800
committerJeffrey Wilcke <obscuren@users.noreply.github.com>2014-12-15 17:22:35 +0800
commit15e46b97b46864f7f72b932c0fe586ea6d223406 (patch)
tree83b08df50a2978cf518aae1bf70738ecd82ed4be /p2p/message_test.go
parentccb4fc8d0370afd85ea9ffca635dae7e792dd66d (diff)
parentda900f94358a9b293a286066b0922a6f7b5d571c (diff)
downloaddexon-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.gz
dexon-15e46b97b46864f7f72b932c0fe586ea6d223406.tar.zst
dexon-15e46b97b46864f7f72b932c0fe586ea6d223406.zip
Merge pull request #204 from fjl/fix-p2p-disconnect
p2p: fix decoding of disconnect reason
Diffstat (limited to 'p2p/message_test.go')
-rw-r--r--p2p/message_test.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/p2p/message_test.go b/p2p/message_test.go
index 557bfed26..066d2516d 100644
--- a/p2p/message_test.go
+++ b/p2p/message_test.go
@@ -2,8 +2,11 @@ package p2p
import (
"bytes"
+ "fmt"
"io/ioutil"
+ "runtime"
"testing"
+ "time"
"github.com/ethereum/go-ethereum/ethutil"
)
@@ -68,3 +71,63 @@ func TestDecodeRealMsg(t *testing.T) {
t.Errorf("incorrect code %d, want %d", msg.Code, 0)
}
}
+
+func ExampleMsgPipe() {
+ rw1, rw2 := MsgPipe()
+ go func() {
+ rw1.EncodeMsg(8, []byte{0, 0})
+ rw1.EncodeMsg(5, []byte{1, 1})
+ rw1.Close()
+ }()
+
+ for {
+ msg, err := rw2.ReadMsg()
+ if err != nil {
+ break
+ }
+ var data [1][]byte
+ msg.Decode(&data)
+ fmt.Printf("msg: %d, %x\n", msg.Code, data[0])
+ }
+ // Output:
+ // msg: 8, 0000
+ // msg: 5, 0101
+}
+
+func TestMsgPipeUnblockWrite(t *testing.T) {
+loop:
+ for i := 0; i < 100; i++ {
+ rw1, rw2 := MsgPipe()
+ done := make(chan struct{})
+ go func() {
+ if err := rw1.EncodeMsg(1); err == nil {
+ t.Error("EncodeMsg returned nil error")
+ } else if err != ErrPipeClosed {
+ t.Error("EncodeMsg returned wrong error: got %v, want %v", err, ErrPipeClosed)
+ }
+ close(done)
+ }()
+
+ // this call should ensure that EncodeMsg is waiting to
+ // deliver sometimes. if this isn't done, Close is likely to
+ // be executed before EncodeMsg starts and then we won't test
+ // all the cases.
+ runtime.Gosched()
+
+ rw2.Close()
+ select {
+ case <-done:
+ case <-time.After(200 * time.Millisecond):
+ t.Errorf("write didn't unblock")
+ break loop
+ }
+ }
+}
+
+// This test should panic if concurrent close isn't implemented correctly.
+func TestMsgPipeConcurrentClose(t *testing.T) {
+ rw1, _ := MsgPipe()
+ for i := 0; i < 10; i++ {
+ go rw1.Close()
+ }
+}