github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/util/timesource_test.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package util 16 17 import ( 18 "testing" 19 "time" 20 ) 21 22 var ( 23 date1 = time.Date(1970, 9, 19, 12, 00, 00, 00, time.UTC) 24 date2 = time.Date(2007, 7, 7, 11, 35, 00, 00, time.UTC) 25 ) 26 27 func TestFakeTimeSource(t *testing.T) { 28 fake := NewFakeTimeSource(date1) 29 30 // Check that a FakeTimeSource can be used as a TimeSource. 31 var ts TimeSource = fake 32 if got, want := ts.Now(), date1; got != want { 33 t.Errorf("ts.Now=%v; want %v", got, want) 34 } 35 36 fake.Set(date2) 37 if got, want := ts.Now(), date2; got != want { 38 t.Errorf("ts.Now=%v; want %v", got, want) 39 } 40 } 41 42 func TestSecondsSince(t *testing.T) { 43 delta := 8 * time.Second 44 date3 := date2.Add(delta) 45 46 var ts TimeSource = NewFakeTimeSource(date3) 47 if got, want := SecondsSince(ts, date2), delta.Seconds(); got != want { 48 t.Errorf("SecondsSince=%v; want %v", got, want) 49 } 50 }