github.com/mailgun/holster/v4@v4.20.0/clock/system_test.go (about)

     1  package clock
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestSleep(t *testing.T) {
    11  	start := Now()
    12  
    13  	// When
    14  	Sleep(100 * time.Millisecond)
    15  
    16  	// Then
    17  	if Now().Sub(start) < 100*time.Millisecond {
    18  		assert.Fail(t, "Sleep did not last long enough")
    19  	}
    20  }
    21  
    22  func TestAfter(t *testing.T) {
    23  	start := Now()
    24  
    25  	// When
    26  	end := <-After(100 * time.Millisecond)
    27  
    28  	// Then
    29  	if end.Sub(start) < 100*time.Millisecond {
    30  		assert.Fail(t, "Sleep did not last long enough")
    31  	}
    32  }
    33  
    34  func TestAfterFunc(t *testing.T) {
    35  	start := Now()
    36  	endCh := make(chan time.Time, 1)
    37  
    38  	// When
    39  	AfterFunc(100*time.Millisecond, func() { endCh <- time.Now() })
    40  
    41  	// Then
    42  	end := <-endCh
    43  	if end.Sub(start) < 100*time.Millisecond {
    44  		assert.Fail(t, "Sleep did not last long enough")
    45  	}
    46  }
    47  
    48  func TestNewTimer(t *testing.T) {
    49  	start := Now()
    50  
    51  	// When
    52  	timer := NewTimer(100 * time.Millisecond)
    53  
    54  	// Then
    55  	end := <-timer.C()
    56  	if end.Sub(start) < 100*time.Millisecond {
    57  		assert.Fail(t, "Sleep did not last long enough")
    58  	}
    59  }
    60  
    61  func TestTimerStop(t *testing.T) {
    62  	timer := NewTimer(50 * time.Millisecond)
    63  
    64  	// When
    65  	active := timer.Stop()
    66  
    67  	// Then
    68  	assert.Equal(t, true, active)
    69  	time.Sleep(100 * time.Nanosecond)
    70  	select {
    71  	case <-timer.C():
    72  		assert.Fail(t, "Timer should not have fired")
    73  	default:
    74  	}
    75  }
    76  
    77  func TestTimerReset(t *testing.T) {
    78  	start := time.Now()
    79  	timer := NewTimer(300 * time.Millisecond)
    80  
    81  	// When
    82  	timer.Reset(100 * time.Millisecond)
    83  
    84  	// Then
    85  	end := <-timer.C()
    86  	if end.Sub(start) > 150*time.Millisecond {
    87  		assert.Fail(t, "Waited too long")
    88  	}
    89  }
    90  
    91  func TestNewTicker(t *testing.T) {
    92  	start := Now()
    93  
    94  	// When
    95  	timer := NewTicker(100 * time.Millisecond)
    96  
    97  	// Then
    98  	end := <-timer.C()
    99  	if end.Sub(start) < 100*time.Millisecond {
   100  		assert.Fail(t, "Sleep did not last long enough")
   101  	}
   102  	end = <-timer.C()
   103  	if end.Sub(start) < 200*time.Millisecond {
   104  		assert.Fail(t, "Sleep did not last long enough")
   105  	}
   106  
   107  	timer.Stop()
   108  	time.Sleep(150)
   109  	select {
   110  	case <-timer.C():
   111  		assert.Fail(t, "Ticker should not have fired")
   112  	default:
   113  	}
   114  }
   115  
   116  func TestTick(t *testing.T) {
   117  	start := Now()
   118  
   119  	// When
   120  	ch := Tick(100 * time.Millisecond)
   121  
   122  	// Then
   123  	end := <-ch
   124  	if end.Sub(start) < 100*time.Millisecond {
   125  		assert.Fail(t, "Sleep did not last long enough")
   126  	}
   127  	end = <-ch
   128  	if end.Sub(start) < 200*time.Millisecond {
   129  		assert.Fail(t, "Sleep did not last long enough")
   130  	}
   131  }
   132  
   133  func TestNewStoppedTimer(t *testing.T) {
   134  	timer := NewStoppedTimer()
   135  
   136  	// When/Then
   137  	select {
   138  	case <-timer.C():
   139  		assert.Fail(t, "Timer should not have fired")
   140  	default:
   141  	}
   142  	assert.Equal(t, false, timer.Stop())
   143  }