tec_test.go 3.38 KB
Newer Older
Jeromy's avatar
Jeromy committed
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
package temperrcatcher

import (
	"fmt"
	"testing"
	"time"
)

var (
	ErrTemp  = ErrTemporary{fmt.Errorf("ErrTemp")}
	ErrSkip  = fmt.Errorf("ErrSkip")
	ErrOther = fmt.Errorf("ErrOther")
)

func testTec(t *testing.T, c TempErrCatcher, errs map[error]bool) {
	for e, expected := range errs {
		if c.IsTemporary(e) != expected {
			t.Error("expected %s to be %v", e, expected)
		}
	}
}

func TestNil(t *testing.T) {
	var c TempErrCatcher
	testTec(t, c, map[error]bool{
		ErrTemp:  true,
		ErrSkip:  false,
		ErrOther: false,
	})
}

func TestWait(t *testing.T) {
	var c TempErrCatcher
	worked := make(chan time.Duration, 3)
	c.Wait = func(t time.Duration) {
		worked <- t
	}
	testTec(t, c, map[error]bool{
		ErrTemp:  true,
		ErrSkip:  false,
		ErrOther: false,
	})

	// should've called it once
	select {
	case <-worked:
	default:
		t.Error("did not call our Wait func")
	}

	// should've called it ONLY once
	select {
	case <-worked:
		t.Error("called our Wait func more than once")
	default:
	}
}

func TestTemporary(t *testing.T) {
	var c TempErrCatcher
	testTec(t, c, map[error]bool{
		ErrTemp:  true,
		ErrSkip:  false,
		ErrOther: false,
	})
}

func TestDoubles(t *testing.T) {
	last := time.Now()
	diff := func() time.Duration {
		now := time.Now()
		diff := now.Sub(last)
		last = now
		return diff
	}

	testDiff := func(low, hi time.Duration) {
		d := diff()
		grace := time.Duration(50 * time.Microsecond)
		if (d + grace) < low {
			t.Error("time difference is smaller than", low, d)
		}
		if (d - grace) > hi {
			t.Error("time difference is greater than", hi, d)
		}
	}

	var c TempErrCatcher
	testDiff(0, c.Start)
	c.IsTemporary(ErrTemp)
	testDiff(c.Start, 2*c.Start) // first time.
	c.IsTemporary(ErrTemp)
	testDiff(2*c.Start, 4*c.Start) // second time.
	c.IsTemporary(ErrTemp)
	testDiff(4*c.Start, 8*c.Start) // third time.
}

func TestDifferentStart(t *testing.T) {
	last := time.Now()
	diff := func() time.Duration {
		now := time.Now()
		diff := now.Sub(last)
		last = now
		return diff
	}

	testDiff := func(low, hi time.Duration) {
		d := diff()
		grace := time.Duration(50 * time.Microsecond)
		if (d + grace) < low {
			t.Error("time difference is smaller than", low, d)
		}
		if (d - grace) > hi {
			t.Error("time difference is greater than", hi, d)
		}
	}

	var c TempErrCatcher
	f := time.Millisecond
	testDiff(0, f)
	c.IsTemporary(ErrTemp)
	testDiff(f, 2*f) // first time.
	c.IsTemporary(ErrTemp)
	testDiff(2*f, 4*f) // second time.
	c.IsTemporary(ErrTemp)
	testDiff(4*f, 8*f) // third time.

	c.Reset()
	c.Start = 10 * time.Millisecond
	f = c.Start
	testDiff(0, f)
	c.IsTemporary(ErrTemp)
	testDiff(f, 2*f) // first time.
	c.IsTemporary(ErrTemp)
	testDiff(2*f, 4*f) // second time.
	c.IsTemporary(ErrTemp)
	testDiff(4*f, 8*f) // third time.
}

func TestDifferentStreaks(t *testing.T) {
	var c TempErrCatcher
	// one streak
	c.IsTemporary(ErrTemp) // 1
	c.IsTemporary(ErrTemp) // 2
	c.IsTemporary(ErrTemp) // 4
	expect := 4 * time.Millisecond
	if c.delay != expect {
		t.Error("delay should be:", expect, c.delay)
	}

	<-time.After(c.delay * 10)

	// a different streak
	c.IsTemporary(ErrTemp) // 1
	c.IsTemporary(ErrTemp) // 2
	c.IsTemporary(ErrTemp) // 4
	if c.delay != expect {
		t.Error("delay should be:", expect, c.delay)
	}
}

func TestFunc(t *testing.T) {
	var c TempErrCatcher
	c.IsTemp = func(e error) bool {
		return e == ErrSkip
	}
	testTec(t, c, map[error]bool{
		ErrTemp:  false,
		ErrSkip:  true,
		ErrOther: false,
	})
}