Skip to content

Commit 72da535

Browse files
committed
Update documentaiton on interactions with context.
1 parent 95528df commit 72da535

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Now you can easily test `myFunc` with a `FakeClock`:
3636

3737
```go
3838
func TestMyFunc(t *testing.T) {
39+
ctx := context.Background()
3940
c := clockwork.NewFakeClock()
4041

4142
// Start our sleepy function
@@ -46,8 +47,12 @@ func TestMyFunc(t *testing.T) {
4647
wg.Done()
4748
}()
4849

49-
// Ensure we wait until myFunc is sleeping
50-
c.BlockUntil(1)
50+
// Ensure we wait until myFunc is waiting on the clock.
51+
// Use a context to avoid blocking forever if something
52+
// goes wrong.
53+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
54+
defer cancel()
55+
c.BlockUntilContext(ctx, 1)
5156

5257
assertState()
5358

context.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import (
44
"context"
55
)
66

7-
// contextKey is private to this package, so we can ensure uniqueness here. This
7+
// contextKey is private to this package so we can ensure uniqueness here. This
88
// type identifies context values provided by this package.
99
type contextKey string
1010

1111
// keyClock provides a clock for injecting during tests. If absent, a real clock should be used.
1212
var keyClock = contextKey("clock") // clockwork.Clock
1313

1414
// AddToContext creates a derived context that references the specified clock.
15+
//
16+
// Be aware this doesn't change the behavior of standard library functions, such
17+
// as [context.WithTimeout] or [context.WithDeadline]. For this reason, users
18+
// should prefer passing explicit [clockwork.Clock] variables rather can passing
19+
// the clock via the context.
1520
func AddToContext(ctx context.Context, clock Clock) context.Context {
1621
return context.WithValue(ctx, keyClock, clock)
1722
}

example_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package clockwork
22

33
import (
4+
"context"
45
"sync"
56
"testing"
67
"time"
@@ -21,6 +22,7 @@ func assertState(t *testing.T, i, j int) {
2122

2223
// TestMyFunc tests myFunc's behaviour with a FakeClock.
2324
func TestMyFunc(t *testing.T) {
25+
ctx := context.Background()
2426
var i int
2527
c := NewFakeClock()
2628

@@ -31,8 +33,12 @@ func TestMyFunc(t *testing.T) {
3133
wg.Done()
3234
}()
3335

34-
// Wait until myFunc is actually sleeping on the clock.
35-
c.BlockUntil(1)
36+
// Ensure we wait until myFunc is waiting on the clock.
37+
// Use a context to avoid blocking forever if something
38+
// goes wrong.
39+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
40+
defer cancel()
41+
c.BlockUntilContext(ctx, 1)
3642

3743
// Assert the initial state.
3844
assertState(t, i, 0)

0 commit comments

Comments
 (0)