-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
68 lines (56 loc) · 1.34 KB
/
store_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
package gokvstore
import (
"testing"
"sync"
"fmt"
)
func TestStore_Init(t *testing.T) {
var s *Store
s = &Store{}
s.Init()
if s.storage == nil {
t.Error("Expected s.storage to not be nil")
}
}
func TestStore_WriteItem(t *testing.T) {
s := &Store{}
s.Init()
s.WriteItem(storageItem{Key: "test", Value: true})
if val, _ := s.GetItem(storageItem{Key: "test"}); val != true {
t.Error("Expected s.GetItem(test) to return true, got", val)
}
}
func TestStore_GetItem(t *testing.T) {
s := &Store{}
s.Init()
if _, err := s.GetItem(storageItem{Key: "wrong"}); err == nil {
t.Error("Expected s.GetItem(wrong) to throw an error with wrong key, got", err)
}
s.WriteItem(storageItem{Key: "test", Value: true})
if _, err := s.GetItem(storageItem{Key: "test"}); err != nil {
t.Error("Expected s.GetItem(test) to throw NO error with wrong key, got", err)
}
}
func TestConcurrent(t *testing.T) {
s := &Store{}
s.Init()
wg := &sync.WaitGroup{}
wg.Add(3)
// Run 3 writes in goroutines, which should run in order thanks to the mutex
go func() {
s.WriteItem(storageItem{"test", 1})
wg.Done()
}()
go func() {
s.WriteItem(storageItem{"test", 2})
wg.Done()
}()
go func() {
s.WriteItem(storageItem{"test", 3})
wg.Done()
}()
wg.Wait()
if val, _ := s.GetItem(storageItem{Key: "test"}); val != 3 {
t.Error("val != 3, got", val)
}
}