1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-08 19:29:16 +08:00

Fix more inconsistent tests

This commit is contained in:
Adrian Zankich 2014-10-28 16:22:05 -07:00
parent 56d396339b
commit fd3e7866a9
2 changed files with 48 additions and 9 deletions

View File

@ -1,9 +1,10 @@
package i2c
import (
"github.com/hybridgroup/gobot"
"testing"
"time"
"github.com/hybridgroup/gobot"
)
// --------- HELPERS
@ -39,6 +40,7 @@ func TestNewHMC6352Driver(t *testing.T) {
// Methods
func TestHMC6352DriverStart(t *testing.T) {
sem := make(chan bool)
// when len(data) is 2
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
@ -54,9 +56,20 @@ func TestHMC6352DriverStart(t *testing.T) {
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
if hmc.Heading == uint16(2534) {
sem <- true
}
}
}()
gobot.Assert(t, hmc.Heading, uint16(2534))
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Heading not read correctly")
}
// when len(data) is not 2
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
@ -67,9 +80,20 @@ func TestHMC6352DriverStart(t *testing.T) {
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
if hmc.Heading == uint16(0) {
sem <- true
}
}
}()
gobot.Assert(t, hmc.Heading, uint16(0))
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Heading not read correctly")
}
}
func TestHMC6352DriverInit(t *testing.T) {

View File

@ -1,9 +1,10 @@
package i2c
import (
"github.com/hybridgroup/gobot"
"testing"
"time"
"github.com/hybridgroup/gobot"
)
// --------- HELPERS
@ -37,6 +38,7 @@ func TestNewWiichuckDriver(t *testing.T) {
}
func TestWiichuckDriverStart(t *testing.T) {
sem := make(chan bool)
wii, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func() []byte {
@ -47,10 +49,23 @@ func TestWiichuckDriverStart(t *testing.T) {
wii.SetInterval(1 * time.Millisecond)
gobot.Assert(t, wii.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
gobot.Assert(t, wii.joystick["sy_origin"], float64(44))
gobot.Assert(t, wii.joystick["sx_origin"], float64(45))
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
if (wii.joystick["sy_origin"] == float64(44)) &&
(wii.joystick["sx_origin"] == float64(45)) {
sem <- true
}
}
}()
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("origin not read correctly")
}
}
func TestWiichuckDriverInit(t *testing.T) {