mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 19:00:46 +00:00
make initilised private to countdown
This commit is contained in:
parent
bcb1fea280
commit
bf56a64fe0
2 changed files with 12 additions and 12 deletions
|
|
@ -12,7 +12,7 @@ type CountdownTimer struct {
|
||||||
lock sync.RWMutex // Protects the Initilised field
|
lock sync.RWMutex // Protects the Initilised field
|
||||||
resetc chan int
|
resetc chan int
|
||||||
quitc chan chan struct{}
|
quitc chan chan struct{}
|
||||||
Initilised bool
|
initilised bool
|
||||||
timeoutDuration time.Duration
|
timeoutDuration time.Duration
|
||||||
// Triggered when the countdown timer timeout for the `timeoutDuration` period, it will pass current timestamp to the callback function
|
// 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
|
OnTimeoutFn func(time time.Time) error
|
||||||
|
|
@ -22,7 +22,7 @@ func NewCountDown(duration time.Duration) *CountdownTimer {
|
||||||
return &CountdownTimer{
|
return &CountdownTimer{
|
||||||
resetc: make(chan int),
|
resetc: make(chan int),
|
||||||
quitc: make(chan chan struct{}),
|
quitc: make(chan chan struct{}),
|
||||||
Initilised: false,
|
initilised: false,
|
||||||
timeoutDuration: duration,
|
timeoutDuration: duration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,11 +73,11 @@ func (t *CountdownTimer) startTimer() {
|
||||||
func (t *CountdownTimer) setInitilised(value bool) {
|
func (t *CountdownTimer) setInitilised(value bool) {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
t.Initilised = value
|
t.initilised = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *CountdownTimer) getInitilisedValue() bool {
|
func (t *CountdownTimer) getInitilisedValue() bool {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
return t.Initilised
|
return t.initilised
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,10 +33,10 @@ func TestCountdownShouldReset(t *testing.T) {
|
||||||
countdown := NewCountDown(5000 * time.Millisecond)
|
countdown := NewCountDown(5000 * time.Millisecond)
|
||||||
countdown.OnTimeoutFn = OnTimeoutFn
|
countdown.OnTimeoutFn = OnTimeoutFn
|
||||||
// Check countdown did not start
|
// Check countdown did not start
|
||||||
assert.False(t, countdown.Initilised)
|
assert.False(t, countdown.getInitilisedValue())
|
||||||
countdown.Reset()
|
countdown.Reset()
|
||||||
// Now the countdown should already started
|
// Now the countdown should already started
|
||||||
assert.True(t, countdown.Initilised)
|
assert.True(t, countdown.getInitilisedValue())
|
||||||
expectedCalledTime := time.Now().Add(9000 * time.Millisecond)
|
expectedCalledTime := time.Now().Add(9000 * time.Millisecond)
|
||||||
resetTimer := time.NewTimer(4000 * time.Millisecond)
|
resetTimer := time.NewTimer(4000 * time.Millisecond)
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ firstReset:
|
||||||
case <-called:
|
case <-called:
|
||||||
if time.Now().After(expectedCalledTime) {
|
if time.Now().After(expectedCalledTime) {
|
||||||
// Make sure the countdown runs forever
|
// Make sure the countdown runs forever
|
||||||
assert.True(t, countdown.Initilised)
|
assert.True(t, countdown.getInitilisedValue())
|
||||||
fmt.Println("Correctly reset the countdown once")
|
fmt.Println("Correctly reset the countdown once")
|
||||||
} else {
|
} else {
|
||||||
t.Fatalf("Countdown did not reset correctly first time")
|
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
|
// 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)
|
expectedTimeAfterReset := time.Now().Add(5000 * time.Millisecond)
|
||||||
countdown.Reset()
|
countdown.Reset()
|
||||||
<-called
|
<-called
|
||||||
// Always initilised
|
// Always initilised
|
||||||
assert.True(t, countdown.Initilised)
|
assert.True(t, countdown.getInitilisedValue())
|
||||||
if time.Now().After(expectedTimeAfterReset) {
|
if time.Now().After(expectedTimeAfterReset) {
|
||||||
fmt.Println("Correctly reset the countdown second time")
|
fmt.Println("Correctly reset the countdown second time")
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -81,13 +81,13 @@ func TestCountdownShouldBeAbleToStop(t *testing.T) {
|
||||||
countdown := NewCountDown(5000 * time.Millisecond)
|
countdown := NewCountDown(5000 * time.Millisecond)
|
||||||
countdown.OnTimeoutFn = OnTimeoutFn
|
countdown.OnTimeoutFn = OnTimeoutFn
|
||||||
// Check countdown did not start
|
// Check countdown did not start
|
||||||
assert.False(t, countdown.Initilised)
|
assert.False(t, countdown.getInitilisedValue())
|
||||||
countdown.Reset()
|
countdown.Reset()
|
||||||
// Now the countdown should already started
|
// 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
|
// Try manually stop the timer before it triggers the callback
|
||||||
stopTimer := time.NewTimer(4000 * time.Millisecond)
|
stopTimer := time.NewTimer(4000 * time.Millisecond)
|
||||||
<-stopTimer.C
|
<-stopTimer.C
|
||||||
countdown.StopTimer()
|
countdown.StopTimer()
|
||||||
assert.False(t, countdown.Initilised)
|
assert.False(t, countdown.getInitilisedValue())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue