File: instancer_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, experimental, sid
  • size: 4,848 kB
  • sloc: sh: 65; makefile: 14
file content (125 lines) | stat: -rw-r--r-- 2,993 bytes parent folder | download | duplicates (2)
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
package zk

import (
	"testing"
	"time"

	"github.com/go-kit/kit/sd"
)

var _ sd.Instancer = (*Instancer)(nil) // API check

func TestInstancer(t *testing.T) {
	client := newFakeClient()

	instancer, err := NewInstancer(client, path, logger)
	if err != nil {
		t.Fatalf("failed to create new Instancer: %v", err)
	}
	defer instancer.Stop()
	endpointer := sd.NewEndpointer(instancer, newFactory(""), logger)

	if _, err := endpointer.Endpoints(); err != nil {
		t.Fatal(err)
	}
}

func TestBadFactory(t *testing.T) {
	client := newFakeClient()

	instancer, err := NewInstancer(client, path, logger)
	if err != nil {
		t.Fatalf("failed to create new Instancer: %v", err)
	}
	defer instancer.Stop()
	endpointer := sd.NewEndpointer(instancer, newFactory("kaboom"), logger)

	// instance1 came online
	client.AddService(path+"/instance1", "kaboom")

	// instance2 came online
	client.AddService(path+"/instance2", "zookeeper_node_data")

	if err = asyncTest(100*time.Millisecond, 1, endpointer); err != nil {
		t.Error(err)
	}
}

func TestServiceUpdate(t *testing.T) {
	client := newFakeClient()

	instancer, err := NewInstancer(client, path, logger)
	if err != nil {
		t.Fatalf("failed to create new Instancer: %v", err)
	}
	defer instancer.Stop()
	endpointer := sd.NewEndpointer(instancer, newFactory(""), logger)

	endpoints, err := endpointer.Endpoints()
	if err != nil {
		t.Fatal(err)
	}
	if want, have := 0, len(endpoints); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	// instance1 came online
	client.AddService(path+"/instance1", "zookeeper_node_data1")

	// instance2 came online
	client.AddService(path+"/instance2", "zookeeper_node_data2")

	// we should have 2 instances
	if err = asyncTest(100*time.Millisecond, 2, endpointer); err != nil {
		t.Error(err)
	}

	// TODO(pb): this bit is flaky
	//
	//// watch triggers an error...
	//client.SendErrorOnWatch()
	//
	//// test if error was consumed
	//if err = client.ErrorIsConsumedWithin(100 * time.Millisecond); err != nil {
	//	t.Error(err)
	//}

	// instance3 came online
	client.AddService(path+"/instance3", "zookeeper_node_data3")

	// we should have 3 instances
	if err = asyncTest(100*time.Millisecond, 3, endpointer); err != nil {
		t.Error(err)
	}

	// instance1 goes offline
	client.RemoveService(path + "/instance1")

	// instance2 goes offline
	client.RemoveService(path + "/instance2")

	// we should have 1 instance
	if err = asyncTest(100*time.Millisecond, 1, endpointer); err != nil {
		t.Error(err)
	}
}

func TestBadInstancerCreate(t *testing.T) {
	client := newFakeClient()
	client.SendErrorOnWatch()

	instancer, err := NewInstancer(client, path, logger)
	if err == nil {
		t.Error("expected error on new Instancer")
	}
	if instancer != nil {
		t.Error("expected Instancer not to be created")
	}
	instancer, err = NewInstancer(client, "BadPath", logger)
	if err == nil {
		t.Error("expected error on new Instancer")
	}
	if instancer != nil {
		t.Error("expected Instancer not to be created")
	}
}