mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-09 19:29:27 +08:00
Fix more inconsistent tests
This commit is contained in:
parent
56d396339b
commit
fd3e7866a9
@ -1,9 +1,10 @@
|
|||||||
package i2c
|
package i2c
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
// --------- HELPERS
|
// --------- HELPERS
|
||||||
@ -39,6 +40,7 @@ func TestNewHMC6352Driver(t *testing.T) {
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
func TestHMC6352DriverStart(t *testing.T) {
|
func TestHMC6352DriverStart(t *testing.T) {
|
||||||
|
sem := make(chan bool)
|
||||||
// when len(data) is 2
|
// when len(data) is 2
|
||||||
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
|
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
|
||||||
|
|
||||||
@ -54,9 +56,20 @@ func TestHMC6352DriverStart(t *testing.T) {
|
|||||||
|
|
||||||
hmc.SetInterval(1 * time.Millisecond)
|
hmc.SetInterval(1 * time.Millisecond)
|
||||||
gobot.Assert(t, hmc.Start(), true)
|
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
|
// when len(data) is not 2
|
||||||
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
|
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
|
||||||
@ -67,9 +80,20 @@ func TestHMC6352DriverStart(t *testing.T) {
|
|||||||
|
|
||||||
hmc.SetInterval(1 * time.Millisecond)
|
hmc.SetInterval(1 * time.Millisecond)
|
||||||
gobot.Assert(t, hmc.Start(), true)
|
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) {
|
func TestHMC6352DriverInit(t *testing.T) {
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
package i2c
|
package i2c
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
// --------- HELPERS
|
// --------- HELPERS
|
||||||
@ -37,6 +38,7 @@ func TestNewWiichuckDriver(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWiichuckDriverStart(t *testing.T) {
|
func TestWiichuckDriverStart(t *testing.T) {
|
||||||
|
sem := make(chan bool)
|
||||||
wii, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
|
wii, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
|
||||||
|
|
||||||
adaptor.i2cReadImpl = func() []byte {
|
adaptor.i2cReadImpl = func() []byte {
|
||||||
@ -47,10 +49,23 @@ func TestWiichuckDriverStart(t *testing.T) {
|
|||||||
|
|
||||||
wii.SetInterval(1 * time.Millisecond)
|
wii.SetInterval(1 * time.Millisecond)
|
||||||
gobot.Assert(t, wii.Start(), true)
|
gobot.Assert(t, wii.Start(), true)
|
||||||
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
|
|
||||||
|
|
||||||
gobot.Assert(t, wii.joystick["sy_origin"], float64(44))
|
go func() {
|
||||||
gobot.Assert(t, wii.joystick["sx_origin"], float64(45))
|
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) {
|
func TestWiichuckDriverInit(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user