Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Panic while accessing SleepyTicker methods Stop()/SetSleep() #1779

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/mgr/sleepyticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

// SleepyTicker is wrapper over time.Ticker that respects the sleep mode of the module.
type SleepyTicker struct {
ticker time.Ticker
ticker *time.Ticker
normalDuration time.Duration
sleepDuration time.Duration
sleepMode bool
Expand All @@ -16,7 +16,7 @@ type SleepyTicker struct {
// If sleepDuration is set to 0 ticker will not tick during sleep.
func NewSleepyTicker(normalDuration time.Duration, sleepDuration time.Duration) *SleepyTicker {
st := &SleepyTicker{
ticker: *time.NewTicker(normalDuration),
ticker: time.NewTicker(normalDuration),
normalDuration: normalDuration,
sleepDuration: sleepDuration,
sleepMode: false,
Expand Down
57 changes: 57 additions & 0 deletions service/mgr/sleepyticker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mgr

import (
"testing"
"time"
)

func TestSleepyTickerStop(t *testing.T) {

Check failure on line 8 in service/mgr/sleepyticker_test.go

View workflow job for this annotation

GitHub Actions / Linter

Function TestSleepyTickerStop missing the call to method parallel
normalDuration := 100 * time.Millisecond
sleepDuration := 200 * time.Millisecond

st := NewSleepyTicker(normalDuration, sleepDuration)
st.Stop() // no panic expected here
}

func TestSleepyTicker(t *testing.T) {

Check failure on line 16 in service/mgr/sleepyticker_test.go

View workflow job for this annotation

GitHub Actions / Linter

Function TestSleepyTicker missing the call to method parallel
normalDuration := 100 * time.Millisecond
sleepDuration := 200 * time.Millisecond

st := NewSleepyTicker(normalDuration, sleepDuration)

// Test normal mode
select {
case <-st.Wait():
// Expected tick
case <-time.After(normalDuration + 50*time.Millisecond):
t.Error("expected tick in normal mode")
}

// Test sleep mode
st.SetSleep(true)
select {
case <-st.Wait():
// Expected tick
case <-time.After(sleepDuration + 50*time.Millisecond):
t.Error("expected tick in sleep mode")
}

// Test sleep mode with sleepDuration == 0
st = NewSleepyTicker(normalDuration, 0)
st.SetSleep(true)
select {
case <-st.Wait():
t.Error("did not expect tick when sleepDuration is 0")
case <-time.After(normalDuration):
// Expected no tick
}

// Test stopping the ticker
st.Stop()
select {
case <-st.Wait():
t.Error("did not expect tick after stopping the ticker")
case <-time.After(normalDuration):
// Expected no tick
}
}
Loading