make initilised private to countdown

This commit is contained in:
Jianrong 2021-11-03 11:10:28 +11:00
parent bcb1fea280
commit bf56a64fe0
2 changed files with 12 additions and 12 deletions

View file

@ -12,7 +12,7 @@ type CountdownTimer struct {
lock sync.RWMutex // Protects the Initilised field
resetc chan int
quitc chan chan struct{}
Initilised bool
initilised bool
timeoutDuration time.Duration
// Triggered when the countdown timer timeout for the `timeoutDuration` period, it will pass current timestamp to the callback function
OnTimeoutFn func(time time.Time) error
@ -22,7 +22,7 @@ func NewCountDown(duration time.Duration) *CountdownTimer {
return &CountdownTimer{
resetc: make(chan int),
quitc: make(chan chan struct{}),
Initilised: false,
initilised: false,
timeoutDuration: duration,
}
}
@ -73,11 +73,11 @@ func (t *CountdownTimer) startTimer() {
func (t *CountdownTimer) setInitilised(value bool) {
t.lock.Lock()
defer t.lock.Unlock()
t.Initilised = value
t.initilised = value
}
func (t *CountdownTimer) getInitilisedValue() bool {
t.lock.Lock()
defer t.lock.Unlock()
return t.Initilised
return t.initilised
}

View file

@ -33,10 +33,10 @@ func TestCountdownShouldReset(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start
assert.False(t, countdown.Initilised)
assert.False(t, countdown.getInitilisedValue())
countdown.Reset()
// Now the countdown should already started
assert.True(t, countdown.Initilised)
assert.True(t, countdown.getInitilisedValue())
expectedCalledTime := time.Now().Add(9000 * time.Millisecond)
resetTimer := time.NewTimer(4000 * time.Millisecond)
@ -46,7 +46,7 @@ firstReset:
case <-called:
if time.Now().After(expectedCalledTime) {
// Make sure the countdown runs forever
assert.True(t, countdown.Initilised)
assert.True(t, countdown.getInitilisedValue())
fmt.Println("Correctly reset the countdown once")
} else {
t.Fatalf("Countdown did not reset correctly first time")
@ -58,12 +58,12 @@ firstReset:
}
// Now the countdown is paused after calling the callback function, let's reset it again
assert.True(t, countdown.Initilised)
assert.True(t, countdown.getInitilisedValue())
expectedTimeAfterReset := time.Now().Add(5000 * time.Millisecond)
countdown.Reset()
<-called
// Always initilised
assert.True(t, countdown.Initilised)
assert.True(t, countdown.getInitilisedValue())
if time.Now().After(expectedTimeAfterReset) {
fmt.Println("Correctly reset the countdown second time")
} else {
@ -81,13 +81,13 @@ func TestCountdownShouldBeAbleToStop(t *testing.T) {
countdown := NewCountDown(5000 * time.Millisecond)
countdown.OnTimeoutFn = OnTimeoutFn
// Check countdown did not start
assert.False(t, countdown.Initilised)
assert.False(t, countdown.getInitilisedValue())
countdown.Reset()
// Now the countdown should already started
assert.True(t, countdown.Initilised)
assert.True(t, countdown.getInitilisedValue())
// Try manually stop the timer before it triggers the callback
stopTimer := time.NewTimer(4000 * time.Millisecond)
<-stopTimer.C
countdown.StopTimer()
assert.False(t, countdown.Initilised)
assert.False(t, countdown.getInitilisedValue())
}