2015-04-14 10:21:56 -05:00
|
|
|
// Contains some common utility functions for testing.
|
|
|
|
|
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2015-04-14 11:00:57 -05:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2015-04-14 10:21:56 -05:00
|
|
|
|
2015-04-15 02:50:31 -05:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-04-14 10:21:56 -05:00
|
|
|
)
|
|
|
|
|
2015-04-14 11:00:57 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|