This repository has been archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 594
/
backend_etcd_test.go
96 lines (78 loc) · 2.58 KB
/
backend_etcd_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
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
package discover
import (
"fmt"
"runtime"
"strings"
"testing"
"github.com/coreos/go-etcd/etcd"
)
func deleteService(client *etcd.Client, service string, addr string) {
client.Delete(fmt.Sprintf("/services/%s/%s", service, addr))
}
const (
NoAttrService = "null"
)
func TestEtcdBackend_RegisterAndUnregister(t *testing.T) {
// TODO Create server here itself and connect to it.
client := etcd.NewClient()
backend := EtcdBackend{Client: client}
serviceName := "test_register"
serviceAddr := "127.0.0.1"
deleteService(client, serviceName, serviceAddr)
backend.Register(serviceName, serviceAddr, nil)
servicePath := KeyPrefix + "/services/" + serviceName + "/" + serviceAddr
results, err := client.Get(servicePath)
if err != nil {
t.Fatal(err)
}
// Adding the case where the result is checked.
if len(results) < 1 {
t.Fatal("Error: No Response From Server")
} else {
// Check if the files the returned values are the same.
if (results[0].Key != servicePath) || (results[0].Value != NoAttrService) {
t.Fatal("Returned value not equal to sent one")
}
}
backend.Unregister(serviceName, serviceAddr)
_, err = client.Get(servicePath)
if err == nil {
t.Fatal("Value not deleted after unregister")
}
}
func TestEtcdBackend_Subscribe(t *testing.T) {
client := etcd.NewClient()
backend := EtcdBackend{Client: client}
backend.Register("test_subscribe", "10.0.0.1", nil)
defer backend.Unregister("test_subscribe", "10.0.0.1")
backend.Register("test_subscribe", "10.0.0.2", nil)
defer backend.Unregister("test_subscribe", "10.0.0.2")
updates, _ := backend.Subscribe("test_subscribe")
runtime.Gosched()
backend.Register("test_subscribe", "10.0.0.3", nil)
defer backend.Unregister("test_subscribe", "10.0.0.3")
backend.Register("test_subscribe", "10.0.0.4", nil)
defer backend.Unregister("test_subscribe", "10.0.0.4")
for i := 0; i < 5; i++ {
update := <-updates.Chan()
if update.Addr == "" && update.Name == "" {
continue // skip the update that signals "up to current" event
}
if update.Online != true {
t.Fatal("Unexpected offline service update: ", update, i)
}
if !strings.Contains("10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4", update.Addr) {
t.Fatal("Service update of unexected addr: ", update, i)
}
}
backend.Register("test_subscribe", "10.0.0.5", nil)
backend.Unregister("test_subscribe", "10.0.0.5")
<-updates.Chan() // .5 comes online
update := <-updates.Chan() // .5 goes offline
if update.Addr != "10.0.0.5" {
t.Fatal("Unexpected addr: ", update)
}
if update.Online != false {
t.Fatal("Expected service to be offline:", update)
}
}