github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/timer.go (about)

     1  package unittest
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  // TestTime is a fake time used for testing.
     9  type TestTime struct {
    10  	mu  sync.Mutex
    11  	cur time.Time // current fake time
    12  }
    13  
    14  // Now returns the current fake time.
    15  func (tt *TestTime) Now() time.Time {
    16  	tt.mu.Lock()
    17  	defer tt.mu.Unlock()
    18  	return tt.cur
    19  }
    20  
    21  // Since returns the fake time since the given time.
    22  func (tt *TestTime) Since(t time.Time) time.Duration {
    23  	tt.mu.Lock()
    24  	defer tt.mu.Unlock()
    25  	return tt.cur.Sub(t)
    26  }
    27  
    28  // Advance advances the fake time.
    29  func (tt *TestTime) Advance(dur time.Duration) {
    30  	tt.mu.Lock()
    31  	defer tt.mu.Unlock()
    32  	tt.cur = tt.cur.Add(dur)
    33  }
    34  
    35  // NewTestTime returns new TestTime.
    36  func NewTestTime() *TestTime {
    37  	return &TestTime{
    38  		cur: time.Now(),
    39  	}
    40  }