-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_internal_test.go
129 lines (118 loc) · 2.95 KB
/
config_internal_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
package snowfake
import (
"bytes"
"errors"
"fmt"
"reflect"
"testing"
)
func TestInit(t *testing.T) {
name := func(nodeBits, seqBits uint8) string {
return fmt.Sprintf("nodeBits=%d.stepBits=%d", nodeBits, seqBits)
}
tests := []struct {
epoch uint64
nodeBits uint8
seqBits uint8
expectedEpoch uint64
expectedTimeShift uint8
expectedNodeShift uint8
expectedSeqShift uint8
expectedTimeMask uint64
expectedNodeMask uint64
expectedSeqMask uint64
expectedMaxNode uint64
expectedErr error
}{
{
epoch: uint64(1577836800),
nodeBits: 16,
seqBits: 16,
expectedEpoch: uint64(1577836800),
expectedTimeShift: uint8(32),
expectedNodeShift: uint8(16),
expectedSeqShift: uint8(0),
expectedTimeMask: 0xFFFFFFFF00000000,
expectedNodeMask: 0xFFFF0000,
expectedSeqMask: 0xFFFF,
expectedMaxNode: 1 << 16,
expectedErr: nil,
},
{
epoch: uint64(1577836800),
nodeBits: 8,
seqBits: 4,
expectedEpoch: uint64(1577836800),
expectedTimeShift: uint8(12),
expectedNodeShift: uint8(4),
expectedSeqShift: uint8(0),
expectedTimeMask: 0xFFFFFFFF000,
expectedNodeMask: 0xFF0,
expectedSeqMask: 0xF,
expectedMaxNode: 1 << 8,
expectedErr: nil,
},
{
epoch: uint64(1577836800),
nodeBits: 2,
seqBits: 0,
expectedEpoch: uint64(1577836800),
expectedTimeShift: uint8(2),
expectedNodeShift: uint8(0),
expectedSeqShift: uint8(0),
expectedTimeMask: 0b1111111111111111111111111111111100,
expectedNodeMask: 0b11,
expectedSeqMask: 0b0,
expectedMaxNode: 1 << 2,
expectedErr: nil,
},
{
epoch: uint64(1577836800),
nodeBits: 17,
seqBits: 16,
expectedErr: errors.New("nodeBits + seqBits should has 32 in total"),
},
}
for _, tt := range tests {
t.Run(name(tt.nodeBits, tt.seqBits), func(t *testing.T) {
SetEpoch(tt.epoch)
SetNodeBits(tt.nodeBits)
SetSeqBits(tt.seqBits)
err := Init()
assertEqual(t, tt.expectedErr, err)
if err == nil {
assertEqual(t, tt.expectedEpoch, epoch)
assertEqual(t, tt.expectedTimeShift, timeShift)
assertEqual(t, tt.expectedNodeShift, nodeShift)
assertEqual(t, tt.expectedSeqShift, seqShift)
assertEqual(t, tt.expectedTimeMask, timeMask)
assertEqual(t, tt.expectedNodeMask, nodeMask)
assertEqual(t, tt.expectedSeqMask, seqMask)
assertEqual(t, tt.expectedMaxNode, maxNode)
}
})
}
}
func assertEqual(t *testing.T, expected, actual interface{}) {
t.Helper()
if !isEqual(expected, actual) {
t.Errorf("got %v, expected %v", actual, expected)
}
}
func isEqual(expected, actual interface{}) bool {
if expected == nil || actual == nil {
return expected == actual
}
exp, ok := expected.([]byte)
if !ok {
return reflect.DeepEqual(expected, actual)
}
act, ok := actual.([]byte)
if !ok {
return false
}
if exp == nil || act == nil {
return exp == nil && act == nil
}
return bytes.Equal(exp, act)
}