Skip to content

Commit

Permalink
Use clockwork.FakeClock in WaitForEdge_Noise_* (#16)
Browse files Browse the repository at this point in the history
This reduces testing time by:

* 2 seconds per WaitForEdge_Noise_Edge run
* 1 second per WaitForEdge_Noise_Got run

No further tests in debounce_test.go sleep.
  • Loading branch information
lutzky authored Sep 14, 2021
1 parent 3bb2581 commit d9a7690
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions gpio/gpioutil/debounce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,28 @@ func TestDebounce_Read_High(t *testing.T) {
}

func TestDebounce_WaitForEdge_Got(t *testing.T) {
f := gpiotest.Pin{EdgesChan: make(chan gpio.Level, 1)}
fakeClock := clockwork.NewFakeClock()
f := gpiotest.Pin{
Clock: fakeClock,
EdgesChan: make(chan gpio.Level, 1),
}
p, err := Debounce(&f, time.Second, 0, gpio.BothEdges)
f.Out(gpio.High)
if err != nil {
t.Fatal(err)
}
p.(*debounced).clock = fakeClock
f.Out(gpio.High)
f.EdgesChan <- gpio.Low
go func() {
// Sleepers:
// * debounce.WaitForEdge's d.Clock.Sleep
//
// gpiotest.WaitForEdge doesn't call sleep due to infinite timeout.
const numSleepers = 1

fakeClock.BlockUntil(numSleepers)
fakeClock.Advance(2 * time.Second)
}()
if !p.WaitForEdge(-1) {
t.Fatal("expected edge")
}
Expand All @@ -99,13 +114,16 @@ func TestDebounce_WaitForEdge_Noise_NoEdge(t *testing.T) {
p.(*debounced).clock = fakeClock
f.Out(gpio.Low)

// Short high, comes back down too soon
f.EdgesChan <- gpio.High
go func() {
// Sleepers:
// * gpiotest.WaitForEdge's After
// * debounce.WaitForEdge's d.Clock.Sleep
fakeClock.BlockUntil(2)
const numSleepers = 2

// Short high, comes back down too soon
f.EdgesChan <- gpio.High
fakeClock.BlockUntil(numSleepers)
fakeClock.Advance(100 * time.Millisecond)
f.Out(gpio.Low)
fakeClock.Advance(2 * time.Second)
}()
Expand All @@ -116,22 +134,37 @@ func TestDebounce_WaitForEdge_Noise_NoEdge(t *testing.T) {
}

func TestDebounce_WaitForEdge_Noise_Edge(t *testing.T) {
f := gpiotest.Pin{EdgesChan: make(chan gpio.Level, 2)}
fakeClock := clockwork.NewFakeClock()

f := gpiotest.Pin{
Clock: fakeClock,
EdgesChan: make(chan gpio.Level, 2),
}
p, err := Debounce(&f, time.Second, 0, gpio.BothEdges)
p.(*debounced).clock = fakeClock
f.Out(gpio.Low)
if err != nil {
t.Fatal(err)
}

// Short high, comes back down too soon
f.EdgesChan <- gpio.High
go func() {
time.Sleep(500 * time.Millisecond)
// Sleepers:
// * gpiotest.WaitForEdge's After
// * debounce.WaitForEdge's d.Clock.Sleep
const numSleepers = 2

// 100ms high (too short)
f.EdgesChan <- gpio.High
fakeClock.BlockUntil(numSleepers)
f.Out(gpio.Low)
fakeClock.Advance(100 * time.Millisecond)

// stays high indefinitely (long enough)
fakeClock.BlockUntil(numSleepers)
f.Out(gpio.High)
fakeClock.Advance(2 * time.Second)
}()

// Second edge, stays high
f.EdgesChan <- gpio.High
if !p.WaitForEdge(4 * time.Second) {
t.Fatal("expected edge")
}
Expand Down

0 comments on commit d9a7690

Please sign in to comment.