This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock_test.go
65 lines (52 loc) · 1.67 KB
/
clock_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
package clock
import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Duration(t *testing.T) {
Convey("Given some non-zero duration", t, func(c C) {
d1 := (time.Second * 5) + (time.Nanosecond * 10)
pb := DurationToPB(d1)
So(pb.Seconds, ShouldEqual, 5)
So(pb.Nanos, ShouldEqual, 10)
d2 := DurationFromPB(pb)
So(int64(d2.Seconds()), ShouldEqual, 5)
So(d1, ShouldEqual, d2)
})
}
func Test_TimestampConversion(t *testing.T) {
Convey("Given some non-zero time", t, func(c C) {
now := time.Now()
Convey("When we convert it to a Timestamp protobuf, it should not be nil", func() {
pb := TimeToTimestamp(now)
So(pb, ShouldNotBeNil)
Convey("And when we convert the protobuf back into a time, we should get our original value", func() {
res := TimestampToTime(pb)
So(now, ShouldEqual, res)
})
})
})
Convey("Given some zero time", t, func(c C) {
now := time.Time{}
Convey("When we convert it to a Timestamp protobuf, it should be nil", func() {
pb := TimeToTimestamp(now)
So(pb, ShouldBeNil)
Convey("And when we convert the protobuf back into a time, we should get our original value", func() {
res := TimestampToTime(pb)
So(now, ShouldEqual, res)
})
})
})
}
func Test_timeToNanosecondSecondFraction(t *testing.T) {
Convey("Given some time", t, func(c C) {
var fraction int32
fraction = timeToNanosecondSecondFraction(time.Unix(1000000, 0))
So(fraction, ShouldEqual, 0)
fraction = timeToNanosecondSecondFraction(time.Unix(1000000, 12345678))
So(fraction, ShouldEqual, 12345678)
fraction = timeToNanosecondSecondFraction(time.Unix(1000000, 999999999))
So(fraction, ShouldEqual, 999999999)
})
}