Remove all time.Sleep calls during tests (#204)

It slows down tests, nobody wants that.
pull/1/head
M-A 9 years ago committed by GitHub
parent 089b8dccab
commit b46e37feba

@ -19,7 +19,7 @@ func (d *Dev) sense180(env *devices.Environment) error {
if err := d.writeCommands([]byte{0xF4, 0x20 | 0x0E}); err != nil { if err := d.writeCommands([]byte{0xF4, 0x20 | 0x0E}); err != nil {
return d.wrap(err) return d.wrap(err)
} }
time.Sleep(4500 * time.Microsecond) doSleep(4500 * time.Microsecond)
var tempBuf [2]byte var tempBuf [2]byte
if err := d.readReg(0xF6, tempBuf[:]); err != nil { if err := d.readReg(0xF6, tempBuf[:]); err != nil {
return d.wrap(err) return d.wrap(err)
@ -31,7 +31,7 @@ func (d *Dev) sense180(env *devices.Environment) error {
if err := d.writeCommands([]byte{0xF4, 0x20 | 0x14 | d.os<<6}); err != nil { if err := d.writeCommands([]byte{0xF4, 0x20 | 0x14 | d.os<<6}); err != nil {
return d.wrap(err) return d.wrap(err)
} }
time.Sleep(pressureConvTime180[d.os]) doSleep(pressureConvTime180[d.os])
var pressureBuf [3]byte var pressureBuf [3]byte
if err := d.readReg(0xF6, pressureBuf[:]); err != nil { if err := d.readReg(0xF6, pressureBuf[:]); err != nil {
return d.wrap(err) return d.wrap(err)

@ -377,3 +377,7 @@ func TestCompensate180(t *testing.T) {
t.Errorf("pressure is wrong, want %v, got %v", 69964, pressure) t.Errorf("pressure is wrong, want %v, got %v", 69964, pressure)
} }
} }
func init() {
doSleep = func(time.Duration) {}
}

@ -167,7 +167,7 @@ func (d *Dev) Sense(env *devices.Environment) error {
if err != nil { if err != nil {
return d.wrap(err) return d.wrap(err)
} }
time.Sleep(d.measDelay) doSleep(d.measDelay)
for idle := false; !idle; { for idle := false; !idle; {
if idle, err = d.isIdle280(); err != nil { if idle, err = d.isIdle280(); err != nil {
return d.wrap(err) return d.wrap(err)
@ -514,6 +514,8 @@ var defaults = Opts{
Humidity: O4x, Humidity: O4x,
} }
var doSleep = time.Sleep
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ devices.Environmental = &Dev{} var _ devices.Environmental = &Dev{}
var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &Dev{}

@ -66,7 +66,7 @@ func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error)
return nil, err return nil, err
} }
// Wait for the write to complete. // Wait for the write to complete.
time.Sleep(10 * time.Millisecond) sleep(10 * time.Millisecond)
} }
return d, nil return d, nil
@ -158,7 +158,7 @@ func (e busError) BusError() bool { return true }
// on the resolution: // on the resolution:
// 9bits:94ms, 10bits:188ms, 11bits:376ms, 12bits:752ms, datasheet p.6. // 9bits:94ms, 10bits:188ms, 11bits:376ms, 12bits:752ms, datasheet p.6.
func conversionSleep(bits int) { func conversionSleep(bits int) {
time.Sleep((94 << uint(bits-9)) * time.Millisecond) sleep((94 << uint(bits-9)) * time.Millisecond)
} }
// readScratchpad reads the 9 bytes of scratchpad and checks the CRC. // readScratchpad reads the 9 bytes of scratchpad and checks the CRC.
@ -183,5 +183,7 @@ func (d *Dev) readScratchpad() ([]byte, error) {
return spad[:8], nil return spad[:8], nil
} }
var sleep = time.Sleep
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &Dev{}

@ -5,6 +5,7 @@
package ds18b20 package ds18b20
import ( import (
"reflect"
"testing" "testing"
"time" "time"
@ -61,9 +62,10 @@ func TestTemperature(t *testing.T) {
t.Fatal(s) t.Fatal(s)
} }
// Read the temperature. // Read the temperature.
t0 := time.Now() var sleeps []time.Duration
sleep = func(d time.Duration) { sleeps = append(sleeps, d) }
defer func() { sleep = func(time.Duration) {} }()
now, err := dev.Temperature() now, err := dev.Temperature()
dt := time.Since(t0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -72,8 +74,8 @@ func TestTemperature(t *testing.T) {
t.Errorf("expected %s, got %s", temp.String(), now.String()) t.Errorf("expected %s, got %s", temp.String(), now.String())
} }
// Expect it to take >187ms // Expect it to take >187ms
if dt < 188*time.Millisecond { if !reflect.DeepEqual(sleeps, []time.Duration{188 * time.Millisecond}) {
t.Errorf("expected conversion to take >187ms, took %s", dt) t.Errorf("expected conversion to sleep: %v", sleeps)
} }
if err := dev.Halt(); err != nil { if err := dev.Halt(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -93,13 +95,15 @@ func TestConvertAll(t *testing.T) {
} }
bus := onewiretest.Playback{Ops: ops} bus := onewiretest.Playback{Ops: ops}
// Perform the conversion // Perform the conversion
t0 := time.Now() var sleeps []time.Duration
sleep = func(d time.Duration) { sleeps = append(sleeps, d) }
defer func() { sleep = func(time.Duration) {} }()
if err := ConvertAll(&bus, 9); err != nil { if err := ConvertAll(&bus, 9); err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Expect it to take >93ms // Expect it to take >93ms
if dt := time.Since(t0); dt < 94*time.Millisecond { if !reflect.DeepEqual(sleeps, []time.Duration{94 * time.Millisecond}) {
t.Errorf("expected conversion to take >93ms, took %s", dt) t.Errorf("expected conversion to take >93ms, took %s", sleeps)
} }
if err := bus.Close(); err != nil { if err := bus.Close(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -120,6 +124,10 @@ func TestConvertAll_fail_io(t *testing.T) {
} }
} }
func init() {
sleep = func(time.Duration) {}
}
/* Commented out in order not to import periph/host, need to move to smoke test /* Commented out in order not to import periph/host, need to move to smoke test
// TestRecordTemp tests and records a temperature conversion. It outputs // TestRecordTemp tests and records a temperature conversion. It outputs
// the recording if the tests are run with the verbose option. // the recording if the tests are run with the verbose option.

@ -162,7 +162,7 @@ func (d *Dev) waitIdle(delay time.Duration) byte {
} }
// Overall timeout. // Overall timeout.
tOut := time.Now().Add(3 * time.Millisecond) tOut := time.Now().Add(3 * time.Millisecond)
time.Sleep(delay) sleep(delay)
for { for {
// Read status register. // Read status register.
var status [1]byte var status [1]byte
@ -179,7 +179,7 @@ func (d *Dev) waitIdle(delay time.Duration) byte {
return 0 return 0
} }
// Try not to hog the kernel thread. // Try not to hog the kernel thread.
time.Sleep(delay / 10) sleep(delay / 10)
} }
} }
@ -196,5 +196,7 @@ type busError string
func (e busError) Error() string { return string(e) } func (e busError) Error() string { return string(e) }
func (e busError) BusError() bool { return true } func (e busError) BusError() bool { return true }
var sleep = time.Sleep
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &Dev{}

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"log" "log"
"testing" "testing"
"time"
"periph.io/x/periph/conn/i2c/i2creg" "periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/conn/i2c/i2ctest" "periph.io/x/periph/conn/i2c/i2ctest"
@ -84,6 +85,10 @@ func TestNew_opts(t *testing.T) {
} }
} }
func init() {
sleep = func(time.Duration) {}
}
/* Commented out in order not to import periph/host, need to move to smoke test /* Commented out in order not to import periph/host, need to move to smoke test
// TestRecordInit tests and records the initialization of a ds248x by accessing // TestRecordInit tests and records the initialization of a ds248x by accessing
// real hardware and outputs the recording ready to use for playback in // real hardware and outputs the recording ready to use for playback in

@ -169,7 +169,7 @@ func New(i i2c.Bus) (*Dev, error) {
} }
//log.Printf("lepton not yet booted: 0x%02x", status) //log.Printf("lepton not yet booted: 0x%02x", status)
// Polling rocks. // Polling rocks.
time.Sleep(5 * time.Millisecond) sleep(5 * time.Millisecond)
} }
} }
@ -334,7 +334,7 @@ func (c *cciConn) waitIdle() (StatusBit, error) {
if s, err := c.r.ReadUint16(regStatus); err != nil || StatusBit(s)&StatusBusy == 0 { if s, err := c.r.ReadUint16(regStatus); err != nil || StatusBit(s)&StatusBusy == 0 {
return StatusBit(s), err return StatusBit(s), err
} }
time.Sleep(5 * time.Millisecond) sleep(5 * time.Millisecond)
} }
} }
@ -558,6 +558,8 @@ const (
// TODO(maruel): Enable RadXXX commands. // TODO(maruel): Enable RadXXX commands.
var sleep = time.Sleep
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &Dev{}
var _ fmt.Stringer = &cciConn{} var _ fmt.Stringer = &cciConn{}

@ -6,6 +6,7 @@ package cci
import ( import (
"testing" "testing"
"time"
"periph.io/x/periph/conn/i2c" "periph.io/x/periph/conn/i2c"
"periph.io/x/periph/conn/i2c/i2ctest" "periph.io/x/periph/conn/i2c/i2ctest"
@ -613,3 +614,7 @@ func runOps(c []byte) []i2ctest.IO {
{Addr: 42, W: []byte{0x00, 0x02}, R: []byte{0x00, 0x06}}, {Addr: 42, W: []byte{0x00, 0x02}, R: []byte{0x00, 0x06}},
} }
} }
func init() {
sleep = func(time.Duration) {}
}

@ -189,7 +189,7 @@ func (d *Dev) writeByte(b byte) (bool, error) {
} }
// 9th clock is ACK. // 9th clock is ACK.
_ = d.data.Out(gpio.Low) _ = d.data.Out(gpio.Low)
time.Sleep(clockHalfCycle) d.sleepHalfCycle()
// TODO(maruel): Add. // TODO(maruel): Add.
//if err := d.data.In(gpio.PullUp, gpio.NoEdge); err != nil { //if err := d.data.In(gpio.PullUp, gpio.NoEdge); err != nil {
// return false, err // return false, err
@ -207,8 +207,10 @@ func (d *Dev) writeByte(b byte) (bool, error) {
// sleep does a busy loop to act as fast as possible. // sleep does a busy loop to act as fast as possible.
func (d *Dev) sleepHalfCycle() { func (d *Dev) sleepHalfCycle() {
cpu.Nanospin(clockHalfCycle) spin(clockHalfCycle)
} }
var spin = cpu.Nanospin
var _ conn.Resource = &Dev{} var _ conn.Resource = &Dev{}
var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &Dev{}

@ -9,6 +9,7 @@ import (
"errors" "errors"
"log" "log"
"testing" "testing"
"time"
"periph.io/x/periph/conn/gpio" "periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpioreg" "periph.io/x/periph/conn/gpio/gpioreg"
@ -102,3 +103,7 @@ func (f *failPin) Out(l gpio.Level) error {
} }
return nil return nil
} }
func init() {
spin = func(time.Duration) {}
}

Loading…
Cancel
Save