From a94de04d96e243d7374dbd5a420dddde1dd9220e Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 7 Dec 2024 13:59:33 -0700 Subject: [PATCH] Fix test failures --- hdc302x/hdc302x.go | 10 +++++----- hdc302x/hdc302x_test.go | 9 +++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/hdc302x/hdc302x.go b/hdc302x/hdc302x.go index f16ee10..c74266e 100644 --- a/hdc302x/hdc302x.go +++ b/hdc302x/hdc302x.go @@ -159,7 +159,7 @@ var writeHighAlertThresholds = devCommand{0x61, 0x1d} var writeLowClearThresholds = devCommand{0x61, 0x0b} var writeHighClearThresholds = devCommand{0x61, 0x16} -var invalidCRCError = errors.New("hdc302x: invalid crc") +var errInvalidCRC = errors.New("hdc302x: invalid crc") const ( // Magic numbers for count to value conversions. @@ -252,7 +252,7 @@ func (dev *Dev) Sense(env *physic.Env) error { return fmt.Errorf("hdc302x: %w", err) } if crc8(res[:2]) != res[2] || crc8(res[3:5]) != res[5] { - return invalidCRCError + return errInvalidCRC } env.Temperature = countToTemperature(res) env.Humidity = countToHumidity(res[3:]) @@ -359,7 +359,7 @@ func (dev *Dev) readAlertValues(cfg *Configuration) error { return err } if crc8(r[:2]) != r[2] { - return invalidCRCError + return errInvalidCRC } wValue := uint16(r[0])<<8 | uint16(r[1]) // The alert value is returned as a 16 bit words, where bits 0-8 are the @@ -382,7 +382,7 @@ func (dev *Dev) readOffsets(cfg *Configuration) error { return fmt.Errorf("hdc302x: %w", err) } if crc8(r[:2]) != r[2] { - return invalidCRCError + return errInvalidCRC } // The result comes back as the humidity offset, followed by @@ -425,7 +425,7 @@ func (dev *Dev) ReadStatus() (StatusWord, error) { return 0, err } if crc8(r[:2]) != r[2] { - return 0, invalidCRCError + return 0, errInvalidCRC } _ = dev.d.Tx(clearStatus, nil) return StatusWord(r[0])<<8 | StatusWord(r[1]), nil diff --git a/hdc302x/hdc302x_test.go b/hdc302x/hdc302x_test.go index 761b25d..eb512d2 100644 --- a/hdc302x/hdc302x_test.go +++ b/hdc302x/hdc302x_test.go @@ -8,6 +8,7 @@ import ( "fmt" "math" "os" + "sync" "sync/atomic" "testing" "time" @@ -391,6 +392,8 @@ func TestSenseContinuous(t *testing.T) { counter := atomic.Int32{} tEnd := time.Now().UnixMilli() + int64(readCount+2)*1000 + wg := sync.WaitGroup{} + wg.Add(1) go func() { for { time.Sleep(100 * time.Millisecond) @@ -401,6 +404,7 @@ func TestSenseContinuous(t *testing.T) { if err != nil { t.Error(err) } + wg.Done() return } } @@ -413,6 +417,7 @@ func TestSenseContinuous(t *testing.T) { if counter.Load() != readCount { t.Errorf("expected %d readings. received %d", readCount, counter.Load()) } + wg.Wait() } func TestConfiguration(t *testing.T) { @@ -590,8 +595,8 @@ func TestHeater(t *testing.T) { t.Logf("initial temperature: %s Humidity: %s", env.Temperature, env.Humidity) err = dev.SetHeater(PowerFull) defer func() { - if err := dev.SetHeater(PowerOff); err != nil { - t.Error(err) + if errOff := dev.SetHeater(PowerOff); errOff != nil { + t.Error(errOff) } }() if err != nil {