-
Notifications
You must be signed in to change notification settings - Fork 3
/
hashingstorage_test.go
108 lines (68 loc) · 2.84 KB
/
hashingstorage_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
package main
import (
"testing"
)
func TestHashingSetAndGetHashing(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("babab"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 5, "invalid byte lenght")
assertEquals(t, string(content), "babab", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestHashingSetShouldUpdateCas(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
_, _, cas_before, _, _ := storage.Get("foo")
storage.Set("foo", 0, 60, 5, []byte("bbbbb"))
_, _, cas_after, _, _ := storage.Get("foo")
assertNotEquals(t, cas_before, cas_after, "Invalid cas update")
}
func TestHashingAddShouldFailIfKeyAlreadyExists(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Add("foo", 1, 30, 4, []byte("bbbb"))
assertNotEquals(t, err, nil, "failed to add")
}
func TestHashingAddShouldAddIfNotExists(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Add("bar", 1, 30, 4, []byte("bbbb"))
assertEquals(t, err, nil, "failed to add")
}
func TestHashingShouldReplaceIfExists(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Replace("foo", 1, 120, 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 1, "invalid flag")
assertEquals(t, int(bytes), 4, "invalid byte lenght")
assertEquals(t, string(content), "bbbb", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestHashingReplaceShouldFailIfKeyNotExists(t *testing.T) {
storage := newHashingStorage(10)
err := storage.Replace("foo", 0, 60, 4, []byte("aaaa"))
assertNotEquals(t, err, nil, "invalid error")
}
func TestHashingShouldAppendContentForKey(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Append("foo", 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 9, "invalid byte lenght")
assertEquals(t, string(content), "aaaaabbbb", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}
func TestHashingShouldPrependContentForKey(t *testing.T) {
storage := newHashingStorage(10)
storage.Set("foo", 0, 60, 5, []byte("aaaaa"))
err := storage.Prepend("foo", 4, []byte("bbbb"))
flag, bytes, _, content, err := storage.Get("foo")
assertEquals(t, int(flag), 0, "invalid flag")
assertEquals(t, int(bytes), 9, "invalid byte lenght")
assertEquals(t, string(content), "bbbbaaaaa", "invalid content")
assertEquals(t, err, nil, "Invalid err ")
}