forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
62 lines (56 loc) · 1.64 KB
/
id_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
package snowflake
import (
"testing"
platform "github.com/influxdata/influxdb/v2"
)
func TestIDLength(t *testing.T) {
gen := NewIDGenerator()
id := gen.ID()
if !id.Valid() {
t.Fail()
}
enc, _ := id.Encode()
if len(enc) != platform.IDLength {
t.Fail()
}
}
func TestToFromString(t *testing.T) {
gen := NewIDGenerator()
id := gen.ID()
var clone platform.ID
if err := clone.DecodeFromString(id.String()); err != nil {
t.Error(err)
} else if id != clone {
t.Errorf("id started as %x but got back %x", id, clone)
}
}
func TestWithMachineID(t *testing.T) {
gen := NewIDGenerator(WithMachineID(1023))
if gen.Generator.MachineID() != 1023 {
t.Errorf("expected machineID of %d but got %d", 1023, gen.Generator.MachineID())
}
gen = NewIDGenerator(WithMachineID(1023))
if gen.Generator.MachineID() != 1023 {
t.Errorf("expected machineID of %d but got %d", 1023, gen.Generator.MachineID())
}
gen = NewIDGenerator(WithMachineID(99))
if gen.Generator.MachineID() != 99 {
t.Errorf("expected machineID of %d but got %d", 99, gen.Generator.MachineID())
}
gen = NewIDGenerator(WithMachineID(101376))
if gen.Generator.MachineID() != 0 {
t.Errorf("expected machineID of %d but got %d", 0, gen.Generator.MachineID())
}
gen = NewIDGenerator(WithMachineID(102399))
if gen.Generator.MachineID() != 1023 {
t.Errorf("expected machineID of %d but got %d", 1023, gen.Generator.MachineID())
}
}
func TestGlobalMachineID(t *testing.T) {
if !globalmachineID.set {
t.Error("expected global machine ID to be set")
}
if GlobalMachineID() < 0 || GlobalMachineID() > 1023 {
t.Error("expected global machine ID to be between 0 and 1023 inclusive")
}
}