This repository was archived by the owner on Sep 29, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Expand file tree Collapse file tree 2 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -4,9 +4,23 @@ import "time"
4
4
5
5
var chars = []byte ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_" )
6
6
7
+ type clock interface {
8
+ Now () time.Time
9
+ }
10
+
11
+ type timeClock struct {}
12
+
13
+ func (timeClock ) Now () time.Time {
14
+ return time .Now ()
15
+ }
16
+
7
17
// Timestamp returns a string based on different nano time.
8
18
func Timestamp () string {
9
- now := time .Now ().UnixNano ()
19
+ return TimestampFromClock (timeClock {})
20
+ }
21
+
22
+ func TimestampFromClock (c clock ) string {
23
+ now := c .Now ().UnixNano ()
10
24
ret := make ([]byte , 0 , 16 )
11
25
for now > 0 {
12
26
ret = append (ret , chars [int (now % int64 (len (chars )))])
Original file line number Diff line number Diff line change 1
1
package base
2
2
3
3
import (
4
- "testing"
5
-
6
4
"github.com/stretchr/testify/assert"
5
+ "testing"
6
+ "time"
7
7
)
8
8
9
- func TestTimestamp (t * testing.T ) {
9
+ type testClock struct {
10
+ now time.Time
11
+ }
12
+
13
+ func (c testClock ) Now () time.Time {
14
+ return c .now
15
+ }
16
+
17
+ func TestTimestampFromClock (t * testing.T ) {
10
18
should := assert .New (t )
11
- t1 := Timestamp ( )
12
- t2 := Timestamp ( )
19
+ t1 := TimestampFromClock ( testClock { time . Unix ( 0 , 1000 )} )
20
+ t2 := TimestampFromClock ( testClock { time . Unix ( 0 , 2000 )} )
13
21
should .NotEmpty (t1 )
14
22
should .NotEmpty (t2 )
15
23
should .NotEqual (t1 , t2 )
You can’t perform that action at this time.
0 commit comments