-
Notifications
You must be signed in to change notification settings - Fork 8
/
example_test.go
42 lines (33 loc) · 966 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package abtime
import (
"time"
)
// It's best to allocate IDs like this for your time usages.
const (
timeoutID = iota
)
func ExampleAbstractTime() {
// Suppose you have a goroutine feeding you something from a socket,
// and you want to do something if that times out. You can test this
// with:
manualTime := NewManual()
timedOut := make(chan struct{})
go ReadSocket(manualTime, timedOut)
manualTime.Trigger(timeoutID)
// This will read the struct{}{} from above. Getting here asserts
// that we did what we wanted when we timed out.
<-timedOut
}
// In production code, at would be a RealTime, and thus use the "real"
// time.After function, ignoring the ID.
func ReadSocket(at AbstractTime, timedOut chan struct{}) {
timeout := at.After(time.Second, timeoutID)
// in this example, this will never be filled
fromSocket := make(chan []byte)
select {
case <-fromSocket:
// handle socketData
case <-timeout:
timedOut <- struct{}{}
}
}