2013-12-15 03:43:31 -07:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2013-12-19 16:01:34 -07:00
|
|
|
"errors"
|
|
|
|
"io"
|
2013-12-15 03:43:31 -07:00
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
2013-12-19 16:01:34 -07:00
|
|
|
"time"
|
2013-12-15 03:43:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeaderFunctions(t *testing.T) {
|
|
|
|
f := func(ver, id, typ int) bool {
|
|
|
|
ver = int(uint(ver) % 16)
|
|
|
|
id = int(uint(id) % 4096)
|
|
|
|
typ = int(uint(typ) % 256)
|
|
|
|
h0 := header{ver, id, typ}
|
|
|
|
h1 := decodeHeader(encodeHeader(h0))
|
|
|
|
return h0 == h1
|
|
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 16:01:34 -07:00
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
c0 := NewConnection("c0", ar, bw, nil, nil)
|
|
|
|
c1 := NewConnection("c1", br, aw, nil, nil)
|
2013-12-19 16:01:34 -07:00
|
|
|
|
2014-01-09 05:58:35 -07:00
|
|
|
if ok := c0.ping(); !ok {
|
2013-12-19 16:01:34 -07:00
|
|
|
t.Error("c0 ping failed")
|
|
|
|
}
|
2014-01-09 05:58:35 -07:00
|
|
|
if ok := c1.ping(); !ok {
|
2013-12-19 16:01:34 -07:00
|
|
|
t.Error("c1 ping failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingErr(t *testing.T) {
|
2014-02-24 05:29:30 -07:00
|
|
|
e := errors.New("something broke")
|
2013-12-19 16:01:34 -07:00
|
|
|
|
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
for j := 0; j < 12; j++ {
|
|
|
|
m0 := &TestModel{}
|
|
|
|
m1 := &TestModel{}
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
|
|
|
|
ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
c0 := NewConnection("c0", ar, ebw, m0, nil)
|
|
|
|
NewConnection("c1", br, eaw, m1, nil)
|
2013-12-19 16:01:34 -07:00
|
|
|
|
2014-01-09 05:58:35 -07:00
|
|
|
res := c0.ping()
|
2013-12-19 16:01:34 -07:00
|
|
|
if (i < 4 || j < 4) && res {
|
|
|
|
t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
|
|
|
|
} else if (i >= 8 && j >= 8) && !res {
|
|
|
|
t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestResponseErr(t *testing.T) {
|
2014-02-24 05:29:30 -07:00
|
|
|
e := errors.New("something broke")
|
2013-12-19 16:01:34 -07:00
|
|
|
|
|
|
|
var pass bool
|
2014-02-13 04:41:25 -07:00
|
|
|
for i := 0; i < 48; i++ {
|
|
|
|
for j := 0; j < 38; j++ {
|
2013-12-19 16:01:34 -07:00
|
|
|
m0 := &TestModel{data: []byte("response data")}
|
|
|
|
m1 := &TestModel{}
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
|
|
|
|
ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
NewConnection("c0", ar, ebw, m0, nil)
|
|
|
|
c1 := NewConnection("c1", br, eaw, m1, nil)
|
2013-12-19 16:01:34 -07:00
|
|
|
|
2014-02-23 05:58:10 -07:00
|
|
|
d, err := c1.Request("default", "tn", 1234, 5678)
|
2013-12-19 16:01:34 -07:00
|
|
|
if err == e || err == ErrClosed {
|
|
|
|
t.Logf("Error at %d+%d bytes", i, j)
|
|
|
|
if !m1.closed {
|
|
|
|
t.Error("c1 not closed")
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
if !m0.closed {
|
|
|
|
t.Error("c0 not closed")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if string(d) != "response data" {
|
|
|
|
t.Errorf("Incorrect response data %q", string(d))
|
|
|
|
}
|
2014-02-13 04:41:25 -07:00
|
|
|
if m0.repo != "default" {
|
2014-02-24 05:24:03 -07:00
|
|
|
t.Errorf("Incorrect repo %q", m0.repo)
|
2014-02-13 04:41:25 -07:00
|
|
|
}
|
2013-12-19 16:01:34 -07:00
|
|
|
if m0.name != "tn" {
|
2014-02-24 05:24:03 -07:00
|
|
|
t.Errorf("Incorrect name %q", m0.name)
|
2013-12-19 16:01:34 -07:00
|
|
|
}
|
2014-02-23 05:58:10 -07:00
|
|
|
if m0.offset != 1234 {
|
2014-02-24 05:24:03 -07:00
|
|
|
t.Errorf("Incorrect offset %d", m0.offset)
|
2013-12-19 16:01:34 -07:00
|
|
|
}
|
2014-02-23 05:58:10 -07:00
|
|
|
if m0.size != 5678 {
|
2014-02-24 05:24:03 -07:00
|
|
|
t.Errorf("Incorrect size %d", m0.size)
|
2013-12-19 16:01:34 -07:00
|
|
|
}
|
|
|
|
t.Logf("Pass at %d+%d bytes", i, j)
|
|
|
|
pass = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !pass {
|
|
|
|
t.Error("Never passed")
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 00:06:54 -07:00
|
|
|
|
|
|
|
func TestVersionErr(t *testing.T) {
|
|
|
|
m0 := &TestModel{}
|
|
|
|
m1 := &TestModel{}
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
c0 := NewConnection("c0", ar, bw, m0, nil)
|
|
|
|
NewConnection("c1", br, aw, m1, nil)
|
2013-12-21 00:06:54 -07:00
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
c0.xw.WriteUint32(encodeHeader(header{
|
2013-12-21 00:06:54 -07:00
|
|
|
version: 2,
|
|
|
|
msgID: 0,
|
|
|
|
msgType: 0,
|
2014-02-20 09:40:15 -07:00
|
|
|
}))
|
2013-12-21 00:06:54 -07:00
|
|
|
c0.flush()
|
|
|
|
|
|
|
|
if !m1.closed {
|
|
|
|
t.Error("Connection should close due to unknown version")
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 00:15:19 -07:00
|
|
|
|
|
|
|
func TestTypeErr(t *testing.T) {
|
|
|
|
m0 := &TestModel{}
|
|
|
|
m1 := &TestModel{}
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
c0 := NewConnection("c0", ar, bw, m0, nil)
|
|
|
|
NewConnection("c1", br, aw, m1, nil)
|
2013-12-21 00:15:19 -07:00
|
|
|
|
2014-02-20 09:40:15 -07:00
|
|
|
c0.xw.WriteUint32(encodeHeader(header{
|
2013-12-21 00:15:19 -07:00
|
|
|
version: 0,
|
|
|
|
msgID: 0,
|
|
|
|
msgType: 42,
|
2014-02-20 09:40:15 -07:00
|
|
|
}))
|
2013-12-21 00:15:19 -07:00
|
|
|
c0.flush()
|
|
|
|
|
|
|
|
if !m1.closed {
|
|
|
|
t.Error("Connection should close due to unknown message type")
|
|
|
|
}
|
|
|
|
}
|
2013-12-28 08:30:02 -07:00
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
m0 := &TestModel{}
|
|
|
|
m1 := &TestModel{}
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-01-23 05:12:45 -07:00
|
|
|
c0 := NewConnection("c0", ar, bw, m0, nil)
|
|
|
|
NewConnection("c1", br, aw, m1, nil)
|
2013-12-28 08:30:02 -07:00
|
|
|
|
2014-01-09 05:58:35 -07:00
|
|
|
c0.close(nil)
|
2013-12-28 08:30:02 -07:00
|
|
|
|
|
|
|
ok := c0.isClosed()
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Connection should be closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of these should panic, some should return an error
|
|
|
|
|
2014-01-09 05:58:35 -07:00
|
|
|
ok = c0.ping()
|
2013-12-28 08:30:02 -07:00
|
|
|
if ok {
|
|
|
|
t.Error("Ping should not return true")
|
|
|
|
}
|
|
|
|
|
2014-02-13 04:41:25 -07:00
|
|
|
c0.Index("default", nil)
|
|
|
|
c0.Index("default", nil)
|
2013-12-28 08:30:02 -07:00
|
|
|
|
2014-02-23 05:58:10 -07:00
|
|
|
_, err := c0.Request("default", "foo", 0, 0)
|
2013-12-28 08:30:02 -07:00
|
|
|
if err == nil {
|
|
|
|
t.Error("Request should return an error")
|
|
|
|
}
|
|
|
|
}
|