OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / sd / eureka / registrar_test.go
1 package eureka
2
3 import (
4         "testing"
5         "time"
6 )
7
8 func TestRegistrar(t *testing.T) {
9         connection := &testConnection{
10                 errHeartbeat: errTest,
11         }
12
13         registrar1 := NewRegistrar(connection, instanceTest1, loggerTest)
14         registrar2 := NewRegistrar(connection, instanceTest2, loggerTest)
15
16         // Not registered.
17         registrar1.Deregister()
18         if want, have := 0, len(connection.instances); want != have {
19                 t.Errorf("want %d, have %d", want, have)
20         }
21
22         // Register.
23         registrar1.Register()
24         if want, have := 1, len(connection.instances); want != have {
25                 t.Errorf("want %d, have %d", want, have)
26         }
27
28         registrar2.Register()
29         if want, have := 2, len(connection.instances); want != have {
30                 t.Errorf("want %d, have %d", want, have)
31         }
32
33         // Deregister.
34         registrar1.Deregister()
35         if want, have := 1, len(connection.instances); want != have {
36                 t.Errorf("want %d, have %d", want, have)
37         }
38
39         // Already registered.
40         registrar1.Register()
41         if want, have := 2, len(connection.instances); want != have {
42                 t.Errorf("want %d, have %d", want, have)
43         }
44         registrar1.Register()
45         if want, have := 2, len(connection.instances); want != have {
46                 t.Errorf("want %d, have %d", want, have)
47         }
48
49         // Wait for a heartbeat failure.
50         time.Sleep(1010 * time.Millisecond)
51         if want, have := 2, len(connection.instances); want != have {
52                 t.Errorf("want %d, have %d", want, have)
53         }
54         registrar1.Deregister()
55         if want, have := 1, len(connection.instances); want != have {
56                 t.Errorf("want %d, have %d", want, have)
57         }
58 }
59
60 func TestBadRegister(t *testing.T) {
61         connection := &testConnection{
62                 errRegister: errTest,
63         }
64
65         registrar := NewRegistrar(connection, instanceTest1, loggerTest)
66         registrar.Register()
67         if want, have := 0, len(connection.instances); want != have {
68                 t.Errorf("want %d, have %d", want, have)
69         }
70 }
71
72 func TestBadDeregister(t *testing.T) {
73         connection := &testConnection{
74                 errDeregister: errTest,
75         }
76
77         registrar := NewRegistrar(connection, instanceTest1, loggerTest)
78         registrar.Register()
79         if want, have := 1, len(connection.instances); want != have {
80                 t.Errorf("want %d, have %d", want, have)
81         }
82         registrar.Deregister()
83         if want, have := 1, len(connection.instances); want != have {
84                 t.Errorf("want %d, have %d", want, have)
85         }
86 }
87
88 func TestExpiredInstance(t *testing.T) {
89         connection := &testConnection{
90                 errHeartbeat: errNotFound,
91         }
92
93         registrar := NewRegistrar(connection, instanceTest1, loggerTest)
94         registrar.Register()
95
96         // Wait for a heartbeat failure.
97         time.Sleep(1010 * time.Millisecond)
98
99         if want, have := 1, len(connection.instances); want != have {
100                 t.Errorf("want %d, have %d", want, have)
101         }
102 }