github.com/uber/kraken@v0.1.4/utils/timeutil/timer_test.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     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  package timeutil
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  const d = 50 * time.Millisecond
    24  
    25  const delta = 10 * time.Millisecond
    26  
    27  func TestTimerFiresAfterStart(t *testing.T) {
    28  	require := require.New(t)
    29  	timer := NewTimer(d)
    30  
    31  	require.True(timer.Start())
    32  
    33  	select {
    34  	case <-timer.C:
    35  	case <-time.After(d + delta):
    36  		t.Fatal("Timer did not fire within expected duration")
    37  	}
    38  }
    39  
    40  func TestTimerSecondStartIsNoop(t *testing.T) {
    41  	require := require.New(t)
    42  	timer := NewTimer(d)
    43  
    44  	require.True(timer.Start())
    45  	require.False(timer.Start())
    46  
    47  	select {
    48  	case <-timer.C:
    49  	case <-time.After(d + delta):
    50  		t.Fatal("Timer did not fire within duration of initial start")
    51  	}
    52  }
    53  
    54  func TestTimerCancelPreventsFiring(t *testing.T) {
    55  	require := require.New(t)
    56  	timer := NewTimer(d)
    57  
    58  	require.True(timer.Start())
    59  	require.True(timer.Cancel())
    60  
    61  	select {
    62  	case <-timer.C:
    63  		t.Fatal("Timer fired after Cancel was called")
    64  	case <-time.After(d + delta):
    65  	}
    66  }
    67  
    68  func TestTimerCanStillStartAfterCancel(t *testing.T) {
    69  	require := require.New(t)
    70  	timer := NewTimer(d)
    71  
    72  	require.True(timer.Start())
    73  	require.True(timer.Cancel())
    74  	require.True(timer.Start())
    75  
    76  	select {
    77  	case <-timer.C:
    78  	case <-time.After(d + delta):
    79  		t.Fatal("Timer did not fire within expected duration")
    80  	}
    81  }
    82  
    83  func TestTimerCancelBeforeStartIsNoop(t *testing.T) {
    84  	require := require.New(t)
    85  	timer := NewTimer(d)
    86  
    87  	require.False(timer.Cancel())
    88  	require.True(timer.Start())
    89  
    90  	select {
    91  	case <-timer.C:
    92  	case <-time.After(d + delta):
    93  		t.Fatal("Timer did not fire within expected duration")
    94  	}
    95  }
    96  
    97  func TestTimerCancelAfterFiringIsNoop(t *testing.T) {
    98  	require := require.New(t)
    99  	timer := NewTimer(d)
   100  
   101  	require.True(timer.Start())
   102  
   103  	select {
   104  	case <-timer.C:
   105  	case <-time.After(d + delta):
   106  		t.Fatal("Timer did not fire within expected duration")
   107  	}
   108  
   109  	require.False(timer.Cancel())
   110  }