-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_test.go
123 lines (107 loc) · 2.61 KB
/
generator_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
package generator_test
import (
"testing"
"time"
generator "github.com/kabi175/snowflake-go"
)
type genCase struct {
arg0 int64
arg1 int64
}
var newGenSuccess = []genCase{
{arg0: 0, arg1: 0},
{arg0: 1023, arg1: 0},
{arg0: 0, arg1: time.Now().UnixMilli()},
{arg0: 478, arg1: time.Now().UnixMilli()},
{arg0: 1023, arg1: time.Now().UnixMilli()},
}
var newGenFails = []genCase{
{arg0: -1, arg1: 0},
{arg0: 0, arg1: -1},
{arg0: 0, arg1: time.Now().UnixMilli() + (1000 * 2)},
{arg0: 1024, arg1: -1},
{arg0: 1009, arg1: -1},
}
func TestNewGenerator(t *testing.T) {
t.Parallel()
var gen *generator.Generator
var err error
t.Run("should pass", func(t *testing.T) {
for _, test := range newGenSuccess {
t.Logf("Case arg0:%v arg1:%v", test.arg0, test.arg1)
gen, err = generator.NewGenerator(test.arg0, test.arg1)
if gen == nil {
t.Errorf("gen: can't be nil")
}
if err != nil {
t.Errorf("err: expected nil got %e", err)
}
}
})
t.Run("should fail", func(t *testing.T) {
for _, test := range newGenFails {
t.Logf("Case arg0:%v arg1:%v", test.arg0, test.arg1)
gen, err = generator.NewGenerator(test.arg0, test.arg1)
if gen != nil {
t.Errorf("gen: should be nil")
}
if err == nil {
t.Errorf("err: expected error got nil")
}
}
})
}
func TestNext(t *testing.T) {
t.Parallel()
t.Run("same id check", func(t *testing.T) {
t.Parallel()
gen1, err := generator.NewGenerator(0, 0)
if err != nil {
t.Errorf("err: expected nil got %e", err)
}
gen2, err := generator.NewGenerator(1, 0)
if err != nil {
t.Errorf("err: expected nil got %e", err)
}
for iter := 0; iter < 1000; iter++ {
next1, err1 := gen1.Next()
next2, err2 := gen2.Next()
if err1 != nil || err2 != nil {
continue
}
if next1 == next2 {
t.Error("ids should't be same")
}
}
})
t.Run("backward id check", func(t *testing.T) {
t.Parallel()
gen, err := generator.NewGenerator(0, 0)
if err != nil {
t.Errorf("err: expected nil got %e", err)
}
var prev int64
for iter := 0; iter < 10000; iter++ {
curr, err := gen.Next()
if curr == 0 && err == nil {
t.Error("err: expected error got nil")
}
if curr < prev {
t.Errorf("curr id value less than prev id curr:%v prev:%v", curr, prev)
}
}
})
t.Run("negative id check", func(t *testing.T) {
t.Parallel()
gen, err := generator.NewGenerator(0, 0)
if err != nil {
t.Errorf("err: expected nil got %e", err)
}
for iter := 0; iter < 10000; iter++ {
_, err := gen.Next()
if err != nil && err.Error() == "negative id genrated" {
t.Errorf("err: expected nil got %v", err)
}
}
})
}