mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
Uses NewTimer() instead of time.After() to be more
efficient since the later creates new timers in a tight loop. - Adds more assertions to tests - Adds some comments to help contributors understand that the send occurs after the Once. Signed-off-by: Warren Fernandes <warren.f.fernandes@gmail.com>
This commit is contained in:
parent
54d9b16141
commit
922e6d98f7
@ -58,6 +58,8 @@ func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *Anal
|
|||||||
func (a *AnalogSensorDriver) Start() (errs []error) {
|
func (a *AnalogSensorDriver) Start() (errs []error) {
|
||||||
value := 0
|
value := 0
|
||||||
go func() {
|
go func() {
|
||||||
|
timer := time.NewTimer(a.interval)
|
||||||
|
timer.Stop()
|
||||||
for {
|
for {
|
||||||
newValue, err := a.Read()
|
newValue, err := a.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,9 +68,12 @@ func (a *AnalogSensorDriver) Start() (errs []error) {
|
|||||||
value = newValue
|
value = newValue
|
||||||
a.Publish(a.Event(Data), value)
|
a.Publish(a.Event(Data), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timer.Reset(a.interval)
|
||||||
select {
|
select {
|
||||||
case <-time.After(a.interval):
|
case <-timer.C:
|
||||||
case <-a.halt:
|
case <-a.halt:
|
||||||
|
timer.Stop()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,8 +14,11 @@ var _ gobot.Driver = (*AnalogSensorDriver)(nil)
|
|||||||
func TestAnalogSensorDriver(t *testing.T) {
|
func TestAnalogSensorDriver(t *testing.T) {
|
||||||
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
|
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
|
||||||
gobottest.Refute(t, d.Connection(), nil)
|
gobottest.Refute(t, d.Connection(), nil)
|
||||||
|
// default interval
|
||||||
|
gobottest.Assert(t, d.interval, 10*time.Millisecond)
|
||||||
|
|
||||||
d = NewAnalogSensorDriver(newGpioTestAdaptor(), "1", 30*time.Second)
|
d = NewAnalogSensorDriver(newGpioTestAdaptor(), "42", 30*time.Second)
|
||||||
|
gobottest.Assert(t, d.Pin(), "42")
|
||||||
gobottest.Assert(t, d.interval, 30*time.Second)
|
gobottest.Assert(t, d.interval, 30*time.Second)
|
||||||
|
|
||||||
testAdaptorAnalogRead = func() (val int, err error) {
|
testAdaptorAnalogRead = func() (val int, err error) {
|
||||||
@ -33,18 +36,15 @@ func TestAnalogSensorDriverStart(t *testing.T) {
|
|||||||
|
|
||||||
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
|
d := NewAnalogSensorDriver(newGpioTestAdaptor(), "1")
|
||||||
|
|
||||||
testAdaptorAnalogRead = func() (val int, err error) {
|
|
||||||
val = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
gobottest.Assert(t, len(d.Start()), 0)
|
gobottest.Assert(t, len(d.Start()), 0)
|
||||||
|
|
||||||
// data was received
|
// expect data to be received
|
||||||
d.Once(d.Event(Data), func(data interface{}) {
|
d.Once(d.Event(Data), func(data interface{}) {
|
||||||
gobottest.Assert(t, data.(int), 100)
|
gobottest.Assert(t, data.(int), 100)
|
||||||
sem <- true
|
sem <- true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// send data
|
||||||
testAdaptorAnalogRead = func() (val int, err error) {
|
testAdaptorAnalogRead = func() (val int, err error) {
|
||||||
val = 100
|
val = 100
|
||||||
return
|
return
|
||||||
@ -56,12 +56,13 @@ func TestAnalogSensorDriverStart(t *testing.T) {
|
|||||||
t.Errorf("AnalogSensor Event \"Data\" was not published")
|
t.Errorf("AnalogSensor Event \"Data\" was not published")
|
||||||
}
|
}
|
||||||
|
|
||||||
// read error
|
// expect error to be received
|
||||||
d.Once(d.Event(Error), func(data interface{}) {
|
d.Once(d.Event(Error), func(data interface{}) {
|
||||||
gobottest.Assert(t, data.(error).Error(), "read error")
|
gobottest.Assert(t, data.(error).Error(), "read error")
|
||||||
sem <- true
|
sem <- true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// send error
|
||||||
testAdaptorAnalogRead = func() (val int, err error) {
|
testAdaptorAnalogRead = func() (val int, err error) {
|
||||||
err = errors.New("read error")
|
err = errors.New("read error")
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user