forked from folbricht/routedns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailback_test.go
49 lines (40 loc) · 1.24 KB
/
failback_test.go
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
package rdns
import (
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
)
func TestFailBack(t *testing.T) {
// Build 2 resolvers that count the number of invocations
var ci ClientInfo
r1 := new(TestResolver)
r2 := new(TestResolver)
g := NewFailBack(FailBackOptions{ResetAfter: time.Second}, r1, r2)
q := new(dns.Msg)
q.SetQuestion("test.com.", dns.TypeA)
// Send the first couple of queries. The first resolver should be active and be used for both
_, err := g.Resolve(q, ci)
require.NoError(t, err)
_, err = g.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 2, r1.HitCount())
require.Equal(t, 0, r2.HitCount())
// Set the 1st to failure
r1.SetFail(true)
// The next one should hit both stores (1st will fail, 2nd succeed)
_, err = g.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 3, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
// Fix the 1st resolver and wait a second
r1.SetFail(false)
time.Sleep(time.Second + 100*time.Millisecond)
// It should have been reset and the first should be active again now
_, err = g.Resolve(q, ci)
require.NoError(t, err)
_, err = g.Resolve(q, ci)
require.NoError(t, err)
require.Equal(t, 5, r1.HitCount())
require.Equal(t, 1, r2.HitCount())
}