-
Notifications
You must be signed in to change notification settings - Fork 3
/
objectid_test.go
38 lines (35 loc) · 1.21 KB
/
objectid_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
// Copyright 2016 Author YuShuangqi. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tokenauth_test
import (
"github.com/ysqi/tokenauth"
. "gopkg.in/check.v1"
)
func (s *S) TestObjectId_New(c *C) {
// Generate 10 ids
ids := make([]tokenauth.ObjectId, 10)
for i := 0; i < 10; i++ {
ids[i] = tokenauth.NewObjectId()
}
for i := 1; i < 10; i++ {
prevId := ids[i-1]
id := ids[i]
// Test for uniqueness among all other 9 generated ids
for j, tid := range ids {
if j != i {
c.Assert(id, Not(Equals), tid, Commentf("Generated ObjectId is not unique"))
}
}
// Check that timestamp was incremented and is within 30 seconds of the previous one
secs := id.Time().Sub(prevId.Time()).Seconds()
c.Assert((secs >= 0 && secs <= 30), Equals, true, Commentf("Wrong timestamp in generated ObjectId"))
// Check that machine ids are the same
c.Assert(id.Machine(), DeepEquals, prevId.Machine())
// Check that pids are the same
c.Assert(id.Pid(), Equals, prevId.Pid())
// Test for proper increment
delta := int(id.Counter() - prevId.Counter())
c.Assert(delta, Equals, 1, Commentf("Wrong increment in generated ObjectId"))
}
}