This file is indexed.

/usr/share/gocode/src/gopkg.in/retry.v1/retry_test.go is in golang-gopkg-retry.v1-dev 0.0~git20161025.0.c09f6b8-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2011, 2012, 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package retry_test // import "gopkg.in/retry.v1"

import (
	"sort"
	"sync"
	"time"

	gc "gopkg.in/check.v1"

	"gopkg.in/retry.v1"
)

type retrySuite struct{}

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

func (*retrySuite) TestAttemptTiming(c *gc.C) {
	testAttempt := retry.Regular{
		Total: 0.25e9,
		Delay: 0.1e9,
	}
	want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9}
	got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
	t0 := time.Now()
	a := testAttempt.Start(nil)
	for a.Next() {
		got = append(got, time.Now().Sub(t0))
	}
	got = append(got, time.Now().Sub(t0))
	c.Assert(a.Stopped(), gc.Equals, false)
	c.Assert(got, gc.HasLen, len(want))
	const margin = 0.01e9
	for i, got := range want {
		lo := want[i] - margin
		hi := want[i] + margin
		if got < lo || got > hi {
			c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds())
		}
	}
}

func (*retrySuite) TestAttemptNextMore(c *gc.C) {
	a := retry.Regular{}.Start(nil)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, false)

	a = retry.Regular{}.Start(nil)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.More(), gc.Equals, false)
	c.Assert(a.Next(), gc.Equals, false)

	a = retry.Regular{Total: 2e8}.Start(nil)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.More(), gc.Equals, true)
	time.Sleep(2e8)
	c.Assert(a.More(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, false)

	a = retry.Regular{Total: 1e8, Min: 2}.Start(nil)
	time.Sleep(1e8)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.More(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.More(), gc.Equals, false)
	c.Assert(a.Next(), gc.Equals, false)
}

func (*retrySuite) TestAttemptWithStop(c *gc.C) {
	stop := make(chan struct{})
	close(stop)
	done := make(chan struct{})
	go func() {
		strategy := retry.Regular{
			Delay: 5 * time.Second,
			Total: 30 * time.Second,
		}
		a := retry.StartWithCancel(strategy, nil, stop)
		for a.Next() {
			c.Errorf("unexpected attempt")
		}
		c.Check(a.Stopped(), gc.Equals, true)
		close(done)
	}()
	assertReceive(c, done, "attempt loop abort")
}

func (*retrySuite) TestAttemptWithLaterStop(c *gc.C) {
	clock := NewClock(time.Now())
	stop := make(chan struct{})
	done := make(chan struct{})
	progress := make(chan struct{}, 10)
	go func() {
		strategy := retry.Regular{
			Delay: 5 * time.Second,
			Total: 30 * time.Second,
		}
		a := retry.StartWithCancel(strategy, clock, stop)
		for a.Next() {
			progress <- struct{}{}
		}
		c.Check(a.Stopped(), gc.Equals, true)
		close(done)
	}()
	assertReceive(c, progress, "progress")
	clock.Advance(5 * time.Second)
	assertReceive(c, progress, "progress")
	clock.Advance(2 * time.Second)
	close(stop)
	assertReceive(c, done, "attempt loop abort")
	select {
	case <-progress:
		c.Fatalf("unxpected loop iteration after stop")
	default:
	}
}

func (*retrySuite) TestAttemptWithMockClock(c *gc.C) {
	clock := NewClock(time.Now())
	strategy := retry.Regular{
		Delay: 5 * time.Second,
		Total: 30 * time.Second,
	}
	progress := make(chan struct{})
	done := make(chan struct{})
	go func() {
		for a := strategy.Start(clock); a.Next(); {
			progress <- struct{}{}
		}
		close(done)
	}()
	assertReceive(c, progress, "progress first time")
	clock.Advance(5 * time.Second)
	assertReceive(c, progress, "progress second time")
	clock.Advance(5 * time.Second)
	assertReceive(c, progress, "progress third time")
	clock.Advance(30 * time.Second)
	assertReceive(c, progress, "progress fourth time")
	assertReceive(c, done, "loop finish")
}

type strategyTest struct {
	about      string
	strategy   retry.Strategy
	calls      []nextCall
	terminates bool
}

type nextCall struct {
	// t holds the time since the timer was started that
	// the Next call will be made.
	t time.Duration
	// delay holds the length of time that a call made at
	// time t is expected to sleep for.
	sleep time.Duration
}

var strategyTests = []strategyTest{{
	about: "regular retry (same params as TestAttemptTiming)",
	strategy: retry.Regular{
		Total: 0.25e9,
		Delay: 0.1e9,
	},
	calls: []nextCall{
		{0, 0},
		{0, 0.1e9},
		{0.1e9, 0.1e9},
		{0.2e9, 0},
	},
	terminates: true,
}, {
	about: "regular retry with calls at different times",
	strategy: retry.Regular{
		Total: 2.5e9,
		Delay: 1e9,
	},
	calls: []nextCall{
		{0.5e9, 0},
		{0.5e9, 0.5e9},
		{1.1e9, 0.9e9},
		{2.2e9, 0},
	},
	terminates: true,
}, {
	about: "regular retry with call after next deadline",
	strategy: retry.Regular{
		Total: 3.5e9,
		Delay: 1e9,
	},
	calls: []nextCall{
		{0.5e9, 0},
		// We call Next at well beyond the deadline,
		// so we get a zero delay, but subsequent events
		// resume pace.
		{2e9, 0},
		{2.1e9, 0.9e9},
		{3e9, 0},
	},
	terminates: true,
}, {
	about: "exponential retry",
	strategy: retry.Exponential{
		Initial: 1e9,
		Factor:  2,
	},
	calls: []nextCall{
		{0, 0},
		{0.1e9, 0.9e9},
		{1e9, 2e9},
		{3e9, 4e9},
		{7e9, 8e9},
	},
}, {
	about: "time-limited exponential retry",
	strategy: retry.LimitTime(5e9, retry.Exponential{
		Initial: 1e9,
		Factor:  2,
	}),
	calls: []nextCall{
		{0, 0},
		{0.1e9, 0.9e9},
		{1e9, 2e9},
		{3e9, 0},
	},
	terminates: true,
}, {
	about: "count-limited exponential retry",
	strategy: retry.LimitCount(2, retry.Exponential{
		Initial: 1e9,
		Factor:  2,
	}),
	calls: []nextCall{
		{0, 0},
		{0.1e9, 0.9e9},
		{1e9, 0},
	},
	terminates: true,
}}

func (*retrySuite) TestStrategies(c *gc.C) {
	for i, test := range strategyTests {
		c.Logf("test %d: %s", i, test.about)
		testStrategy(c, test)
	}
}

func testStrategy(c *gc.C, test strategyTest) {
	t0 := time.Now()
	clk := &mockClock{
		now: t0,
	}
	a := retry.Start(test.strategy, clk)
	for i, call := range test.calls {
		c.Logf("call %d - %v", i, call.t)
		clk.now = t0.Add(call.t)
		ok := a.Next()
		expectTerminate := test.terminates && i == len(test.calls)-1
		c.Assert(ok, gc.Equals, !expectTerminate)
		if got, want := clk.now.Sub(t0), call.t+call.sleep; !closeTo(got, want) {
			c.Fatalf("incorrect time after Next; got %v want %v", got, want)
		}
		if ok {
			c.Assert(a.Count(), gc.Equals, i+1)
		}
	}
}

func (*retrySuite) TestGapBetweenMoreAndNext(c *gc.C) {
	t0 := time.Now().UTC()
	clk := &mockClock{
		now: t0,
	}
	a := (&retry.Regular{
		Min:   3,
		Delay: time.Second,
	}).Start(clk)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(clk.now, gc.Equals, t0)

	clk.now = clk.now.Add(500 * time.Millisecond)
	// Sanity check that the first iteration sleeps for half a second.
	c.Assert(a.More(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(clk.now.Sub(t0), gc.Equals, t0.Add(time.Second).Sub(t0))

	clk.now = clk.now.Add(500 * time.Millisecond)
	c.Assert(a.More(), gc.Equals, true)

	// Add a delay between calling More and Next.
	// Next should wait until the correct time anyway.
	clk.now = clk.now.Add(250 * time.Millisecond)
	c.Assert(a.More(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(clk.now.Sub(t0), gc.Equals, t0.Add(2*time.Second).Sub(t0))
}

// closeTo reports whether d0 and d1 are close enough
// to one another to cater for inaccuracies of floating point arithmetic.
func closeTo(d0, d1 time.Duration) bool {
	const margin = 20 * time.Nanosecond
	diff := d1 - d0
	if diff < 0 {
		diff = -diff
	}
	return diff < margin
}

type mockClock struct {
	now   time.Time
	sleep func(d time.Duration)
}

func (c *mockClock) After(d time.Duration) <-chan time.Time {
	c.now = c.now.Add(d)
	ch := make(chan time.Time)
	close(ch)
	return ch
}

func (c *mockClock) Now() time.Time {
	return c.now
}

func assertReceive(c *gc.C, ch <-chan struct{}, what string) {
	select {
	case <-ch:
	case <-time.After(time.Second):
		c.Fatalf("timed out waiting for %s", what)
	}
}

// Below is code from github.com/juju/testing/clock.go (fortunately with
// the same copyright and license as this file), copied in rather than
// packaging juju/testing and its many (oftentimes circular) dependencies.

// timer implements a mock clock.Timer for testing purposes.
type timer struct {
	deadline time.Time
	clock    *Clock
	c        chan time.Time
	// trigger is called when the timer expires. It is
	// called with the clock mutex held and will not block.
	trigger func()
}

// Chan is part of the clock.Timer interface.
func (t *timer) Chan() <-chan time.Time {
	return t.c
}

// Clock implements a mock clock.Clock for testing purposes.
type Clock struct {
	mu           sync.Mutex
	now          time.Time
	waiting      []*timer // timers waiting to fire, sorted by deadline.
	notifyAlarms chan struct{}
}

// NewClock returns a new clock set to the supplied time. If your SUT needs to
// call After, AfterFunc, NewTimer or Timer.Reset more than 10000 times: (1)
// you have probably written a bad test; and (2) you'll need to read from the
// Alarms chan to keep the buffer clear.
func NewClock(now time.Time) *Clock {
	return &Clock{
		now:          now,
		notifyAlarms: make(chan struct{}, 10000),
	}
}

// Now is part of the clock.Clock interface.
func (clock *Clock) Now() time.Time {
	clock.mu.Lock()
	defer clock.mu.Unlock()
	return clock.now
}

// After is part of the clock.Clock interface.
func (clock *Clock) After(d time.Duration) <-chan time.Time {
	return clock.NewTimer(d).Chan()
}

func (clock *Clock) NewTimer(d time.Duration) *timer {
	c := make(chan time.Time, 1)
	return clock.addAlarm(d, c, func() {
		c <- clock.now
	})
}

func (clock *Clock) addAlarm(d time.Duration, c chan time.Time, trigger func()) *timer {
	defer clock.notifyAlarm()
	clock.mu.Lock()
	defer clock.mu.Unlock()
	t := &timer{
		c:        c,
		deadline: clock.now.Add(d),
		clock:    clock,
		trigger:  trigger,
	}
	clock.addTimer(t)
	clock.triggerAll()
	return t
}

// Advance advances the result of Now by the supplied duration, and sends
// the "current" time on all alarms which are no longer "in the future".
func (clock *Clock) Advance(d time.Duration) {
	clock.mu.Lock()
	defer clock.mu.Unlock()
	clock.now = clock.now.Add(d)
	clock.triggerAll()
}

// triggerAll triggers any alarms that are currently due and removes them
// from clock.waiting.
func (clock *Clock) triggerAll() {
	triggered := 0
	for _, t := range clock.waiting {
		if clock.now.Before(t.deadline) {
			break
		}
		t.trigger()
		triggered++
	}
	clock.waiting = clock.waiting[triggered:]
}

// reset is the underlying implementation of clock.Timer.Reset, which may be
// called by any Timer backed by this Clock.
func (clock *Clock) reset(t *timer, d time.Duration) bool {
	defer clock.notifyAlarm()
	clock.mu.Lock()
	defer clock.mu.Unlock()

	found := false
	for _, wt := range clock.waiting {
		if wt == t {
			found = true
		}
	}
	if !found {
		clock.waiting = append(clock.waiting, t)
	}
	t.deadline = clock.now.Add(d)
	sort.Sort(byDeadline(clock.waiting))
	return found
}

// stop is the underlying implementation of clock.Timer.Reset, which may be
// called by any Timer backed by this Clock.
func (clock *Clock) stop(t *timer) bool {
	clock.mu.Lock()
	defer clock.mu.Unlock()

	for i, wt := range clock.waiting {
		if wt == t {
			clock.waiting = removeFromSlice(clock.waiting, i)
			return true
		}
	}
	return false
}

// addTimer adds an alarm at time t.
func (clock *Clock) addTimer(t *timer) {
	clock.waiting = append(clock.waiting, t)
	sort.Sort(byDeadline(clock.waiting))
}

// notifyAlarm sends a value on the channel exposed by Alarms().
func (clock *Clock) notifyAlarm() {
	select {
	case clock.notifyAlarms <- struct{}{}:
	default:
		panic("alarm notification buffer full")
	}
}

// byDeadline is used to sort alarms by time.
type byDeadline []*timer

func (a byDeadline) Len() int           { return len(a) }
func (a byDeadline) Less(i, j int) bool { return a[i].deadline.Before(a[j].deadline) }
func (a byDeadline) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// removeFromSlice removes item at the specified index from the slice.
func removeFromSlice(sl []*timer, index int) []*timer {
	return append(sl[:index], sl[index+1:]...)
}