2014-10-23 18:57:54 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2014-11-04 14:21:44 +02:00
|
|
|
"bytes"
|
2015-03-04 13:03:43 +02:00
|
|
|
"encoding/hex"
|
2014-12-12 12:39:07 +02:00
|
|
|
"fmt"
|
2015-02-05 04:07:58 +02:00
|
|
|
"io"
|
2014-11-04 14:21:44 +02:00
|
|
|
"io/ioutil"
|
2014-12-12 12:39:07 +02:00
|
|
|
"runtime"
|
2015-03-04 13:03:43 +02:00
|
|
|
"strings"
|
2014-10-23 18:57:54 +03:00
|
|
|
"testing"
|
2014-12-12 12:39:07 +02:00
|
|
|
"time"
|
2014-10-23 18:57:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewMsg(t *testing.T) {
|
2014-11-04 14:21:44 +02:00
|
|
|
msg := NewMsg(3, 1, "000")
|
|
|
|
if msg.Code != 3 {
|
|
|
|
t.Errorf("incorrect code %d, want %d", msg.Code)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2015-03-04 13:03:43 +02:00
|
|
|
expect := unhex("c50183303030")
|
|
|
|
if msg.Size != uint32(len(expect)) {
|
|
|
|
t.Errorf("incorrect size %d, want %d", msg.Size, len(expect))
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
2014-11-04 14:21:44 +02:00
|
|
|
pl, _ := ioutil.ReadAll(msg.Payload)
|
|
|
|
if !bytes.Equal(pl, expect) {
|
|
|
|
t.Errorf("incorrect payload content, got %x, want %x", pl, expect)
|
2014-10-23 18:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 12:39:07 +02:00
|
|
|
func ExampleMsgPipe() {
|
|
|
|
rw1, rw2 := MsgPipe()
|
|
|
|
go func() {
|
2015-01-06 12:35:09 +02:00
|
|
|
EncodeMsg(rw1, 8, []byte{0, 0})
|
|
|
|
EncodeMsg(rw1, 5, []byte{1, 1})
|
2014-12-12 12:39:07 +02:00
|
|
|
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() {
|
2015-01-06 12:35:09 +02:00
|
|
|
if err := EncodeMsg(rw1, 1); err == nil {
|
2014-12-12 12:39:07 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
|
|
|
|
func TestEOFSignal(t *testing.T) {
|
|
|
|
rb := make([]byte, 10)
|
|
|
|
|
|
|
|
// empty reader
|
|
|
|
eof := make(chan struct{}, 1)
|
|
|
|
sig := &eofSignal{new(bytes.Buffer), 0, eof}
|
|
|
|
if n, err := sig.Read(rb); n != 0 || err != io.EOF {
|
|
|
|
t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-eof:
|
|
|
|
default:
|
|
|
|
t.Error("EOF chan not signaled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// count before error
|
|
|
|
eof = make(chan struct{}, 1)
|
|
|
|
sig = &eofSignal{bytes.NewBufferString("aaaaaaaa"), 4, eof}
|
|
|
|
if n, err := sig.Read(rb); n != 4 || err != nil {
|
|
|
|
t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-eof:
|
|
|
|
default:
|
|
|
|
t.Error("EOF chan not signaled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// error before count
|
|
|
|
eof = make(chan struct{}, 1)
|
|
|
|
sig = &eofSignal{bytes.NewBufferString("aaaa"), 999, eof}
|
|
|
|
if n, err := sig.Read(rb); n != 4 || err != nil {
|
|
|
|
t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
|
|
|
|
}
|
|
|
|
if n, err := sig.Read(rb); n != 0 || err != io.EOF {
|
|
|
|
t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-eof:
|
|
|
|
default:
|
|
|
|
t.Error("EOF chan not signaled")
|
|
|
|
}
|
|
|
|
|
|
|
|
// no signal if neither occurs
|
|
|
|
eof = make(chan struct{}, 1)
|
|
|
|
sig = &eofSignal{bytes.NewBufferString("aaaaaaaaaaaaaaaaaaaaa"), 999, eof}
|
|
|
|
if n, err := sig.Read(rb); n != 10 || err != nil {
|
|
|
|
t.Errorf("Read returned unexpected values: (%v, %v)", n, err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-eof:
|
|
|
|
t.Error("unexpected EOF signal")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2015-03-04 13:03:43 +02:00
|
|
|
|
|
|
|
func unhex(str string) []byte {
|
|
|
|
b, err := hex.DecodeString(strings.Replace(str, "\n", "", -1))
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid hex string: %q", str))
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|