@@ -3,7 +3,6 @@ package aio
3
3
4
4
import (
5
5
"errors"
6
- "fmt"
7
6
"strings"
8
7
"testing"
9
8
"time"
@@ -123,7 +122,7 @@ func TestTemperatureSensorDriver_LinearScaler(t *testing.T) {
123
122
got , err := d .Read ()
124
123
// assert
125
124
require .NoError (t , err )
126
- assert .InDelta (t , tc .want , got , 0.0 )
125
+ assert .InDelta (t , tc .want , got , 1.0e-14 )
127
126
})
128
127
}
129
128
}
@@ -136,23 +135,39 @@ func TestTemperatureSensorWithSensorCyclicRead_PublishesTemperatureInCelsius(t *
136
135
ntc := TemperatureSensorNtcConf {TC0 : 25 , R0 : 10000.0 , B : 3975 } // Ohm, R25=10k
137
136
d .SetNtcScaler (1023 , 10000 , false , ntc ) // Ohm, reference value: 1023, series R: 10k
138
137
138
+ // 584: 31.52208881030674, 585: 31.61532462352477
139
+ lastRawValue := 585
139
140
a .analogReadFunc = func () (int , error ) {
140
- return 585 , nil
141
+ // ensure a changed value on each read, otherwise no event will be published
142
+ lastRawValue ++
143
+ if lastRawValue > 585 {
144
+ lastRawValue = 584
145
+ }
146
+ return lastRawValue , nil
141
147
}
142
148
149
+ // act: start cyclic reading
143
150
require .NoError (t , d .Start ())
151
+
152
+ // wait some time to ensure the cyclic go routine is working
153
+ time .Sleep (15 * time .Millisecond )
154
+
155
+ var eventValue float64
144
156
_ = d .Once (d .Event (Value ), func (data interface {}) {
145
- assert . Equal ( t , "31.62" , fmt . Sprintf ( "%.2f" , data .(float64 )) )
157
+ eventValue = data .(float64 )
146
158
sem <- true
147
159
})
148
160
161
+ // assert: value was published and is in expected delta
149
162
select {
150
163
case <- sem :
151
- case <- time .After (1 * time .Second ):
152
- require .Fail (t , " Temperature Sensor Event \" Data\" was not published" )
164
+ require .NoError (t , d .Halt ())
165
+ case <- time .After (100 * time .Millisecond ):
166
+ require .Fail (t , "Grove Temperature Sensor Event \" Value\" was not published" )
153
167
}
154
168
155
- assert .InDelta (t , 31.61532462352477 , d .Value (), 0.0 )
169
+ assert .InDelta (t , eventValue , d .Temperature (), 0.0 )
170
+ assert .InDelta (t , 31.61532462352477 , d .Value (), 31.61532462352477 - 31.52208881030674 )
156
171
}
157
172
158
173
func TestTemperatureSensorWithSensorCyclicRead_PublishesError (t * testing.T ) {
0 commit comments