File: registrar_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (102 lines) | stat: -rw-r--r-- 2,579 bytes parent folder | download | duplicates (5)
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
package eureka

import (
	"testing"
	"time"
)

func TestRegistrar(t *testing.T) {
	connection := &testConnection{
		errHeartbeat: errTest,
	}

	registrar1 := NewRegistrar(connection, instanceTest1, loggerTest)
	registrar2 := NewRegistrar(connection, instanceTest2, loggerTest)

	// Not registered.
	registrar1.Deregister()
	if want, have := 0, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Register.
	registrar1.Register()
	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	registrar2.Register()
	if want, have := 2, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Deregister.
	registrar1.Deregister()
	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Already registered.
	registrar1.Register()
	if want, have := 2, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	registrar1.Register()
	if want, have := 2, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// Wait for a heartbeat failure.
	time.Sleep(1010 * time.Millisecond)
	if want, have := 2, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	registrar1.Deregister()
	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}

func TestBadRegister(t *testing.T) {
	connection := &testConnection{
		errRegister: errTest,
	}

	registrar := NewRegistrar(connection, instanceTest1, loggerTest)
	registrar.Register()
	if want, have := 0, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}

func TestBadDeregister(t *testing.T) {
	connection := &testConnection{
		errDeregister: errTest,
	}

	registrar := NewRegistrar(connection, instanceTest1, loggerTest)
	registrar.Register()
	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	registrar.Deregister()
	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}

func TestExpiredInstance(t *testing.T) {
	connection := &testConnection{
		errHeartbeat: errNotFound,
	}

	registrar := NewRegistrar(connection, instanceTest1, loggerTest)
	registrar.Register()

	// Wait for a heartbeat failure.
	time.Sleep(1010 * time.Millisecond)

	if want, have := 1, len(connection.instances); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}