Skip to content

Commit

Permalink
Fix race conditions in test SenseContinuous()
Browse files Browse the repository at this point in the history
  • Loading branch information
gsexton committed Dec 7, 2024
1 parent a94de04 commit 890d624
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions scd4x/scd4x_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package scd4x
import (
"fmt"
"os"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -235,7 +237,7 @@ func TestSense(t *testing.T) {
}

func TestSenseContinuous(t *testing.T) {
readings := 6
readings := int32(6)
timeBase := time.Second
if liveDevice {
timeBase *= 10
Expand All @@ -244,26 +246,36 @@ func TestSenseContinuous(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer func() { _ = dev.Halt() }()

defer shutdown(t)
t.Log("dev.sensing=", dev.sensing)
ch, err := dev.SenseContinuous(timeBase)
if err != nil {
t.Error(err)
}

received := atomic.Int32{}
wg := sync.WaitGroup{}
wg.Add(1)
tEnd := time.Now().UnixMilli() + int64(readings+2)*1000
go func() {
time.Sleep(time.Duration(readings) * timeBase)
_ = dev.Halt()
for {
if received.Load() == readings || time.Now().UnixMilli() > tEnd {
_ = dev.Halt()
wg.Done()
break
}
}
}()
received := 0

for env := range ch {
received.Add(1)
t.Log(env.String())
received += 1

}
if received < (readings-1) || received > readings {
t.Errorf("SenseContinuous() expected at least %d readings, got %d", readings-1, received)
if received.Load() != readings {
t.Errorf("SenseContinuous() expected at least %d readings, got %d", readings-1, received.Load())
}
wg.Wait()

}

Expand Down Expand Up @@ -335,7 +347,7 @@ func TestGetSetConfiguration(t *testing.T) {
// previously programmed into the device.
func TestPersistAndResetFactory(t *testing.T) {
if !liveDevice || os.Getenv("SCDRESET") == "" {
t.Skip("using live device and SCDRESET not defined. skipping")
t.Skip("not using live device or SCDRESET not defined. skipping")
}
dev, err := getDev(t)
if err != nil {
Expand Down

0 comments on commit 890d624

Please sign in to comment.