-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
143 lines (106 loc) · 2.4 KB
/
cache_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package locache_test
import (
"go/build"
"math/rand"
"path"
"reflect"
"strconv"
"sync"
"syscall"
"testing"
"testing/quick"
"time"
"github.com/esimonov/locache"
)
var testLocNames []string
func init() {
zoneinfoLoc := path.Join(build.Default.GOROOT, "/lib/time/zoneinfo.zip")
testLocNames = loadIANATzNamesFromZip(zoneinfoLoc)
if len(testLocNames) == 0 {
panic("empty location names slice")
}
}
func TestLoadLocation_SafeForConcurrentAccess(t *testing.T) {
wg := new(sync.WaitGroup)
for range 100 {
for _, locName := range testLocNames {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := locache.LoadLocation(locName); err != nil {
panic(err)
}
}()
}
}
wg.Wait()
}
func TestLoadLocation_ReturnArgumentsAreEquivalentToStandardLibraryImplementation_Property(t *testing.T) {
f := func(n int64) bool {
locName := "unknown" + strconv.Itoa(int(n))
n %= int64(len(testLocNames))
if n%2 == 0 {
locName = testLocNames[(n^(n>>63))-(n>>63)]
}
loc1, err1 := time.LoadLocation(locName)
loc2, err2 := locache.LoadLocation(locName)
return reflect.DeepEqual(loc1, loc2) && (err1 == nil) == (err2 == nil)
}
cfg := &quick.Config{
MaxCount: 10000,
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
if err := quick.Check(f, cfg); err != nil {
t.Fatal(err)
}
}
func loadIANATzNamesFromZip(zipfile string) []string {
fd, err := open(zipfile)
if err != nil {
panic(err)
}
defer syscall.Close(int(fd))
const ztailsize = 22
buf := make([]byte, ztailsize)
preadn(fd, buf, -ztailsize)
getn(4, buf)
n := getn(2, buf[10:])
size := getn(4, buf[12:])
off := getn(4, buf[16:])
buf = make([]byte, size)
preadn(fd, buf, off)
ianaNames := make([]string, n)
for i := range n {
getn(4, buf)
namelen := getn(2, buf[28:])
xlen := getn(2, buf[30:])
fclen := getn(2, buf[32:])
ianaNames[i] = string(buf[46 : 46+namelen])
buf = buf[46+namelen+xlen+fclen:]
}
return ianaNames
}
func open(name string) (uintptr, error) {
fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
if err != nil {
return 0, err
}
return uintptr(fd), nil
}
func preadn(fd uintptr, buf []byte, off int) {
whence := 0
if off < 0 {
whence = 2
}
syscall.Seek(int(fd), int64(off), whence)
syscall.Read(int(fd), buf)
}
func getn(n int, b []byte) (result int) {
if len(b) < n {
return 0
}
for i := range n {
result |= int(b[i]) << (i * 8)
}
return result
}