-
Notifications
You must be signed in to change notification settings - Fork 3
/
snowflake_test.go
94 lines (84 loc) · 2.14 KB
/
snowflake_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
package snowflake
import (
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/sensorbee/sensorbee.v0/core"
"gopkg.in/sensorbee/sensorbee.v0/data"
"testing"
"time"
)
func TestSnowflake(t *testing.T) {
ctx := core.NewContext(&core.ContextConfig{})
{
s, err := NewState(ctx, data.Map{
"machine_id": data.Int(601),
})
if err != nil {
t.Fatal(err)
}
if err := ctx.SharedStates.Add("test_snowflake", "snowflake_id", s); err != nil {
t.Fatal(err)
}
}
Convey("Given a snowflake state", t, func() {
Convey("when calling the snowflake function within the same millisecond", func() {
var (
now time.Time
v data.Value
err error
)
for {
now = time.Now()
v, err = Snowflake(ctx, data.String("test_snowflake"))
if err != nil {
So(err, ShouldBeNil)
}
if time.Now().Sub(now)/time.Millisecond == 0 {
break
}
}
So(err, ShouldBeNil)
id, err := data.ToInt(v)
So(err, ShouldBeNil)
Convey("the value sholud contain the current millisecond", func() {
elapse := now.UnixNano()/int64(time.Millisecond) -
pluginPublishTime.UnixNano()/int64(time.Millisecond)
So(id&(((1<<41)-1)<<22), ShouldEqual, elapse<<22)
})
Convey("the value should contain the machine id", func() {
So((id>>12)&((1<<10)-1), ShouldEqual, 601)
})
})
Convey("when calling the snowflake function multiple times within the same millisecond", func() {
var ids []int64
for {
now := time.Now()
var a []data.Value
for i := 0; i < 3; i++ {
v, err := Snowflake(ctx, data.String("test_snowflake"))
if err != nil {
So(err, ShouldBeNil)
}
a = append(a, v)
}
if time.Now().Sub(now)/time.Millisecond == 0 {
for _, v := range a {
id, err := data.ToInt(v)
So(err, ShouldBeNil)
ids = append(ids, id)
}
break
}
}
Convey("the ids should have the same prefix", func() {
for i := 1; i < len(ids); i++ {
So(ids[i] & ^((1<<12)-1), ShouldEqual, ids[0] & ^((1<<12)-1))
}
})
Convey("ids should differ 1", func() {
for i := 1; i < len(ids); i++ {
So(ids[i]-ids[i-1], ShouldEqual, 1)
}
})
})
})
}