From b46e37febaa562d9175a161972c7c59dc3ec61a0 Mon Sep 17 00:00:00 2001 From: M-A Date: Sat, 23 Dec 2017 21:29:59 -0500 Subject: [PATCH] Remove all time.Sleep calls during tests (#204) It slows down tests, nobody wants that. --- devices/bmxx80/bmp180.go | 4 ++-- devices/bmxx80/bmp180_test.go | 4 ++++ devices/bmxx80/bmxx80.go | 4 +++- devices/ds18b20/ds18b20.go | 6 ++++-- devices/ds18b20/ds18b20_test.go | 22 +++++++++++++++------- devices/ds248x/dev.go | 6 ++++-- devices/ds248x/ds248x_test.go | 5 +++++ devices/lepton/cci/cci.go | 6 ++++-- devices/lepton/cci/cci_test.go | 5 +++++ devices/tm1637/tm1637.go | 6 ++++-- devices/tm1637/tm1637_test.go | 5 +++++ 11 files changed, 55 insertions(+), 18 deletions(-) diff --git a/devices/bmxx80/bmp180.go b/devices/bmxx80/bmp180.go index 32997f1..fcdd8e9 100644 --- a/devices/bmxx80/bmp180.go +++ b/devices/bmxx80/bmp180.go @@ -19,7 +19,7 @@ func (d *Dev) sense180(env *devices.Environment) error { if err := d.writeCommands([]byte{0xF4, 0x20 | 0x0E}); err != nil { return d.wrap(err) } - time.Sleep(4500 * time.Microsecond) + doSleep(4500 * time.Microsecond) var tempBuf [2]byte if err := d.readReg(0xF6, tempBuf[:]); err != nil { 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 { return d.wrap(err) } - time.Sleep(pressureConvTime180[d.os]) + doSleep(pressureConvTime180[d.os]) var pressureBuf [3]byte if err := d.readReg(0xF6, pressureBuf[:]); err != nil { return d.wrap(err) diff --git a/devices/bmxx80/bmp180_test.go b/devices/bmxx80/bmp180_test.go index 6acd118..b24c0cc 100644 --- a/devices/bmxx80/bmp180_test.go +++ b/devices/bmxx80/bmp180_test.go @@ -377,3 +377,7 @@ func TestCompensate180(t *testing.T) { t.Errorf("pressure is wrong, want %v, got %v", 69964, pressure) } } + +func init() { + doSleep = func(time.Duration) {} +} diff --git a/devices/bmxx80/bmxx80.go b/devices/bmxx80/bmxx80.go index 28f907d..0cc3743 100644 --- a/devices/bmxx80/bmxx80.go +++ b/devices/bmxx80/bmxx80.go @@ -167,7 +167,7 @@ func (d *Dev) Sense(env *devices.Environment) error { if err != nil { return d.wrap(err) } - time.Sleep(d.measDelay) + doSleep(d.measDelay) for idle := false; !idle; { if idle, err = d.isIdle280(); err != nil { return d.wrap(err) @@ -514,6 +514,8 @@ var defaults = Opts{ Humidity: O4x, } +var doSleep = time.Sleep + var _ conn.Resource = &Dev{} var _ devices.Environmental = &Dev{} var _ fmt.Stringer = &Dev{} diff --git a/devices/ds18b20/ds18b20.go b/devices/ds18b20/ds18b20.go index 9380fa5..fcf62f6 100644 --- a/devices/ds18b20/ds18b20.go +++ b/devices/ds18b20/ds18b20.go @@ -66,7 +66,7 @@ func New(o onewire.Bus, addr onewire.Address, resolutionBits int) (*Dev, error) return nil, err } // Wait for the write to complete. - time.Sleep(10 * time.Millisecond) + sleep(10 * time.Millisecond) } return d, nil @@ -158,7 +158,7 @@ func (e busError) BusError() bool { return true } // on the resolution: // 9bits:94ms, 10bits:188ms, 11bits:376ms, 12bits:752ms, datasheet p.6. 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. @@ -183,5 +183,7 @@ func (d *Dev) readScratchpad() ([]byte, error) { return spad[:8], nil } +var sleep = time.Sleep + var _ conn.Resource = &Dev{} var _ fmt.Stringer = &Dev{} diff --git a/devices/ds18b20/ds18b20_test.go b/devices/ds18b20/ds18b20_test.go index b036521..da3fba8 100644 --- a/devices/ds18b20/ds18b20_test.go +++ b/devices/ds18b20/ds18b20_test.go @@ -5,6 +5,7 @@ package ds18b20 import ( + "reflect" "testing" "time" @@ -61,9 +62,10 @@ func TestTemperature(t *testing.T) { t.Fatal(s) } // 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() - dt := time.Since(t0) if err != nil { t.Fatal(err) } @@ -72,8 +74,8 @@ func TestTemperature(t *testing.T) { t.Errorf("expected %s, got %s", temp.String(), now.String()) } // Expect it to take >187ms - if dt < 188*time.Millisecond { - t.Errorf("expected conversion to take >187ms, took %s", dt) + if !reflect.DeepEqual(sleeps, []time.Duration{188 * time.Millisecond}) { + t.Errorf("expected conversion to sleep: %v", sleeps) } if err := dev.Halt(); err != nil { t.Fatal(err) @@ -93,13 +95,15 @@ func TestConvertAll(t *testing.T) { } bus := onewiretest.Playback{Ops: ops} // 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 { t.Fatal(err) } // Expect it to take >93ms - if dt := time.Since(t0); dt < 94*time.Millisecond { - t.Errorf("expected conversion to take >93ms, took %s", dt) + if !reflect.DeepEqual(sleeps, []time.Duration{94 * time.Millisecond}) { + t.Errorf("expected conversion to take >93ms, took %s", sleeps) } if err := bus.Close(); err != nil { 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 // TestRecordTemp tests and records a temperature conversion. It outputs // the recording if the tests are run with the verbose option. diff --git a/devices/ds248x/dev.go b/devices/ds248x/dev.go index bf1a8fc..eddfa3e 100644 --- a/devices/ds248x/dev.go +++ b/devices/ds248x/dev.go @@ -162,7 +162,7 @@ func (d *Dev) waitIdle(delay time.Duration) byte { } // Overall timeout. tOut := time.Now().Add(3 * time.Millisecond) - time.Sleep(delay) + sleep(delay) for { // Read status register. var status [1]byte @@ -179,7 +179,7 @@ func (d *Dev) waitIdle(delay time.Duration) byte { return 0 } // 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) BusError() bool { return true } +var sleep = time.Sleep + var _ conn.Resource = &Dev{} var _ fmt.Stringer = &Dev{} diff --git a/devices/ds248x/ds248x_test.go b/devices/ds248x/ds248x_test.go index a799774..d0b1c27 100644 --- a/devices/ds248x/ds248x_test.go +++ b/devices/ds248x/ds248x_test.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "testing" + "time" "periph.io/x/periph/conn/i2c/i2creg" "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 // TestRecordInit tests and records the initialization of a ds248x by accessing // real hardware and outputs the recording ready to use for playback in diff --git a/devices/lepton/cci/cci.go b/devices/lepton/cci/cci.go index 95e0004..39f31e0 100644 --- a/devices/lepton/cci/cci.go +++ b/devices/lepton/cci/cci.go @@ -169,7 +169,7 @@ func New(i i2c.Bus) (*Dev, error) { } //log.Printf("lepton not yet booted: 0x%02x", status) // 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 { return StatusBit(s), err } - time.Sleep(5 * time.Millisecond) + sleep(5 * time.Millisecond) } } @@ -558,6 +558,8 @@ const ( // TODO(maruel): Enable RadXXX commands. +var sleep = time.Sleep + var _ conn.Resource = &Dev{} var _ fmt.Stringer = &Dev{} var _ fmt.Stringer = &cciConn{} diff --git a/devices/lepton/cci/cci_test.go b/devices/lepton/cci/cci_test.go index 7cf1d40..ff90e12 100644 --- a/devices/lepton/cci/cci_test.go +++ b/devices/lepton/cci/cci_test.go @@ -6,6 +6,7 @@ package cci import ( "testing" + "time" "periph.io/x/periph/conn/i2c" "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}}, } } + +func init() { + sleep = func(time.Duration) {} +} diff --git a/devices/tm1637/tm1637.go b/devices/tm1637/tm1637.go index bf84909..6905416 100644 --- a/devices/tm1637/tm1637.go +++ b/devices/tm1637/tm1637.go @@ -189,7 +189,7 @@ func (d *Dev) writeByte(b byte) (bool, error) { } // 9th clock is ACK. _ = d.data.Out(gpio.Low) - time.Sleep(clockHalfCycle) + d.sleepHalfCycle() // TODO(maruel): Add. //if err := d.data.In(gpio.PullUp, gpio.NoEdge); err != nil { // 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. func (d *Dev) sleepHalfCycle() { - cpu.Nanospin(clockHalfCycle) + spin(clockHalfCycle) } +var spin = cpu.Nanospin + var _ conn.Resource = &Dev{} var _ fmt.Stringer = &Dev{} diff --git a/devices/tm1637/tm1637_test.go b/devices/tm1637/tm1637_test.go index 85fd669..8856211 100644 --- a/devices/tm1637/tm1637_test.go +++ b/devices/tm1637/tm1637_test.go @@ -9,6 +9,7 @@ import ( "errors" "log" "testing" + "time" "periph.io/x/periph/conn/gpio" "periph.io/x/periph/conn/gpio/gpioreg" @@ -102,3 +103,7 @@ func (f *failPin) Out(l gpio.Level) error { } return nil } + +func init() { + spin = func(time.Duration) {} +}