Merge pull request #2688 from calmh/prototests

Improve protocol tests, close handling
This commit is contained in:
Audrius Butkevicius 2016-01-12 08:44:02 +00:00
commit 1e45111bde
3 changed files with 25 additions and 21 deletions

View File

@ -16,12 +16,13 @@ type TestModel struct {
hash []byte hash []byte
flags uint32 flags uint32
options []Option options []Option
closedCh chan bool closedCh chan struct{}
closedErr error
} }
func newTestModel() *TestModel { func newTestModel() *TestModel {
return &TestModel{ return &TestModel{
closedCh: make(chan bool), closedCh: make(chan struct{}),
} }
} }
@ -44,18 +45,19 @@ func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64
} }
func (t *TestModel) Close(deviceID DeviceID, err error) { func (t *TestModel) Close(deviceID DeviceID, err error) {
t.closedErr = err
close(t.closedCh) close(t.closedCh)
} }
func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) { func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
} }
func (t *TestModel) isClosed() bool { func (t *TestModel) closedError() error {
select { select {
case <-t.closedCh: case <-t.closedCh:
return true return t.closedErr
case <-time.After(1 * time.Second): case <-time.After(1 * time.Second):
return false // Timeout return nil // Timeout
} }
} }

View File

@ -691,6 +691,7 @@ func (c *rawConnection) writerLoop() {
func (c *rawConnection) close(err error) { func (c *rawConnection) close(err error) {
c.once.Do(func() { c.once.Do(func() {
l.Debugln("close due to", err)
close(c.closed) close(c.closed)
c.awaitingMut.Lock() c.awaitingMut.Lock()

View File

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -107,14 +108,14 @@ func TestVersionErr(t *testing.T) {
w := xdr.NewWriter(c0.cw) w := xdr.NewWriter(c0.cw)
w.WriteUint32(encodeHeader(header{ w.WriteUint32(encodeHeader(header{
version: 2, version: 2, // higher than supported
msgID: 0, msgID: 0,
msgType: 0, msgType: messageTypeIndex,
})) }))
w.WriteUint32(0) // Avoids reader closing due to EOF w.WriteUint32(0) // Avoids reader closing due to EOF
if !m1.isClosed() { if err := m1.closedError(); err == nil || !strings.Contains(err.Error(), "unknown protocol version") {
t.Error("Connection should close due to unknown version") t.Error("Connection should close due to unknown version, not", err)
} }
} }
@ -140,8 +141,8 @@ func TestTypeErr(t *testing.T) {
})) }))
w.WriteUint32(0) // Avoids reader closing due to EOF w.WriteUint32(0) // Avoids reader closing due to EOF
if !m1.isClosed() { if err := m1.closedError(); err == nil || !strings.Contains(err.Error(), "unknown message type") {
t.Error("Connection should close due to unknown message type") t.Error("Connection should close due to unknown message type, not", err)
} }
} }
@ -159,10 +160,10 @@ func TestClose(t *testing.T) {
c0.ClusterConfig(ClusterConfigMessage{}) c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{}) c1.ClusterConfig(ClusterConfigMessage{})
c0.close(nil) c0.close(errors.New("manual close"))
<-c0.closed <-c0.closed
if !m0.isClosed() { if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
t.Fatal("Connection should be closed") t.Fatal("Connection should be closed")
} }