-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_test.go
69 lines (61 loc) · 1.71 KB
/
driver_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
package nscache
import (
"errors"
"net/url"
"sync/atomic"
"testing"
"time"
)
func TestRegister_WithNilCacheFactory(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Errorf("register a nil cache factory expect to panic but not")
} else if !errors.Is(err.(error), errCacheDriverFactoryIsNil) {
t.Errorf("expect to get error => %v, but get error => %v", errCacheDriverFactoryIsNil, err)
}
}()
Register("nil-cache", nil)
}
func TestRegister_WithInvalidDriverName(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Errorf("register a cache factory with an invalid name that expects to panic but not")
} else if !errors.Is(err.(error), errInvalidCacheDriverName) {
t.Errorf("expect to get error => %v, but get error => %v", errInvalidCacheDriverName, err)
}
}()
Register("invalid_cache:", mockFactory)
}
func TestRegister_WithRepeatedCacheFactory(t *testing.T) {
driverName := "repeated-cache"
overwritten := Register(driverName, mockFactory)
if overwritten {
t.Errorf("register cache factory '%s' once, expect get overwritten false but get true", driverName)
return
}
overwritten = Register(driverName, mockFactory)
if !overwritten {
t.Errorf("register cache factory '%s' twice, expect get overwritten true but get false", driverName)
}
}
func TestRegister_WithConcurrent(t *testing.T) {
driverName := "concurrent-cache"
var stop int32
go func() {
for atomic.LoadInt32(&stop) == 0 {
Register(driverName, mockFactory)
}
}()
go func() {
for atomic.LoadInt32(&stop) == 0 {
Register(driverName, mockFactory)
}
}()
<-time.After(time.Second * 3)
atomic.StoreInt32(&stop, 1)
}
var mockFactory = func(conn *url.URL) (NSCache, error) {
return nil, nil
}