1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

gpio: eliminate race conditions introduced by tests

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-02 17:42:01 +02:00
parent b6c8138551
commit fe94979487
4 changed files with 63 additions and 25 deletions

View File

@ -45,10 +45,10 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
}
})
select {
case <-sem:
@ -61,10 +61,10 @@ func TestButtonDriverStart(t *testing.T) {
sem <- true
})
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 0
return
}
})
select {
case <-sem:
@ -72,10 +72,10 @@ func TestButtonDriverStart(t *testing.T) {
t.Errorf("Button Event \"Release\" was not published")
}
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
err = errors.New("digital read error")
return
}
})
d.Once(Error, func(data interface{}) {
sem <- true
@ -87,10 +87,10 @@ func TestButtonDriverStart(t *testing.T) {
t.Errorf("Button Event \"Error\" was not published")
}
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
}
})
d.Once(ButtonPush, func(data interface{}) {
sem <- true
@ -103,7 +103,6 @@ func TestButtonDriverStart(t *testing.T) {
t.Errorf("Button Event \"Press\" should not published")
case <-time.After(BUTTON_TEST_DELAY * time.Millisecond):
}
}
func TestButtonDriverDefaultName(t *testing.T) {

View File

@ -1,5 +1,7 @@
package gpio
import "sync"
type gpioTestBareAdaptor struct{}
func (t *gpioTestBareAdaptor) Connect() (err error) { return }
@ -16,6 +18,7 @@ func (t *gpioTestDigitalWriter) DigitalWrite(string, byte) (err error) { return
type gpioTestAdaptor struct {
name string
port string
mtx sync.Mutex
testAdaptorDigitalWrite func() (err error)
testAdaptorServoWrite func() (err error)
testAdaptorPwmWrite func() (err error)
@ -23,21 +26,57 @@ type gpioTestAdaptor struct {
testAdaptorDigitalRead func() (val int, err error)
}
func (t *gpioTestAdaptor) DigitalWrite(string, byte) (err error) {
return t.testAdaptorDigitalWrite()
func (t *gpioTestAdaptor) TestAdaptorDigitalWrite(f func() (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorDigitalWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorServoWrite(f func() (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorServoWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorPwmWrite(f func() (err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorPwmWrite = f
}
func (t *gpioTestAdaptor) TestAdaptorAnalogRead(f func() (val int, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorAnalogRead = f
}
func (t *gpioTestAdaptor) TestAdaptorDigitalRead(f func() (val int, err error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.testAdaptorDigitalRead = f
}
func (t *gpioTestAdaptor) ServoWrite(string, byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorServoWrite()
}
func (t *gpioTestAdaptor) PwmWrite(string, byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorPwmWrite()
}
func (t *gpioTestAdaptor) AnalogRead(string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorAnalogRead()
}
func (t *gpioTestAdaptor) DigitalRead(string) (val int, err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorDigitalRead()
}
func (t *gpioTestAdaptor) DigitalWrite(string, byte) (err error) {
t.mtx.Lock()
defer t.mtx.Unlock()
return t.testAdaptorDigitalWrite()
}
func (t *gpioTestAdaptor) Connect() (err error) { return }
func (t *gpioTestAdaptor) Finalize() (err error) { return }
func (t *gpioTestAdaptor) Name() string { return t.name }

View File

@ -49,10 +49,10 @@ func TestMakeyButtonDriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 0
return
}
})
d.Once(ButtonPush, func(data interface{}) {
gobottest.Assert(t, d.Active, true)
@ -65,10 +65,10 @@ func TestMakeyButtonDriverStart(t *testing.T) {
t.Errorf("MakeyButton Event \"Push\" was not published")
}
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
}
})
d.Once(ButtonRelease, func(data interface{}) {
gobottest.Assert(t, d.Active, false)
@ -81,10 +81,10 @@ func TestMakeyButtonDriverStart(t *testing.T) {
t.Errorf("MakeyButton Event \"Release\" was not published")
}
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
err = errors.New("digital read error")
return
}
})
d.Once(Error, func(data interface{}) {
gobottest.Assert(t, data.(error).Error(), "digital read error")
@ -102,10 +102,10 @@ func TestMakeyButtonDriverStart(t *testing.T) {
sem <- true
})
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
}
})
d.halt <- true

View File

@ -46,10 +46,10 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 1
return
}
})
select {
case <-sem:
@ -62,10 +62,10 @@ func TestPIRMotionDriverStart(t *testing.T) {
sem <- true
})
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
val = 0
return
}
})
select {
case <-sem:
@ -73,10 +73,10 @@ func TestPIRMotionDriverStart(t *testing.T) {
t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
}
a.testAdaptorDigitalRead = func() (val int, err error) {
a.TestAdaptorDigitalRead(func() (val int, err error) {
err = errors.New("digital read error")
return
}
})
d.Once(Error, func(data interface{}) {
sem <- true