-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdata_source_test.go
80 lines (67 loc) · 2.37 KB
/
data_source_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
package main
import (
"testing"
"time"
)
func TestCachedDataSource_CanReuseAndExpireGetNodeInfo(t *testing.T) {
fds := new(fakeDataSource)
ds := NewCachedDataSource(fds)
cds := ds.(*cachedDataSource)
cds.cacheTTL = time.Millisecond * 10
if cds.cachedGetNodeInfo != nil || time.Now().Before(cds.cachedGetNodeInfoExpiresAt) {
t.Fatal("No cache should be set after initialize")
}
// Read from data source
cds.GetNodeInfo()
if cds.cachedGetNodeInfo == nil {
t.Fatal("Cache was not set after calling GetNodeInfo")
}
if cds.cachedGetNodeInfoExpiresAt.Before(time.Now()) {
t.Fatal("Cache expiration time was not set after calling GetNodeInfo")
}
// Read from cache (update value before to verify the read is cached)
fds.byteLag = 1337
nodeInfo, _ := cds.GetNodeInfo()
if nodeInfo != cds.cachedGetNodeInfo && nodeInfo.ByteLag != 0 {
t.Fatal("Did not use the cached GetNodeInfo value when cached read was expected")
}
// Wait for cache to expire
time.Sleep(cds.cacheTTL)
if !cds.cachedGetNodeInfoExpiresAt.Before(time.Now()) {
t.Fatal("Need to wait longer before cache will expire")
}
// Read from data source
nodeInfo, _ = cds.GetNodeInfo()
if cds.cachedGetNodeInfo.ByteLag != 1337 {
t.Fatal("Cache was not successfully expired")
}
}
func TestCachedDataSource_CanReuseAndExpireIsInRecovery(t *testing.T) {
fds := new(fakeDataSource)
ds := NewCachedDataSource(fds)
cds := ds.(*cachedDataSource)
cds.cacheTTL = time.Millisecond * 10
if cds.cachedIsInRecovery == true || time.Now().Before(cds.cachedIsInRecoveryExpiresAt) {
t.Fatal("No cache should be set after initialize")
}
// Read from data source
cds.IsInRecovery()
if cds.cachedIsInRecoveryExpiresAt.Before(time.Now()) {
t.Fatal("Cache expiration time was not set after calling IsInRecovery")
}
// Read from cache (update value before to verify the read is cached)
isInRecovery, _ := cds.IsInRecovery()
if isInRecovery != cds.cachedIsInRecovery {
t.Fatal("Did not use the cached IsInRecovery value when cached read was expected")
}
// Wait for cache to expire
time.Sleep(cds.cacheTTL)
if !cds.cachedIsInRecoveryExpiresAt.Before(time.Now()) {
t.Fatal("Need to wait longer before cache will expire")
}
// Read from data source
isInRecovery, _ = cds.IsInRecovery()
if cds.cachedIsInRecoveryExpiresAt.Before(time.Now()) {
t.Fatal("Cache expiration time was not set after calling IsInRecovery")
}
}