mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
BUGFIX: stabilize firmata tests
This commit is contained in:
parent
cab50412e9
commit
bf98277e03
@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -9,7 +10,39 @@ import (
|
|||||||
"gobot.io/x/gobot/gobottest"
|
"gobot.io/x/gobot/gobottest"
|
||||||
)
|
)
|
||||||
|
|
||||||
type readWriteCloser struct{}
|
const semPublishWait = 10 * time.Millisecond
|
||||||
|
|
||||||
|
type readWriteCloser struct {
|
||||||
|
id string
|
||||||
|
}
|
||||||
|
|
||||||
|
var testWriteData = bytes.Buffer{}
|
||||||
|
var writeDataMutex sync.Mutex
|
||||||
|
|
||||||
|
// do not set this data directly, use always addTestReadData()
|
||||||
|
var testReadDataMap = make(map[string][]byte)
|
||||||
|
var rwDataMapMutex sync.Mutex
|
||||||
|
|
||||||
|
// arduino uno r3 protocol response "2.3"
|
||||||
|
var testDataProtocolResponse = []byte{249, 2, 3}
|
||||||
|
|
||||||
|
// arduino uno r3 firmware response "StandardFirmata.ino"
|
||||||
|
var testDataFirmwareResponse = []byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97,
|
||||||
|
0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46,
|
||||||
|
0, 105, 0, 110, 0, 111, 0, 247}
|
||||||
|
|
||||||
|
// arduino uno r3 capabilities response
|
||||||
|
var testDataCapabilitiesResponse = []byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3,
|
||||||
|
8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1,
|
||||||
|
1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0,
|
||||||
|
1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8,
|
||||||
|
4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2,
|
||||||
|
10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10,
|
||||||
|
127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 247}
|
||||||
|
|
||||||
|
// arduino uno r3 analog mapping response
|
||||||
|
var testDataAnalogMappingResponse = []byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
|
||||||
|
127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247}
|
||||||
|
|
||||||
func (readWriteCloser) Write(p []byte) (int, error) {
|
func (readWriteCloser) Write(p []byte) (int, error) {
|
||||||
writeDataMutex.Lock()
|
writeDataMutex.Lock()
|
||||||
@ -17,30 +50,37 @@ func (readWriteCloser) Write(p []byte) (int, error) {
|
|||||||
return testWriteData.Write(p)
|
return testWriteData.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
var clientMutex sync.Mutex
|
func (rwc readWriteCloser) addTestReadData(d []byte) {
|
||||||
var writeDataMutex sync.Mutex
|
// concurrent read/change of map is not allowed
|
||||||
var readDataMutex sync.Mutex
|
rwDataMapMutex.Lock()
|
||||||
var testReadData = []byte{}
|
defer rwDataMapMutex.Unlock()
|
||||||
var testWriteData = bytes.Buffer{}
|
|
||||||
|
|
||||||
func SetTestReadData(d []byte) {
|
data, ok := testReadDataMap[rwc.id]
|
||||||
readDataMutex.Lock()
|
if !ok {
|
||||||
defer readDataMutex.Unlock()
|
data = []byte{}
|
||||||
testReadData = d
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (readWriteCloser) Read(b []byte) (int, error) {
|
data = append(data, d...)
|
||||||
readDataMutex.Lock()
|
testReadDataMap[rwc.id] = data
|
||||||
defer readDataMutex.Unlock()
|
}
|
||||||
|
|
||||||
|
func (rwc readWriteCloser) Read(b []byte) (int, error) {
|
||||||
|
// concurrent change of map is not allowed
|
||||||
|
rwDataMapMutex.Lock()
|
||||||
|
defer rwDataMapMutex.Unlock()
|
||||||
|
|
||||||
|
data, ok := testReadDataMap[rwc.id]
|
||||||
|
if !ok {
|
||||||
|
// there was no content stored before to read
|
||||||
|
log.Printf("no content stored in %s", rwc.id)
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
size := len(b)
|
size := len(b)
|
||||||
if len(testReadData) < size {
|
if len(data) < size {
|
||||||
size = len(testReadData)
|
size = len(data)
|
||||||
}
|
}
|
||||||
copy(b, []byte(testReadData)[:size])
|
copy(b, []byte(data)[:size])
|
||||||
testReadData = testReadData[size:]
|
testReadDataMap[rwc.id] = data[size:]
|
||||||
|
|
||||||
return size, nil
|
return size, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,86 +88,45 @@ func (readWriteCloser) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testProtocolResponse() []byte {
|
func initTestFirmataWithReadWriteCloser(name string, data ...[]byte) (*Client, readWriteCloser) {
|
||||||
// arduino uno r3 protocol response "2.3"
|
|
||||||
return []byte{249, 2, 3}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testFirmwareResponse() []byte {
|
|
||||||
// arduino uno r3 firmware response "StandardFirmata.ino"
|
|
||||||
return []byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97,
|
|
||||||
0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46,
|
|
||||||
0, 105, 0, 110, 0, 111, 0, 247}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCapabilitiesResponse() []byte {
|
|
||||||
// arduino uno r3 capabilities response
|
|
||||||
return []byte{240, 108, 127, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3,
|
|
||||||
8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1,
|
|
||||||
1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0,
|
|
||||||
1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8, 4, 14, 127, 0, 1, 1, 1, 3, 8,
|
|
||||||
4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 4, 14, 127, 0, 1, 1, 1, 2,
|
|
||||||
10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10, 127, 0, 1, 1, 1, 2, 10,
|
|
||||||
127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 0, 1, 1, 1, 2, 10, 6, 1, 127, 247}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testAnalogMappingResponse() []byte {
|
|
||||||
// arduino uno r3 analog mapping response
|
|
||||||
return []byte{240, 106, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
|
|
||||||
127, 127, 127, 127, 0, 1, 2, 3, 4, 5, 247}
|
|
||||||
}
|
|
||||||
|
|
||||||
func initTestFirmata() *Client {
|
|
||||||
b := New()
|
b := New()
|
||||||
b.connection = readWriteCloser{}
|
rwc := readWriteCloser{id: name}
|
||||||
|
b.connection = rwc
|
||||||
|
|
||||||
for _, f := range []func() []byte{
|
for _, d := range data {
|
||||||
testProtocolResponse,
|
rwc.addTestReadData(d)
|
||||||
testFirmwareResponse,
|
|
||||||
testCapabilitiesResponse,
|
|
||||||
testAnalogMappingResponse,
|
|
||||||
} {
|
|
||||||
SetTestReadData(f())
|
|
||||||
b.process()
|
b.process()
|
||||||
}
|
}
|
||||||
|
|
||||||
return b
|
b.setConnected(true)
|
||||||
|
return b, rwc
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPins(t *testing.T) {
|
func TestPins(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse, testDataAnalogMappingResponse)
|
||||||
b.setConnected(true)
|
|
||||||
|
|
||||||
gobottest.Assert(t, len(b.Pins()), 20)
|
gobottest.Assert(t, len(b.Pins()), 20)
|
||||||
gobottest.Assert(t, len(b.analogPins), 6)
|
gobottest.Assert(t, len(b.analogPins), 6)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReportVersion(t *testing.T) {
|
func TestProtocolVersionQuery(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
//test if functions executes
|
|
||||||
gobottest.Assert(t, b.ProtocolVersionQuery(), nil)
|
gobottest.Assert(t, b.ProtocolVersionQuery(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryFirmware(t *testing.T) {
|
func TestFirmwareQuery(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
//test if functions executes
|
|
||||||
gobottest.Assert(t, b.FirmwareQuery(), nil)
|
gobottest.Assert(t, b.FirmwareQuery(), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryPinState(t *testing.T) {
|
func TestPinStateQuery(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
//test if functions executes
|
|
||||||
gobottest.Assert(t, b.PinStateQuery(1), nil)
|
gobottest.Assert(t, b.PinStateQuery(1), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessProtocolVersion(t *testing.T) {
|
func TestProcessProtocolVersion(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
rwc.addTestReadData(testDataProtocolResponse)
|
||||||
SetTestReadData([]byte{249, 2, 3})
|
|
||||||
|
|
||||||
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, "2.3")
|
gobottest.Assert(t, data, "2.3")
|
||||||
@ -138,16 +137,15 @@ func TestProcessProtocolVersion(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("ProtocolVersion was not published")
|
t.Errorf("ProtocolVersion was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessAnalogRead0(t *testing.T) {
|
func TestProcessAnalogRead0(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse, testDataAnalogMappingResponse)
|
||||||
b.setConnected(true)
|
rwc.addTestReadData([]byte{0xE0, 0x23, 0x05})
|
||||||
SetTestReadData([]byte{0xE0, 0x23, 0x05})
|
|
||||||
|
|
||||||
b.Once(b.Event("AnalogRead0"), func(data interface{}) {
|
b.Once(b.Event("AnalogRead0"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, 675)
|
gobottest.Assert(t, data, 675)
|
||||||
@ -158,16 +156,15 @@ func TestProcessAnalogRead0(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("AnalogRead0 was not published")
|
t.Errorf("AnalogRead0 was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessAnalogRead1(t *testing.T) {
|
func TestProcessAnalogRead1(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse, testDataAnalogMappingResponse)
|
||||||
b.setConnected(true)
|
rwc.addTestReadData([]byte{0xE1, 0x23, 0x06})
|
||||||
SetTestReadData([]byte{0xE1, 0x23, 0x06})
|
|
||||||
|
|
||||||
b.Once(b.Event("AnalogRead1"), func(data interface{}) {
|
b.Once(b.Event("AnalogRead1"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, 803)
|
gobottest.Assert(t, data, 803)
|
||||||
@ -178,17 +175,16 @@ func TestProcessAnalogRead1(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("AnalogRead1 was not published")
|
t.Errorf("AnalogRead1 was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessDigitalRead2(t *testing.T) {
|
func TestProcessDigitalRead2(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse)
|
||||||
b.setConnected(true)
|
|
||||||
b.pins[2].Mode = Input
|
b.pins[2].Mode = Input
|
||||||
SetTestReadData([]byte{0x90, 0x04, 0x00})
|
rwc.addTestReadData([]byte{0x90, 0x04, 0x00})
|
||||||
|
|
||||||
b.Once(b.Event("DigitalRead2"), func(data interface{}) {
|
b.Once(b.Event("DigitalRead2"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, 1)
|
gobottest.Assert(t, data, 1)
|
||||||
@ -199,17 +195,16 @@ func TestProcessDigitalRead2(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("DigitalRead2 was not published")
|
t.Errorf("DigitalRead2 was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessDigitalRead4(t *testing.T) {
|
func TestProcessDigitalRead4(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse)
|
||||||
b.setConnected(true)
|
|
||||||
b.pins[4].Mode = Input
|
b.pins[4].Mode = Input
|
||||||
SetTestReadData([]byte{0x90, 0x16, 0x00})
|
rwc.addTestReadData([]byte{0x90, 0x16, 0x00})
|
||||||
|
|
||||||
b.Once(b.Event("DigitalRead4"), func(data interface{}) {
|
b.Once(b.Event("DigitalRead4"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, 1)
|
gobottest.Assert(t, data, 1)
|
||||||
@ -220,41 +215,36 @@ func TestProcessDigitalRead4(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("DigitalRead4 was not published")
|
t.Errorf("DigitalRead4 was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDigitalWrite(t *testing.T) {
|
func TestDigitalWrite(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse)
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.DigitalWrite(13, 0), nil)
|
gobottest.Assert(t, b.DigitalWrite(13, 0), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetPinMode(t *testing.T) {
|
func TestSetPinMode(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse)
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.SetPinMode(13, Output), nil)
|
gobottest.Assert(t, b.SetPinMode(13, Output), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAnalogWrite(t *testing.T) {
|
func TestAnalogWrite(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse)
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.AnalogWrite(0, 128), nil)
|
gobottest.Assert(t, b.AnalogWrite(0, 128), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReportAnalog(t *testing.T) {
|
func TestReportAnalog(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.ReportAnalog(0, 1), nil)
|
gobottest.Assert(t, b.ReportAnalog(0, 1), nil)
|
||||||
gobottest.Assert(t, b.ReportAnalog(0, 0), nil)
|
gobottest.Assert(t, b.ReportAnalog(0, 0), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessPinState13(t *testing.T) {
|
func TestProcessPinState13(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name(), testDataCapabilitiesResponse, testDataAnalogMappingResponse)
|
||||||
b.setConnected(true)
|
rwc.addTestReadData([]byte{240, 110, 13, 1, 1, 247})
|
||||||
SetTestReadData([]byte{240, 110, 13, 1, 1, 247})
|
|
||||||
|
|
||||||
b.Once(b.Event("PinState13"), func(data interface{}) {
|
b.Once(b.Event("PinState13"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, Pin{[]int{0, 1, 4}, 1, 0, 1, 127})
|
gobottest.Assert(t, data, Pin{[]int{0, 1, 4}, 1, 0, 1, 127})
|
||||||
@ -265,40 +255,35 @@ func TestProcessPinState13(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("PinState13 was not published")
|
t.Errorf("PinState13 was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestI2cConfig(t *testing.T) {
|
func TestI2cConfig(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.I2cConfig(100), nil)
|
gobottest.Assert(t, b.I2cConfig(100), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestI2cWrite(t *testing.T) {
|
func TestI2cWrite(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.I2cWrite(0x00, []byte{0x01, 0x02}), nil)
|
gobottest.Assert(t, b.I2cWrite(0x00, []byte{0x01, 0x02}), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestI2cRead(t *testing.T) {
|
func TestI2cRead(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.I2cRead(0x00, 10), nil)
|
gobottest.Assert(t, b.I2cRead(0x00, 10), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteSysex(t *testing.T) {
|
func TestWriteSysex(t *testing.T) {
|
||||||
b := initTestFirmata()
|
b, _ := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
|
||||||
gobottest.Assert(t, b.WriteSysex([]byte{0x01, 0x02}), nil)
|
gobottest.Assert(t, b.WriteSysex([]byte{0x01, 0x02}), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessI2cReply(t *testing.T) {
|
func TestProcessI2cReply(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
rwc.addTestReadData([]byte{240, 119, 9, 0, 0, 0, 24, 1, 1, 0, 26, 1, 247})
|
||||||
SetTestReadData([]byte{240, 119, 9, 0, 0, 0, 24, 1, 1, 0, 26, 1, 247})
|
|
||||||
|
|
||||||
b.Once(b.Event("I2cReply"), func(data interface{}) {
|
b.Once(b.Event("I2cReply"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, I2cReply{
|
gobottest.Assert(t, data, I2cReply{
|
||||||
@ -313,18 +298,15 @@ func TestProcessI2cReply(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("I2cReply was not published")
|
t.Errorf("I2cReply was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessFirmwareQuery(t *testing.T) {
|
func TestProcessFirmwareQuery(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
rwc.addTestReadData(testDataFirmwareResponse)
|
||||||
SetTestReadData([]byte{240, 121, 2, 3, 83, 0, 116, 0, 97, 0, 110, 0, 100, 0, 97,
|
|
||||||
0, 114, 0, 100, 0, 70, 0, 105, 0, 114, 0, 109, 0, 97, 0, 116, 0, 97, 0, 46,
|
|
||||||
0, 105, 0, 110, 0, 111, 0, 247})
|
|
||||||
|
|
||||||
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, "StandardFirmata.ino")
|
gobottest.Assert(t, data, "StandardFirmata.ino")
|
||||||
@ -335,16 +317,15 @@ func TestProcessFirmwareQuery(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("FirmwareQuery was not published")
|
t.Errorf("FirmwareQuery was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProcessStringData(t *testing.T) {
|
func TestProcessStringData(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
rwc.addTestReadData(append([]byte{240, 0x71}, append([]byte("Hello Firmata!"), 247)...))
|
||||||
SetTestReadData(append([]byte{240, 0x71}, append([]byte("Hello Firmata!"), 247)...))
|
|
||||||
|
|
||||||
b.Once(b.Event("StringData"), func(data interface{}) {
|
b.Once(b.Event("StringData"), func(data interface{}) {
|
||||||
gobottest.Assert(t, data, "Hello Firmata!")
|
gobottest.Assert(t, data, "Hello Firmata!")
|
||||||
@ -355,55 +336,34 @@ func TestProcessStringData(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(100 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("StringData was not published")
|
t.Errorf("StringData was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
b := New()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
|
b.setConnected(false)
|
||||||
|
|
||||||
var responseMutex sync.Mutex
|
rwc.addTestReadData(testDataProtocolResponse)
|
||||||
responseMutex.Lock()
|
|
||||||
response := testProtocolResponse()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
|
|
||||||
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
|
||||||
responseMutex.Lock()
|
rwc.addTestReadData(testDataFirmwareResponse)
|
||||||
response = testFirmwareResponse()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
|
||||||
responseMutex.Lock()
|
rwc.addTestReadData(testDataCapabilitiesResponse)
|
||||||
response = testCapabilitiesResponse()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
b.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
b.Once(b.Event("CapabilityQuery"), func(data interface{}) {
|
||||||
responseMutex.Lock()
|
rwc.addTestReadData(testDataAnalogMappingResponse)
|
||||||
response = testAnalogMappingResponse()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
b.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
b.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
|
||||||
responseMutex.Lock()
|
rwc.addTestReadData(testDataProtocolResponse)
|
||||||
response = testProtocolResponse()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
go func() {
|
gobottest.Assert(t, b.Connect(rwc), nil)
|
||||||
for {
|
|
||||||
responseMutex.Lock()
|
|
||||||
readDataMutex.Lock()
|
|
||||||
testReadData = append(testReadData, response...)
|
|
||||||
readDataMutex.Unlock()
|
|
||||||
responseMutex.Unlock()
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
gobottest.Assert(t, b.Connect(readWriteCloser{}), nil)
|
|
||||||
time.Sleep(150 * time.Millisecond)
|
time.Sleep(150 * time.Millisecond)
|
||||||
gobottest.Assert(t, b.Disconnect(), nil)
|
gobottest.Assert(t, b.Disconnect(), nil)
|
||||||
}
|
}
|
||||||
@ -449,9 +409,8 @@ func TestServoConfig(t *testing.T) {
|
|||||||
|
|
||||||
func TestProcessSysexData(t *testing.T) {
|
func TestProcessSysexData(t *testing.T) {
|
||||||
sem := make(chan bool)
|
sem := make(chan bool)
|
||||||
b := initTestFirmata()
|
b, rwc := initTestFirmataWithReadWriteCloser(t.Name())
|
||||||
b.setConnected(true)
|
rwc.addTestReadData([]byte{240, 17, 1, 2, 3, 247})
|
||||||
SetTestReadData([]byte{240, 17, 1, 2, 3, 247})
|
|
||||||
|
|
||||||
b.Once("SysexResponse", func(data interface{}) {
|
b.Once("SysexResponse", func(data interface{}) {
|
||||||
gobottest.Assert(t, data, []byte{240, 17, 1, 2, 3, 247})
|
gobottest.Assert(t, data, []byte{240, 17, 1, 2, 3, 247})
|
||||||
@ -462,7 +421,7 @@ func TestProcessSysexData(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-sem:
|
case <-sem:
|
||||||
case <-time.After(250 * time.Millisecond):
|
case <-time.After(semPublishWait):
|
||||||
t.Errorf("SysexResponse was not published")
|
t.Errorf("SysexResponse was not published")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user