@@ -74,13 +74,28 @@ func TestDebounce_Read_High(t *testing.T) {
7474}
7575
7676func TestDebounce_WaitForEdge_Got (t * testing.T ) {
77- f := gpiotest.Pin {EdgesChan : make (chan gpio.Level , 1 )}
77+ fakeClock := clockwork .NewFakeClock ()
78+ f := gpiotest.Pin {
79+ Clock : fakeClock ,
80+ EdgesChan : make (chan gpio.Level , 1 ),
81+ }
7882 p , err := Debounce (& f , time .Second , 0 , gpio .BothEdges )
79- f .Out (gpio .High )
8083 if err != nil {
8184 t .Fatal (err )
8285 }
86+ p .(* debounced ).clock = fakeClock
87+ f .Out (gpio .High )
8388 f .EdgesChan <- gpio .Low
89+ go func () {
90+ // Sleepers:
91+ // * debounce.WaitForEdge's d.Clock.Sleep
92+ //
93+ // gpiotest.WaitForEdge doesn't call sleep due to infinite timeout.
94+ const numSleepers = 1
95+
96+ fakeClock .BlockUntil (numSleepers )
97+ fakeClock .Advance (2 * time .Second )
98+ }()
8499 if ! p .WaitForEdge (- 1 ) {
85100 t .Fatal ("expected edge" )
86101 }
@@ -99,13 +114,16 @@ func TestDebounce_WaitForEdge_Noise_NoEdge(t *testing.T) {
99114 p .(* debounced ).clock = fakeClock
100115 f .Out (gpio .Low )
101116
102- // Short high, comes back down too soon
103- f .EdgesChan <- gpio .High
104117 go func () {
105118 // Sleepers:
106119 // * gpiotest.WaitForEdge's After
107120 // * debounce.WaitForEdge's d.Clock.Sleep
108- fakeClock .BlockUntil (2 )
121+ const numSleepers = 2
122+
123+ // Short high, comes back down too soon
124+ f .EdgesChan <- gpio .High
125+ fakeClock .BlockUntil (numSleepers )
126+ fakeClock .Advance (100 * time .Millisecond )
109127 f .Out (gpio .Low )
110128 fakeClock .Advance (2 * time .Second )
111129 }()
@@ -116,22 +134,37 @@ func TestDebounce_WaitForEdge_Noise_NoEdge(t *testing.T) {
116134}
117135
118136func TestDebounce_WaitForEdge_Noise_Edge (t * testing.T ) {
119- f := gpiotest.Pin {EdgesChan : make (chan gpio.Level , 2 )}
137+ fakeClock := clockwork .NewFakeClock ()
138+
139+ f := gpiotest.Pin {
140+ Clock : fakeClock ,
141+ EdgesChan : make (chan gpio.Level , 2 ),
142+ }
120143 p , err := Debounce (& f , time .Second , 0 , gpio .BothEdges )
144+ p .(* debounced ).clock = fakeClock
121145 f .Out (gpio .Low )
122146 if err != nil {
123147 t .Fatal (err )
124148 }
125149
126- // Short high, comes back down too soon
127- f .EdgesChan <- gpio .High
128150 go func () {
129- time .Sleep (500 * time .Millisecond )
151+ // Sleepers:
152+ // * gpiotest.WaitForEdge's After
153+ // * debounce.WaitForEdge's d.Clock.Sleep
154+ const numSleepers = 2
155+
156+ // 100ms high (too short)
157+ f .EdgesChan <- gpio .High
158+ fakeClock .BlockUntil (numSleepers )
130159 f .Out (gpio .Low )
160+ fakeClock .Advance (100 * time .Millisecond )
161+
162+ // stays high indefinitely (long enough)
163+ fakeClock .BlockUntil (numSleepers )
164+ f .Out (gpio .High )
165+ fakeClock .Advance (2 * time .Second )
131166 }()
132167
133- // Second edge, stays high
134- f .EdgesChan <- gpio .High
135168 if ! p .WaitForEdge (4 * time .Second ) {
136169 t .Fatal ("expected edge" )
137170 }
0 commit comments