Skip to content

Commit

Permalink
test: try to stabilize eventer tests (#1066)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen2thomas authored Feb 13, 2024
1 parent 244f699 commit 026fd03
Show file tree
Hide file tree
Showing 68 changed files with 140 additions and 133 deletions.
8 changes: 4 additions & 4 deletions drivers/aio/analog_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestAnalogSensor_WithSensorCyclicRead(t *testing.T) {
select {
case <-semDone:
case <-time.After(readTimeout):
t.Errorf("AnalogSensor Event \"Data\" was not published")
require.Fail(t, "AnalogSensor Event \"Data\" was not published")
}

// arrange: for error to be received
Expand All @@ -206,7 +206,7 @@ func TestAnalogSensor_WithSensorCyclicRead(t *testing.T) {
select {
case <-semDone:
case <-time.After(readTimeout):
t.Errorf("AnalogSensor Event \"Error\" was not published")
require.Fail(t, "AnalogSensor Event \"Error\" was not published")
}

// arrange: for halt message
Expand All @@ -224,9 +224,9 @@ func TestAnalogSensor_WithSensorCyclicRead(t *testing.T) {
// assert: no event
select {
case <-semData:
t.Errorf("AnalogSensor Event for data should not published")
require.Fail(t, "AnalogSensor Event for data should not published")
case <-semDone:
t.Errorf("AnalogSensor Event for value should not published")
require.Fail(t, "AnalogSensor Event for value should not published")
case <-time.After(readTimeout):
}
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/aio/grove_drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestGroveDriverHalt_WithSensorCyclicRead(t *testing.T) {
time.Sleep(20 * time.Millisecond)
// note: if a reading is already in progress, it will be finished before halt have an impact
if atomic.LoadInt32(&callCount) > lastCallCount+1 {
t.Errorf("AnalogRead was called more than once after driver was halted")
require.Fail(t, "AnalogRead was called more than once after driver was halted")
}
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestGroveDriverWithSensorCyclicReadPublishesError(t *testing.T) {
select {
case <-sem:
case <-time.After(time.Second):
t.Errorf("%s Event \"Error\" was not published", groveGetType(driver))
require.Fail(t, "%s Event \"Error\" was not published", groveGetType(driver))
}

// Cleanup
Expand Down
2 changes: 1 addition & 1 deletion drivers/aio/grove_temperature_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestGroveTemperatureSensor_publishesTemperatureInCelsius(t *testing.T) {
select {
case <-sem:
case <-time.After(1 * time.Second):
t.Errorf("Grove Temperature Sensor Event \"Value\" was not published")
require.Fail(t, "Grove Temperature Sensor Event \"Value\" was not published")
}

assert.InDelta(t, 31.61532462352477, d.Temperature(), 0.0)
Expand Down
6 changes: 3 additions & 3 deletions drivers/aio/temperature_sensor_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestTemperatureSensorWithSensorCyclicRead_PublishesTemperatureInCelsius(t *
select {
case <-sem:
case <-time.After(1 * time.Second):
t.Errorf(" Temperature Sensor Event \"Data\" was not published")
require.Fail(t, " Temperature Sensor Event \"Data\" was not published")
}

assert.InDelta(t, 31.61532462352477, d.Value(), 0.0)
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestTemperatureSensorWithSensorCyclicRead_PublishesError(t *testing.T) {
select {
case <-sem:
case <-time.After(1 * time.Second):
t.Errorf(" Temperature Sensor Event \"Error\" was not published")
require.Fail(t, " Temperature Sensor Event \"Error\" was not published")
}
}

Expand All @@ -195,7 +195,7 @@ func TestTemperatureSensorHalt_WithSensorCyclicRead(t *testing.T) {
select {
case <-done:
case <-time.After(100 * time.Millisecond):
t.Errorf("Temperature Sensor was not halted")
require.Fail(t, "Temperature Sensor was not halted")
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/aio/thermalzone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestThermalZoneWithSensorCyclicRead_PublishesTemperatureInFahrenheit(t *tes
select {
case <-sem:
case <-time.After(1 * time.Second):
t.Errorf(" Temperature Sensor Event \"Data\" was not published")
require.Fail(t, " Temperature Sensor Event \"Data\" was not published")
}

assert.InDelta(t, -148.0, d.Value(), 0.0)
Expand Down
2 changes: 1 addition & 1 deletion drivers/ble/microbit/accelerometer_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ func TestAccelerometerReadData(t *testing.T) {
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Microbit Event \"Accelerometer\" was not published")
require.Fail(t, "Microbit Event \"Accelerometer\" was not published")
}
}
2 changes: 1 addition & 1 deletion drivers/ble/microbit/button_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func TestButtonReadData(t *testing.T) {
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Microbit Event \"ButtonB\" was not published")
require.Fail(t, "Microbit Event \"ButtonB\" was not published")
}
}
2 changes: 1 addition & 1 deletion drivers/ble/microbit/magnetometer_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ func TestMagnetometerReadData(t *testing.T) {
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Microbit Event \"Magnetometer\" was not published")
require.Fail(t, "Microbit Event \"Magnetometer\" was not published")
}
}
2 changes: 1 addition & 1 deletion drivers/ble/microbit/temperature_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ func TestTemperatureReadData(t *testing.T) {
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Microbit Event \"Temperature\" was not published")
require.Fail(t, "Microbit Event \"Temperature\" was not published")
}
}
8 changes: 6 additions & 2 deletions drivers/common/spherocommon/spherocommon_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package spherocommon

import "testing"
import (
"testing"

"github.com/stretchr/testify/require"
)

func TestCalculateChecksum(t *testing.T) {
tests := []struct {
Expand All @@ -14,7 +18,7 @@ func TestCalculateChecksum(t *testing.T) {
for _, tt := range tests {
actual := CalculateChecksum(tt.data)
if actual != tt.checksum {
t.Errorf("Expected %x, got %x for data %x.", tt.checksum, actual, tt.data)
require.Fail(t, "Expected %x, got %x for data %x.", tt.checksum, actual, tt.data)
}
}
}
4 changes: 2 additions & 2 deletions drivers/gpio/grove_drivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestDigitalDriverHalt(t *testing.T) {
time.Sleep(20 * time.Millisecond)
// note: if a reading is already in progress, it will be finished before halt have an impact
if atomic.LoadInt32(&callCount) > lastCallCount+1 {
t.Errorf("DigitalRead was called more than once after driver was halted")
require.Fail(t, "DigitalRead was called more than once after driver was halted")
}
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestDriverPublishesError(t *testing.T) {
select {
case <-sem:
case <-time.After(time.Second):
t.Errorf("%s Event \"Error\" was not published", getType(driver))
require.Fail(t, "%s Event \"Error\" was not published", getType(driver))
}

// Cleanup
Expand Down
8 changes: 4 additions & 4 deletions drivers/gpio/pir_motion_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestPIRMotionStart(t *testing.T) {
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"MotionDetected\" was not published")
require.Fail(t, "PIRMotionDriver Event \"MotionDetected\" was not published")
}

_ = d.Once(MotionStopped, func(data interface{}) {
Expand All @@ -116,7 +116,7 @@ func TestPIRMotionStart(t *testing.T) {
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"MotionStopped\" was not published")
require.Fail(t, "PIRMotionDriver Event \"MotionStopped\" was not published")
}

_ = d.Once(Error, func(data interface{}) {
Expand All @@ -126,7 +126,7 @@ func TestPIRMotionStart(t *testing.T) {
select {
case <-sem:
case <-time.After(motionTestDelay * time.Millisecond):
t.Errorf("PIRMotionDriver Event \"Error\" was not published")
require.Fail(t, "PIRMotionDriver Event \"Error\" was not published")
}

_ = d.Once(MotionDetected, func(data interface{}) {
Expand All @@ -138,7 +138,7 @@ func TestPIRMotionStart(t *testing.T) {

select {
case <-sem:
t.Errorf("PIRMotion Event \"MotionDetected\" should not published")
require.Fail(t, "PIRMotion Event \"MotionDetected\" should not published")
case <-time.After(motionTestDelay * time.Millisecond):
}
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/adafruit1109_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewAdafruit1109Driver(t *testing.T) {
var di interface{} = NewAdafruit1109Driver(newI2cTestAdaptor())
d, ok := di.(*Adafruit1109Driver)
if !ok {
t.Errorf("NewAdafruit1109Driver() should have returned a *Adafruit1109Driver")
require.Fail(t, "NewAdafruit1109Driver() should have returned a *Adafruit1109Driver")
}
assert.NotNil(t, d.Driver)
assert.NotNil(t, d.Connection())
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ads1x15_driver_1015_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewADS1015Driver(t *testing.T) {
var di interface{} = NewADS1015Driver(newI2cTestAdaptor())
d, ok := di.(*ADS1x15Driver)
if !ok {
t.Errorf("NewADS1015Driver() should have returned a *ADS1x15Driver")
require.Fail(t, "NewADS1015Driver() should have returned a *ADS1x15Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "ADS1015"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ads1x15_driver_1115_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewADS1115Driver(t *testing.T) {
var di interface{} = NewADS1115Driver(newI2cTestAdaptor())
d, ok := di.(*ADS1x15Driver)
if !ok {
t.Errorf("NewADS1115Driver() should have returned a *ADS1x15Driver")
require.Fail(t, "NewADS1115Driver() should have returned a *ADS1x15Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "ADS1115"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/adxl345_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewADXL345Driver(t *testing.T) {
var di interface{} = NewADXL345Driver(newI2cTestAdaptor())
d, ok := di.(*ADXL345Driver)
if !ok {
t.Errorf("NewADXL345Driver() should have returned a *ADXL345Driver")
require.Fail(t, "NewADXL345Driver() should have returned a *ADXL345Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "ADXL345"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/bh1750_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewBH1750Driver(t *testing.T) {
var di interface{} = NewBH1750Driver(newI2cTestAdaptor())
d, ok := di.(*BH1750Driver)
if !ok {
t.Errorf("NewBH1750Driver() should have returned a *BH1750Driver")
require.Fail(t, "NewBH1750Driver() should have returned a *BH1750Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BH1750"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/blinkm_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNewBlinkMDriver(t *testing.T) {
var di interface{} = NewBlinkMDriver(newI2cTestAdaptor())
d, ok := di.(*BlinkMDriver)
if !ok {
t.Errorf("NewBlinkMDriver() should have returned a *BlinkMDriver")
require.Fail(t, "NewBlinkMDriver() should have returned a *BlinkMDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BlinkM"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/bme280_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewBME280Driver(t *testing.T) {
var di interface{} = NewBME280Driver(newI2cTestAdaptor())
d, ok := di.(*BME280Driver)
if !ok {
t.Errorf("NewBME280Driver() should have returned a *BME280Driver")
require.Fail(t, "NewBME280Driver() should have returned a *BME280Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BMP280"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/bmp180_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewBMP180Driver(t *testing.T) {
var di interface{} = NewBMP180Driver(newI2cTestAdaptor())
d, ok := di.(*BMP180Driver)
if !ok {
t.Errorf("NewBMP180Driver() should have returned a *BMP180Driver")
require.Fail(t, "NewBMP180Driver() should have returned a *BMP180Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BMP180"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/bmp280_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewBMP280Driver(t *testing.T) {
var di interface{} = NewBMP280Driver(newI2cTestAdaptor())
d, ok := di.(*BMP280Driver)
if !ok {
t.Errorf("NewBMP280Driver() should have returned a *BMP280Driver")
require.Fail(t, "NewBMP280Driver() should have returned a *BMP280Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BMP280"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/bmp388_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestNewBMP388Driver(t *testing.T) {
var di interface{} = NewBMP388Driver(newI2cTestAdaptor())
d, ok := di.(*BMP388Driver)
if !ok {
t.Errorf("NewBMP388Driver() should have returned a *BMP388Driver")
require.Fail(t, "NewBMP388Driver() should have returned a *BMP388Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BMP388"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/ccs811_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewCCS811Driver(t *testing.T) {
var di interface{} = NewCCS811Driver(newI2cTestAdaptor())
d, ok := di.(*CCS811Driver)
if !ok {
t.Errorf("NewCCS811Driver() should have returned a *CCS811Driver")
require.Fail(t, "NewCCS811Driver() should have returned a *CCS811Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "CCS811"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/drv2605l_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestNewDRV2605LDriver(t *testing.T) {
var di interface{} = NewDRV2605LDriver(newI2cTestAdaptor())
d, ok := di.(*DRV2605LDriver)
if !ok {
t.Errorf("NewDRV2605LDriver() should have returned a *DRV2605LDriver")
require.Fail(t, "NewDRV2605LDriver() should have returned a *DRV2605LDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "DRV2605L"))
Expand Down
3 changes: 2 additions & 1 deletion drivers/i2c/generic_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"gobot.io/x/gobot/v2"
)
Expand All @@ -19,7 +20,7 @@ func TestNewGenericDriver(t *testing.T) {
// assert
d, ok := di.(*GenericDriver)
if !ok {
t.Errorf("NewGenericDriver() should have returned a *GenericDriver")
require.Fail(t, "NewGenericDriver() should have returned a *GenericDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "GenericI2C"))
Expand Down
6 changes: 3 additions & 3 deletions drivers/i2c/grovepi_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNewGrovePiDriver(t *testing.T) {
var di interface{} = NewGrovePiDriver(newI2cTestAdaptor())
d, ok := di.(*GrovePiDriver)
if !ok {
t.Errorf("NewGrovePiDriver() should have returned a *GrovePiDriver")
require.Fail(t, "NewGrovePiDriver() should have returned a *GrovePiDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "GrovePi"))
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestGrovePiSomeRead(t *testing.T) {
case strings.Contains(name, "DHTRead"):
gotF1, gotF2, err = g.DHTRead(strconv.Itoa(tc.usedPin), 1, 2)
default:
t.Errorf("unknown command %s", name)
require.Fail(t, "unknown command %s", name)
return
}
// assert
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestGrovePiSomeWrite(t *testing.T) {
case "AnalogWrite":
err = g.AnalogWrite(strconv.Itoa(tc.usedPin), tc.usedValue)
default:
t.Errorf("unknown command %s", name)
require.Fail(t, "unknown command %s", name)
return
}
// assert
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/hmc5883l_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewHMC5883LDriver(t *testing.T) {
var di interface{} = NewHMC5883LDriver(newI2cTestAdaptor())
d, ok := di.(*HMC5883LDriver)
if !ok {
t.Errorf("NewHMC5883LDriver() should have returned a *HMC5883LDriver")
require.Fail(t, "NewHMC5883LDriver() should have returned a *HMC5883LDriver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.name, "HMC5883L"))
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/hmc6352_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewHMC6352Driver(t *testing.T) {
var di interface{} = NewHMC6352Driver(newI2cTestAdaptor())
d, ok := di.(*HMC6352Driver)
if !ok {
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
require.Fail(t, "NewHMC6352Driver() should have returned a *HMC6352Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "HMC6352"))
Expand Down
3 changes: 2 additions & 1 deletion drivers/i2c/i2c_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewConfig(t *testing.T) {
Expand All @@ -13,7 +14,7 @@ func TestNewConfig(t *testing.T) {
// assert
c, ok := ci.(*i2cConfig)
if !ok {
t.Errorf("NewConfig() should have returned a *i2cConfig")
require.Fail(t, "NewConfig() should have returned a *i2cConfig")
}
assert.Equal(t, BusNotInitialized, c.bus)
assert.Equal(t, AddressNotInitialized, c.address)
Expand Down
2 changes: 1 addition & 1 deletion drivers/i2c/i2c_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestNewDriver(t *testing.T) {
// assert
d, ok := di.(*Driver)
if !ok {
t.Errorf("NewDriver() should have returned a *Driver")
require.Fail(t, "NewDriver() should have returned a *Driver")
}
assert.Contains(t, d.name, "I2C_BASIC")
assert.Equal(t, 0x15, d.defaultAddress)
Expand Down
Loading

0 comments on commit 026fd03

Please sign in to comment.