diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-15 00:00:57 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-15 00:00:57 +0800 |
commit | 4fb7ab5d090a49837ca50318fab468b056f2ec9f (patch) | |
tree | 76ddbe3529ffa95160c751f21a22abf5d3ba2b76 /whisper/common_test.go | |
parent | 86372b20c0995abe0343ce5f453be113e5f192d0 (diff) | |
download | go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar.gz go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar.bz2 go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar.lz go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar.xz go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.tar.zst go-tangerine-4fb7ab5d090a49837ca50318fab468b056f2ec9f.zip |
whisper: mock tests to use simulated peers
Diffstat (limited to 'whisper/common_test.go')
-rw-r--r-- | whisper/common_test.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/whisper/common_test.go b/whisper/common_test.go index 8c12f8aeb..4e221d6b1 100644 --- a/whisper/common_test.go +++ b/whisper/common_test.go @@ -3,7 +3,9 @@ package whisper import ( + "bytes" "fmt" + "io/ioutil" "math/rand" "github.com/ethereum/go-ethereum/p2p" @@ -36,3 +38,33 @@ func whisperCaps() []p2p.Cap { }, } } + +// bufMsgPipe creates a buffered message pipe between two endpoints. +func bufMsgPipe() (*p2p.MsgPipeRW, *p2p.MsgPipeRW) { + A, midA := p2p.MsgPipe() + midB, B := p2p.MsgPipe() + + go copyMsgPipe(midA, midB) + go copyMsgPipe(midB, midA) + + return A, B +} + +// copyMsgPipe copies messages from the src pipe to the dest. +func copyMsgPipe(dst, src *p2p.MsgPipeRW) { + defer dst.Close() + for { + msg, err := src.ReadMsg() + if err != nil { + return + } + data, err := ioutil.ReadAll(msg.Payload) + if err != nil { + return + } + msg.Payload = bytes.NewReader(data) + if err := dst.WriteMsg(msg); err != nil { + return + } + } +} |