-
Notifications
You must be signed in to change notification settings - Fork 2
/
imdgo_test.go
104 lines (83 loc) · 1.79 KB
/
imdgo_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
package imdgo
import (
"log"
"net"
"os"
"testing"
"time"
)
func getLocalAddr() string {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
log.Printf("IMDGO: hostname: %s", hostname)
addresses, err := net.LookupIP(hostname)
if err != nil {
panic(err)
}
return addresses[0].String()
}
func TestCacheNew(t *testing.T) {
c := &Config{Members: []string{getLocalAddr()}}
s, err := New(c)
if err != nil {
t.Fatalf("failed creating store: %s", err.Error())
}
t.Cleanup(func() {
s.Close()
})
}
func TestStoreOperations(t *testing.T) {
c := &Config{Members: []string{getLocalAddr()}}
s, err := New(c)
if err != nil {
t.Fatalf("failed creating store: %s", err.Error())
}
// Wait for the leader to be elected
time.Sleep(3 * time.Second)
value := "value"
key := "key"
t.Run("set operation", func(t *testing.T) {
err := s.Set(key, value)
if err != nil {
t.Fatalf("failed to put item: %s", err.Error())
}
})
t.Run("get operation", func(t *testing.T) {
got, ok := s.Get(key)
if !ok {
t.Errorf("failed to get item: %s", err.Error())
}
if got != value {
t.Errorf("want %s but got %s", value, got)
}
})
t.Run("delete operation", func(t *testing.T) {
err := s.Delete(key)
if err != nil {
t.Errorf("failed to delete item: %s", err.Error())
}
item, _ := s.Get(key)
if item != nil {
t.Errorf("failed to delete item, it is still in the cache: %s", item)
}
})
t.Run("count operation", func(t *testing.T) {
got := s.Count()
if got != 0 {
t.Errorf("want 0 but got %d", got)
}
})
t.Cleanup(func() {
s.Close()
})
}
func Test_getCurrentNodeAddress(t *testing.T) {
want := getLocalAddr()
members := []string{want, "123.123.123.123:9"}
got := getHostAddr(members)
if got != want {
t.Errorf("want %s, got %s", want, got)
}
}