-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseqid_test.go
46 lines (42 loc) · 970 Bytes
/
seqid_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
package idgen
import (
"testing"
"fmt"
)
func Test_seqNextId(t *testing.T) {
ig := NewSeqIdGenerator(1024, 0)
if ig != nil {
t.Fatal("nil expected")
}
ig = NewSeqIdGenerator(1, 0)
lastId := ig.NextID()
fmt.Printf("firstId: %d (%b)\n", lastId, lastId)
workerId, sequence := DecomposeSeq(lastId)
fmt.Printf("workerId: %d, sequence: %d\n", workerId, sequence)
for i:=0; i<10; i++ {
newId := ig.NextID()
fmt.Printf("#%d : %d (%b)\n", i, newId, newId)
if newId < lastId {
t.Fatal("newId is less than lastId")
}
lastId = newId
}
ig.Exit()
fmt.Printf("------------done for seqid --------------\n")
}
func Benchmark_seqNextId(b *testing.B) {
ig := NewSeqIdGenerator(1, 0)
lastId := ig.NextID()
for i:=0; i<b.N; i++ {
id := ig.NextID()
// ig.NextID()
if id < lastId {
fmt.Printf("\n")
fmt.Printf("lastId: %d (%b)\n", lastId, lastId)
fmt.Printf("newId: %d (%b)\n", id, id)
b.Fatal("failed")
}
lastId = id
}
ig.Exit()
}