File: attempt_test.go

package info (click to toggle)
golang-github-adroll-goamz 0.0~git20170225.0.c5d7d9b-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,520 kB
  • ctags: 2,498
  • sloc: makefile: 41
file content (57 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download | duplicates (2)
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
package aws_test

import (
	"github.com/AdRoll/goamz/aws"
	"gopkg.in/check.v1"
	"time"
)

func (S) TestAttemptTiming(c *check.C) {
	testAttempt := aws.AttemptStrategy{
		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()
	for a := testAttempt.Start(); a.Next(); {
		got = append(got, time.Now().Sub(t0))
	}
	got = append(got, time.Now().Sub(t0))
	c.Assert(got, check.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 (S) TestAttemptNextHasNext(c *check.C) {
	a := aws.AttemptStrategy{}.Start()
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.Next(), check.Equals, false)

	a = aws.AttemptStrategy{}.Start()
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.HasNext(), check.Equals, false)
	c.Assert(a.Next(), check.Equals, false)

	a = aws.AttemptStrategy{Total: 2e8}.Start()
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.HasNext(), check.Equals, true)
	time.Sleep(2e8)
	c.Assert(a.HasNext(), check.Equals, true)
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.Next(), check.Equals, false)

	a = aws.AttemptStrategy{Total: 1e8, Min: 2}.Start()
	time.Sleep(1e8)
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.HasNext(), check.Equals, true)
	c.Assert(a.Next(), check.Equals, true)
	c.Assert(a.HasNext(), check.Equals, false)
	c.Assert(a.Next(), check.Equals, false)
}