go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/clock/external_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 clock
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  )
    24  
    25  // testClock is a Clock implementation used for testing.
    26  type testClock struct {
    27  	nowCallback      func() time.Time
    28  	sleepCallback    func() TimerResult
    29  	newTimerCallback func() Timer
    30  }
    31  
    32  func (tc *testClock) Now() time.Time {
    33  	return tc.nowCallback()
    34  }
    35  
    36  func (tc *testClock) Sleep(context.Context, time.Duration) TimerResult {
    37  	return tc.sleepCallback()
    38  }
    39  
    40  func (tc *testClock) NewTimer(context.Context) Timer {
    41  	return tc.newTimerCallback()
    42  }
    43  
    44  func TestExternal(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	now := time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC)
    48  	Convey(`A Context with a testClock installed`, t, func() {
    49  		tc := &testClock{}
    50  		c := Set(context.Background(), tc)
    51  
    52  		Convey(`Now() will use the testClock's Now().`, func() {
    53  			used := false
    54  			tc.nowCallback = func() time.Time {
    55  				used = true
    56  				return now
    57  			}
    58  
    59  			So(Now(c), ShouldResemble, now)
    60  			So(used, ShouldBeTrue)
    61  		})
    62  
    63  		Convey(`Sleep() will use testClock's Sleep().`, func() {
    64  			used := false
    65  			tc.sleepCallback = func() TimerResult {
    66  				used = true
    67  				return TimerResult{}
    68  			}
    69  
    70  			Sleep(c, time.Second)
    71  			So(used, ShouldBeTrue)
    72  		})
    73  
    74  		Convey(`NewTimer() will use testClock's NewTimer().`, func() {
    75  			used := false
    76  			tc.newTimerCallback = func() Timer {
    77  				used = true
    78  				return nil
    79  			}
    80  
    81  			NewTimer(c)
    82  			So(used, ShouldBeTrue)
    83  		})
    84  	})
    85  
    86  	Convey(`An Context with no clock installed`, t, func() {
    87  		c := context.Background()
    88  
    89  		Convey(`Will return a SystemClock instance.`, func() {
    90  			So(Get(c), ShouldHaveSameTypeAs, systemClock{})
    91  		})
    92  	})
    93  }