This repository was archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlockservice_stubbed_test.go
154 lines (118 loc) · 2.71 KB
/
lockservice_stubbed_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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package locker
import "testing"
const name = "myservice"
func TestGetOnValidLock(t *testing.T) {
expectedValue := "http://hostname"
store := &memoryStore{}
store.set(name, expectedValue)
val, err := Client{store}.Get(name)
if err != nil {
t.Fatal(err)
}
if val != expectedValue {
t.Errorf("Expected value to be '%s' got '%s'", expectedValue, val)
}
}
func TestGetOnMissingLockReturnsLockNotFound(t *testing.T) {
store := &memoryStore{}
val, err := Client{store}.Get(name)
if err == nil {
t.Error("Expected an error, didn't get one")
}
if _, ok := err.(LockNotFound); !ok {
t.Errorf("Expected lock not found, got different error %s", err)
}
if val != "" {
t.Errorf("Expected no value, got '%s'", val)
}
}
func TestLockSetsKeyValue(t *testing.T) {
store := &memoryStore{}
client := Client{store}
owned := make(chan bool)
quit := make(chan bool)
go client.Lock(name, "host", owned, quit)
select {
case change := <-owned:
if change != true {
t.Errorf("Expected initial Won state, got %s", change)
}
}
v, _ := store.Get(name)
if v != "host" {
t.Error("Expected key to be set")
}
quit <- true
}
func TestWatchReturnsInitialValue(t *testing.T) {
store := &memoryStore{}
client := Client{store}
valueChanges := make(chan string)
quit := make(chan bool)
store.set(name, "value")
go client.Watch(name, valueChanges, quit)
select {
case <-timeout():
t.Fatal("Timeout")
case change := <-valueChanges:
if change != "value" {
t.Error("Expected value to be 'value'")
}
}
quit <- true
}
func TestWatch(t *testing.T) {
store := &memoryStore{}
client := Client{store}
valueChanges := make(chan string)
quit := make(chan bool)
go client.Watch(name, valueChanges, quit)
select {
case <-timeout():
t.Fatal("Timeout")
case change := <-valueChanges:
if change != "" {
t.Error("Expected initial blank value inidicating missing lock")
}
}
store.set(name, "value")
select {
case <-timeout():
t.Fatal("Timeout")
case change := <-valueChanges:
if change != "value" {
t.Error("Expected value to change to 'value'")
}
}
quit <- true
}
// For testing purposes
type memoryStore struct {
cache map[string]string
}
func (c *memoryStore) ensureCache() {
if c.cache == nil {
c.cache = make(map[string]string)
}
}
func (c *memoryStore) set(key, value string) {
c.ensureCache()
c.cache[key] = value
}
func (c *memoryStore) Get(name string) (string, error) {
c.ensureCache()
if v, ok := c.cache[name]; ok {
return v, nil
}
return "", LockNotFound{name}
}
func (c *memoryStore) AcquireOrFreshenLock(name, value string) error {
c.ensureCache()
if v, ok := c.cache[name]; ok {
if v != value {
return LockDenied{name}
}
}
c.cache[name] = value
return nil
}