go.temporal.io/server@v1.23.0/common/clock/hybrid_logical_clock/hybrid_logical_clock_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package hybrid_logical_clock 26 27 import ( 28 "testing" 29 "time" 30 31 "github.com/stretchr/testify/assert" 32 "go.temporal.io/server/common/clock" 33 ) 34 35 func Test_Next_ReturnsGreaterClock(t *testing.T) { 36 t.Parallel() 37 t0 := Zero(1) 38 timesource := clock.NewEventTimeSource() 39 40 // Same wallclock 41 timesource.Update(time.Unix(0, 0).UTC()) 42 t1 := Next(t0, timesource) 43 assert.Equal(t, 1, Compare(t0, t1)) 44 // Greater wallclock 45 timesource.Update(time.Unix(0, 1).UTC()) 46 t2 := Next(t1, timesource) 47 assert.Equal(t, 1, Compare(t1, t2)) 48 } 49 50 func Test_Compare(t *testing.T) { 51 t.Parallel() 52 t0 := Clock{WallClock: 1, Version: 1, ClusterId: 1} 53 t1 := Clock{WallClock: 1, Version: 1, ClusterId: 1} 54 assert.Equal(t, 0, Compare(&t0, &t1)) 55 assert.True(t, Equal(&t0, &t1)) 56 57 t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} 58 t1 = Clock{WallClock: 1, Version: 1, ClusterId: 2} 59 assert.Equal(t, 1, Compare(&t0, &t1)) 60 // Let's get a -1 in there for sanity 61 assert.Equal(t, -1, Compare(&t1, &t0)) 62 63 t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} 64 t1 = Clock{WallClock: 1, Version: 2, ClusterId: 1} 65 assert.Equal(t, 1, Compare(&t0, &t1)) 66 67 t0 = Clock{WallClock: 1, Version: 1, ClusterId: 1} 68 t1 = Clock{WallClock: 2, Version: 1, ClusterId: 1} 69 assert.Equal(t, 1, Compare(&t0, &t1)) 70 71 assert.True(t, Greater(&t1, &t0)) 72 assert.True(t, Less(&t0, &t1)) 73 } 74 75 func Test_Max_ReturnsMaximum(t *testing.T) { 76 t.Parallel() 77 t0 := Zero(1) 78 t1 := Zero(2) 79 80 max := Max(t0, t1) 81 assert.Equal(t, max, t1) 82 // Just in case it doesn't work in reverse order... 83 max = Max(t1, t0) 84 assert.Equal(t, max, t1) 85 } 86 87 func Test_Min_ReturnsMinimum(t *testing.T) { 88 t.Parallel() 89 t0 := Zero(1) 90 t1 := Zero(2) 91 92 min := Min(t0, t1) 93 assert.Equal(t, min, t0) 94 // Just in case it doesn't work in reverse order... 95 min = Min(t1, t0) 96 assert.Equal(t, min, t0) 97 } 98 99 func Test_UTC_ReturnsTimeInUTC(t *testing.T) { 100 t.Parallel() 101 assert.Equal(t, time.Unix(0, 0).UTC(), UTC(Zero(0))) 102 103 timesource := clock.NewEventTimeSource() 104 now := time.Date(1999, 12, 31, 23, 59, 59, 999000000, time.UTC) 105 timesource.Update(now) 106 assert.Equal(t, now, UTC(Next(Zero(0), timesource))) 107 }