File: clock_test.go

package info (click to toggle)
golang-github-juju-testing 0.0~git20170608.2fe0e88-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 464 kB
  • sloc: makefile: 6
file content (229 lines) | stat: -rw-r--r-- 4,955 bytes parent folder | download
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package testing_test

import (
	"sync"
	"time"

	gc "gopkg.in/check.v1"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
)

type clockSuite struct {
	testing.LoggingSuite
}

var _ = gc.Suite(&clockSuite{})

func (*clockSuite) TestNow(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)
	c.Assert(cl.Now(), gc.Equals, t0)
}

var (
	shortWait = 50 * time.Millisecond
	longWait  = time.Second
)

func (*clockSuite) TestAdvanceLogs(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)

	// Shouldn't log anything.
	t := cl.After(time.Second)
	cl.Advance(time.Minute)
	<-t
	c.Check(c.GetTestLog(), jc.DeepEquals, "")

	// Should log since nothing's waiting.
	cl.Advance(time.Hour)
	c.Check(c.GetTestLog(), jc.Contains, "advancing a clock that has nothing waiting: cf. https://github.com/juju/juju/wiki/Intermittent-failures")
}

func (*clockSuite) TestWaitAdvance(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)

	// Test that no timers errors out.
	err := cl.WaitAdvance(time.Millisecond, 10*time.Millisecond, 1)
	c.Check(err, gc.ErrorMatches, "got 0 timers added after waiting 10ms: wanted 1")

	// Test that a timer doesn't error.
	_ = cl.After(time.Nanosecond)
	err = cl.WaitAdvance(time.Millisecond, 10*time.Millisecond, 1)
	c.Check(err, jc.ErrorIsNil)
}

func (*clockSuite) TestAdvanceWithAfter(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)
	ch := cl.After(time.Second)
	select {
	case <-ch:
		c.Fatalf("received unexpected event")
	case <-time.After(shortWait):
	}

	cl.Advance(time.Second - 1)

	select {
	case <-ch:
		c.Fatalf("received unexpected event")
	case <-time.After(shortWait):
	}

	cl.Advance(1)

	select {
	case <-ch:
	case <-time.After(longWait):
		c.Fatalf("expected event to be triggered")
	}

	cl.Advance(time.Second)
	select {
	case <-ch:
		c.Fatalf("received unexpected event")
	case <-time.After(shortWait):
	}

	// Test that we can do it again
	ch = cl.After(time.Second)
	cl.Advance(2 * time.Second)
	select {
	case <-ch:
	case <-time.After(longWait):
		c.Fatalf("expected event to be triggered")
	}
	c.Assert(cl.Now().UTC(), gc.Equals, t0.Add(4*time.Second).UTC())
}

func (*clockSuite) TestAdvanceWithAfterFunc(c *gc.C) {
	// Most of the details have been checked in TestAdvanceWithAfter,
	// so just check that AfterFunc is wired up correctly.
	t0 := time.Now()
	cl := testing.NewClock(t0)
	fired := make(chan struct{})
	cl.AfterFunc(time.Second, func() {
		close(fired)
	})
	cl.Advance(2 * time.Second)
	select {
	case <-fired:
	case <-time.After(longWait):
		c.Fatalf("expected event to be triggered")
	}
}

func (*clockSuite) TestAfterFuncStop(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)
	fired := make(chan struct{})
	timer := cl.AfterFunc(time.Second, func() {
		close(fired)
	})
	cl.Advance(50 * time.Millisecond)
	timer.Stop()
	select {
	case <-fired:
		c.Fatalf("received unexpected event")
	case <-time.After(shortWait):
	}
}

func (*clockSuite) TestNewTimerReset(c *gc.C) {
	t0 := time.Now()
	cl := testing.NewClock(t0)
	timer := cl.NewTimer(time.Second)
	cl.Advance(time.Second)
	select {
	case t := <-timer.Chan():
		c.Assert(t.UTC(), gc.Equals, t0.Add(time.Second).UTC())
	case <-time.After(longWait):
		c.Fatalf("expected event to be triggered")
	}

	timer.Reset(50 * time.Millisecond)
	cl.Advance(100 * time.Millisecond)
	select {
	case t := <-timer.Chan():
		c.Assert(t.UTC(), gc.Equals, t0.Add(time.Second+100*time.Millisecond).UTC())
	case <-time.After(longWait):
		c.Fatalf("expected event to be triggered")
	}
}

func (*clockSuite) TestMultipleWaiters(c *gc.C) {
	var wg sync.WaitGroup
	t0 := time.Date(2000, 01, 01, 01, 0, 0, 0, time.UTC)
	cl := testing.NewClock(t0)

	total := 0
	start := func(f func()) {
		total++
		wg.Add(1)
		go func() {
			defer wg.Done()
			f()
		}()
	}
	start(func() {
		<-cl.After(50 * time.Millisecond)
	})
	start(func() {
		ch := make(chan struct{})
		cl.AfterFunc(100*time.Millisecond, func() {
			close(ch)
		})
		<-ch
	})
	start(func() {
		timer := cl.NewTimer(150 * time.Millisecond)
		<-timer.Chan()
		timer.Reset(50 * time.Millisecond)
		<-timer.Chan()
	})

	done := make(chan struct{})
	go func() {
		wg.Wait()
		close(done)
	}()

	// Wait for all the alarms to be waited on.
	for i := 0; i < total; i++ {
		select {
		case <-cl.Alarms():
		case <-time.After(longWait):
			c.Fatalf("expected a notification on the alarms channel")
		}
	}
	select {
	case <-cl.Alarms():
		c.Fatalf("unexpected extra notification on alarms channel")
	case <-time.After(shortWait):
	}

	cl.Advance(150 * time.Millisecond)

	// Wait for the extra notification after reset.
	select {
	case <-cl.Alarms():
	case <-time.After(longWait):
		c.Fatalf("expected a notification on the alarms channel")
	}

	cl.Advance(50 * time.Millisecond)

	select {
	case <-done:
	case <-time.After(longWait):
		c.Fatalf("expected all waits to complete")
	}

}