~egtann/lanhttp

ref: 3d0a36d422ec3e91780e98fd91e3f76394ef1918 lanhttp/lanhttp_test.go -rw-r--r-- 1.9 KiB
3d0a36d4 — Evan Tann fix goroutine leak 1 year, 10 months ago
                                                                                
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
package lanhttp

import (
	"math/rand"
	"testing"
)

func TestDiff(t *testing.T) {
	t.Parallel()

	type testcase struct {
		haveA Routes
		haveB Routes
		want  bool
	}
	tcs := map[string]testcase{
		"same empty": testcase{
			haveA: Routes{},
			haveB: Routes{},
			want:  false,
		},
		"same content": testcase{
			haveA: Routes{"a": []string{"a"}},
			haveB: Routes{"a": []string{"a"}},
			want:  false,
		},
		"sort": testcase{
			haveA: Routes{"a": []string{"a", "b"}},
			haveB: Routes{"a": []string{"b", "a"}},
			want:  false,
		},
		"a > b": testcase{
			haveA: Routes{"a": []string{"a"}},
			haveB: Routes{},
			want:  true,
		},
		"b > a": testcase{
			haveA: Routes{},
			haveB: Routes{"a": []string{"a"}},
			want:  true,
		},
		"a > b ips": testcase{
			haveA: Routes{"a": []string{"a", "b", "c"}},
			haveB: Routes{"a": []string{"a", "c"}},
			want:  true,
		},
		"b > a ips": testcase{
			haveA: Routes{"a": []string{"a"}},
			haveB: Routes{"a": []string{"a", "b"}},
			want:  true,
		},
		"a != b": testcase{
			haveA: Routes{"a": []string{"a"}},
			haveB: Routes{"b": []string{"a"}},
			want:  true,
		},
		"a != b ips": testcase{
			haveA: Routes{"a": []string{"a"}},
			haveB: Routes{"a": []string{"b"}},
			want:  true,
		},
	}
	for name, tc := range tcs {
		tc := tc // capture reference
		t.Run(name, func(t *testing.T) {
			t.Parallel()

			if diff(tc.haveA, tc.haveB) != tc.want {
				t.Fatal(name, tc.haveA, tc.haveB)
			}
		})
	}
}

func TestGetIP(t *testing.T) {
	// Set a seed to ensure our results below are consistent
	rand.Seed(16)
	c := NewClient(nil).WithRoutes(Routes{
		"a.internal": []string{"1", "2"},
	})
	if got := c.getIP("a.internal"); got != "1" {
		t.Fatal("expected 1 (1st)")
	}
	if got := c.getIP("a.internal"); got != "2" {
		t.Fatal("expected 2 (1st)")
	}
	if got := c.getIP("a.internal"); got != "1" {
		t.Fatal("expected 1 (2nd)")
	}
	if got := c.getIP("a.internal"); got != "2" {
		t.Fatal("expected 2 (2nd)")
	}
}