-
Notifications
You must be signed in to change notification settings - Fork 3
/
xid_test.go
424 lines (355 loc) · 8.72 KB
/
xid_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package main
import (
"context"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/modfin/henry/slicez"
"github.com/rs/xid"
"io/ioutil"
"testing"
"time"
)
func startpg() types.Container {
fmt.Println("Starting postgres")
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
fmt.Println("Pulling image")
_, err = cli.ImagePull(ctx, "postgres", types.ImagePullOptions{})
if err != nil {
panic(err)
}
//defer reader.Close()
fmt.Println("Creating container")
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "postgres",
ExposedPorts: map[nat.Port]struct{}{"5432/tcp": {}},
Env: []string{"POSTGRES_PASSWORD=qwerty"},
}, &container.HostConfig{
PortBindings: nat.PortMap{
"5432/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "5432",
},
},
},
}, nil, nil, "xid_test")
if err != nil {
panic(err)
}
fmt.Println("Starting container")
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{})
for _, c := range containers {
if c.ID == resp.ID {
return c
}
}
panic("could not find container")
}
func stoppg(c types.Container) {
fmt.Println("Stopping postgres")
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
timeout := 5 * time.Second
err = cli.ContainerStop(ctx, c.ID, &timeout)
if err != nil {
panic(err)
}
fmt.Println("Removing postgres")
err = cli.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{})
if err != nil {
panic(err)
}
}
func waitForDb() (*sqlx.DB, error) {
cuttoff := time.Now().Add(10 * time.Second)
for time.Now().Before(cuttoff) {
time.Sleep(time.Second / 2)
db, err := sqlx.Open("postgres", "postgres://postgres:qwerty@localhost:5432/postgres?sslmode=disable")
if err != nil {
//fmt.Println(1, err)
continue
}
_, err = db.Exec("SELECT 1")
if err != nil {
//fmt.Println(2, err)
continue
}
return db, nil
}
return nil, errors.New("could not connect to database withing 10 sec")
}
func installXid(db *sqlx.DB) {
fmt.Println("Install ./xid.sql")
b, err := ioutil.ReadFile("./xid.sql")
if err != nil {
panic(err)
}
_, err = db.Exec(string(b))
if err != nil {
panic(err)
}
_, err = db.Exec("SELECT setval('xid_serial', 16777215/2);")
if err != nil {
panic(err)
}
}
func TestXid(t *testing.T) {
c := startpg()
defer stoppg(c)
db, err := waitForDb()
if err != nil {
panic(err)
}
installXid(db)
t.Run("Generate", generate(db))
t.Run("GenerateAt", generateAt(db))
t.Run("Encoding", encoding(db))
t.Run("Decoding", decoding(db))
t.Run("Inspection", inspection(db))
t.Run("CyclingCounter", testCycle(db))
}
func testCycle(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
off := 100
max := 16777215 + 1
start := max - off
_, err := db.Exec("SELECT setval('xid_serial', $1);", start-1)
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
var xs []string
err = db.Select(&xs, "SELECT xid() FROM generate_series(1,$1)", off*2)
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
if len(xs) != off*2 {
t.Log("expected len ==", off*2, "got", len(xs))
t.Fail()
}
for i, str := range xs {
x, err := xid.FromString(str)
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
counter := x.Counter()
if counter != int32((start+i)%max) {
t.Logf("expected counter %v, got %v", int32((start+i)%max), counter)
t.Fail()
}
}
}
}
func inspection(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
t.Run("Time", inspectTime(db))
t.Run("Counter", inspectCounter(db))
t.Run("Pid", inspectPid(db))
t.Run("Machine", inspectMachine(db))
}
}
func inspectMachine(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 10; i++ {
x := xid.New()
var got pq.Int32Array
err := db.Get(&got, "SELECT xid_machine($1)", x.String())
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
exp := slicez.Map(x.Machine(), func(a byte) int32 {
return int32(a)
})
if !slicez.Equal(exp, got) {
t.Logf("expected %v, got %v", exp, got)
t.Fail()
}
}
}
}
func inspectPid(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 10; i++ {
x := xid.New()
var got uint16
err := db.Get(&got, "SELECT xid_pid($1)", x.String())
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
exp := x.Pid()
if exp != got {
t.Logf("expected %v, got %v", exp, got)
t.Fail()
}
}
}
}
func inspectCounter(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 100; i++ {
x := xid.New()
var got int32
err := db.Get(&got, "SELECT xid_counter($1)", x.String())
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
exp := x.Counter()
if exp != got {
t.Logf("expected %v, got %v", exp, got)
t.Fail()
}
}
}
}
func inspectTime(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 100; i++ {
refTime := time.Now().
Add(time.Duration(-i) * time.Millisecond).
Add(time.Duration(-i) * time.Hour).
Add(time.Duration(-i) * time.Minute).
Add(time.Duration(-i) * time.Second)
x := xid.NewWithTime(refTime)
var got time.Time
err := db.Get(&got, "SELECT xid_time($1)", x.String())
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
exp := x.Time()
if !exp.Equal(got) {
t.Logf("expected %s, got %s", exp, got)
t.Fail()
}
}
}
}
func encoding(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 100; i++ {
x := xid.New()
b := slicez.Map(x.Bytes(), func(b byte) int32 { return int32(b) })
var str string
err := db.Get(&str, "SELECT xid_encode($1)", pq.Int32Array(b))
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
if str != x.String() {
t.Logf("expected %s, got %s", x.String(), str)
t.Fail()
}
}
}
}
func decoding(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
for i := 0; i < 100; i++ {
x := xid.New()
str := x.String()
var res pq.Int32Array
err := db.Get(&res, "SELECT xid_decode($1)", str)
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
b := slicez.Map(x.Bytes(), func(b byte) int32 { return int32(b) })
if !slicez.Equal(b, res) {
t.Logf("expected %v, got %v", b, str)
t.Fail()
}
}
}
}
func generate(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
var ids []string
start := time.Now().Add(-time.Second)
err := db.Select(&ids, "SELECT xid() FROM generate_series(1, 100)")
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
done := time.Now()
var last xid.ID
for i, id := range ids {
x, err := xid.FromString(id)
if err != nil {
t.Log("expected a valid xid, got", err)
t.Fail()
}
if !start.Before(x.Time()) {
t.Logf("expected xid to contain timestamp after %v, but it held %v", start, x.Time())
t.Fail()
}
if !x.Time().Before(done) {
t.Logf("expected xid to contain timestamp before %v, but it held %v", done, x.Time())
t.Fail()
}
if i > 0 {
if last.Counter()+1 != x.Counter() {
t.Logf("expected xid counter to be incremented by 1, last %v, but it held %v", last.Counter(), x.Counter())
t.Fail()
}
}
last = x
}
}
}
func generateAt(db *sqlx.DB) func(t *testing.T) {
return func(t *testing.T) {
var last xid.ID
for i := 0; i < 1000; i++ {
refTime := time.Now().
Add(time.Duration(-i*3) * time.Millisecond).
Add(time.Duration(-i) * time.Hour).
Add(time.Duration(-i) * time.Minute).
Add(time.Duration(-i) * time.Second)
var str string
err := db.Get(&str, "SELECT xid(_at => $1)", refTime)
if err != nil {
t.Log("unexpected err, got", err)
t.Fail()
}
expTime := xid.NewWithTime(refTime).Time()
got, err := xid.FromString(str)
if err != nil {
t.Log("expected a valid xid, got", err, str)
t.Fail()
return
}
if !expTime.Equal(got.Time()) {
t.Logf("expected %s, got %s", expTime, got.Time())
t.Fail()
}
if i > 0 {
if last.Counter()+1 != got.Counter() {
t.Logf("expected xid counter to be incremented by 1, last %v, but it held %v", last.Counter(), got.Counter())
t.Fail()
}
}
last = got
}
}
}