github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/gossip/info_test.go (about) 1 // Copyright 2014 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package gossip 12 13 import ( 14 "testing" 15 "time" 16 17 "github.com/cockroachdb/cockroach/pkg/roachpb" 18 "github.com/cockroachdb/cockroach/pkg/util/hlc" 19 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 20 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 21 ) 22 23 func newInfo(val float64) Info { 24 now := timeutil.Now() 25 26 v := roachpb.Value{Timestamp: hlc.Timestamp{WallTime: now.UnixNano()}} 27 v.SetFloat(val) 28 29 return Info{ 30 Value: v, 31 OrigStamp: now.UnixNano(), 32 TTLStamp: now.Add(time.Millisecond).UnixNano(), 33 } 34 } 35 36 func TestExpired(t *testing.T) { 37 defer leaktest.AfterTest(t)() 38 39 i := newInfo(float64(1)) 40 if i.expired(i.Value.Timestamp.WallTime) { 41 t.Error("premature expiration") 42 } 43 if !i.expired(i.TTLStamp) { 44 t.Error("info should have expired") 45 } 46 } 47 48 func TestIsFresh(t *testing.T) { 49 defer leaktest.AfterTest(t)() 50 51 i := newInfo(float64(1)) 52 if !i.isFresh(i.OrigStamp - 1) { 53 t.Error("info should be fresh:", i) 54 } 55 if i.isFresh(i.OrigStamp) { 56 t.Error("info should not be fresh:", i) 57 } 58 if i.isFresh(i.OrigStamp + 1) { 59 t.Error("info should not be fresh:", i) 60 } 61 // Using timestamp 0 will always yield fresh data. 62 if !i.isFresh(0) { 63 t.Error("info should be fresh from node0:", i) 64 } 65 }