Fix tests for >1 CPU (fixes #99)

This commit is contained in:
Jakob Borg 2014-03-22 17:06:15 +01:00
parent 68d9454bc4
commit 513100bb92
3 changed files with 42 additions and 25 deletions

View File

@ -15,7 +15,7 @@ prepare() {
} }
test() { test() {
go test ./... go test -cpu=1,2,4 ./...
} }
tarDist() { tarDist() {

View File

@ -1,14 +1,23 @@
package protocol package protocol
import "io" import (
"io"
"time"
)
type TestModel struct { type TestModel struct {
data []byte data []byte
repo string repo string
name string name string
offset int64 offset int64
size int size int
closed bool closedCh chan bool
}
func newTestModel() *TestModel {
return &TestModel{
closedCh: make(chan bool),
}
} }
func (t *TestModel) Index(nodeID string, files []FileInfo) { func (t *TestModel) Index(nodeID string, files []FileInfo) {
@ -26,7 +35,16 @@ func (t *TestModel) Request(nodeID, repo, name string, offset int64, size int) (
} }
func (t *TestModel) Close(nodeID string, err error) { func (t *TestModel) Close(nodeID string, err error) {
t.closed = true close(t.closedCh)
}
func (t *TestModel) isClosed() bool {
select {
case <-t.closedCh:
return true
case <-time.After(1 * time.Second):
return false // Timeout
}
} }
type ErrPipe struct { type ErrPipe struct {

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"testing" "testing"
"testing/quick" "testing/quick"
"time"
) )
func TestHeaderFunctions(t *testing.T) { func TestHeaderFunctions(t *testing.T) {
@ -42,8 +41,8 @@ func TestPingErr(t *testing.T) {
for i := 0; i < 12; i++ { for i := 0; i < 12; i++ {
for j := 0; j < 12; j++ { for j := 0; j < 12; j++ {
m0 := &TestModel{} m0 := newTestModel()
m1 := &TestModel{} m1 := newTestModel()
ar, aw := io.Pipe() ar, aw := io.Pipe()
br, bw := io.Pipe() br, bw := io.Pipe()
@ -69,8 +68,9 @@ func TestRequestResponseErr(t *testing.T) {
var pass bool var pass bool
for i := 0; i < 48; i++ { for i := 0; i < 48; i++ {
for j := 0; j < 38; j++ { for j := 0; j < 38; j++ {
m0 := &TestModel{data: []byte("response data")} m0 := newTestModel()
m1 := &TestModel{} m0.data = []byte("response data")
m1 := newTestModel()
ar, aw := io.Pipe() ar, aw := io.Pipe()
br, bw := io.Pipe() br, bw := io.Pipe()
@ -83,11 +83,10 @@ func TestRequestResponseErr(t *testing.T) {
d, err := c1.Request("default", "tn", 1234, 5678) d, err := c1.Request("default", "tn", 1234, 5678)
if err == e || err == ErrClosed { if err == e || err == ErrClosed {
t.Logf("Error at %d+%d bytes", i, j) t.Logf("Error at %d+%d bytes", i, j)
if !m1.closed { if !m1.isClosed() {
t.Error("c1 not closed") t.Error("c1 not closed")
} }
time.Sleep(1 * time.Millisecond) if !m0.isClosed() {
if !m0.closed {
t.Error("c0 not closed") t.Error("c0 not closed")
} }
continue continue
@ -120,8 +119,8 @@ func TestRequestResponseErr(t *testing.T) {
} }
func TestVersionErr(t *testing.T) { func TestVersionErr(t *testing.T) {
m0 := &TestModel{} m0 := newTestModel()
m1 := &TestModel{} m1 := newTestModel()
ar, aw := io.Pipe() ar, aw := io.Pipe()
br, bw := io.Pipe() br, bw := io.Pipe()
@ -136,14 +135,14 @@ func TestVersionErr(t *testing.T) {
})) }))
c0.flush() c0.flush()
if !m1.closed { if !m1.isClosed() {
t.Error("Connection should close due to unknown version") t.Error("Connection should close due to unknown version")
} }
} }
func TestTypeErr(t *testing.T) { func TestTypeErr(t *testing.T) {
m0 := &TestModel{} m0 := newTestModel()
m1 := &TestModel{} m1 := newTestModel()
ar, aw := io.Pipe() ar, aw := io.Pipe()
br, bw := io.Pipe() br, bw := io.Pipe()
@ -158,14 +157,14 @@ func TestTypeErr(t *testing.T) {
})) }))
c0.flush() c0.flush()
if !m1.closed { if !m1.isClosed() {
t.Error("Connection should close due to unknown message type") t.Error("Connection should close due to unknown message type")
} }
} }
func TestClose(t *testing.T) { func TestClose(t *testing.T) {
m0 := &TestModel{} m0 := newTestModel()
m1 := &TestModel{} m1 := newTestModel()
ar, aw := io.Pipe() ar, aw := io.Pipe()
br, bw := io.Pipe() br, bw := io.Pipe()