mirror of
https://github.com/safing/portmaster
synced 2025-04-21 03:19:10 +00:00
Merge pull request #1779 from stenya/fix_crash_SleepyTicker
[fix] Panic while accessing SleepyTicker methods Stop()/SetSleep()
This commit is contained in:
commit
0937bedd6c
2 changed files with 59 additions and 2 deletions
service/mgr
|
@ -4,7 +4,7 @@ import "time"
|
||||||
|
|
||||||
// SleepyTicker is wrapper over time.Ticker that respects the sleep mode of the module.
|
// SleepyTicker is wrapper over time.Ticker that respects the sleep mode of the module.
|
||||||
type SleepyTicker struct {
|
type SleepyTicker struct {
|
||||||
ticker time.Ticker
|
ticker *time.Ticker
|
||||||
normalDuration time.Duration
|
normalDuration time.Duration
|
||||||
sleepDuration time.Duration
|
sleepDuration time.Duration
|
||||||
sleepMode bool
|
sleepMode bool
|
||||||
|
@ -16,7 +16,7 @@ type SleepyTicker struct {
|
||||||
// If sleepDuration is set to 0 ticker will not tick during sleep.
|
// If sleepDuration is set to 0 ticker will not tick during sleep.
|
||||||
func NewSleepyTicker(normalDuration time.Duration, sleepDuration time.Duration) *SleepyTicker {
|
func NewSleepyTicker(normalDuration time.Duration, sleepDuration time.Duration) *SleepyTicker {
|
||||||
st := &SleepyTicker{
|
st := &SleepyTicker{
|
||||||
ticker: *time.NewTicker(normalDuration),
|
ticker: time.NewTicker(normalDuration),
|
||||||
normalDuration: normalDuration,
|
normalDuration: normalDuration,
|
||||||
sleepDuration: sleepDuration,
|
sleepDuration: sleepDuration,
|
||||||
sleepMode: false,
|
sleepMode: false,
|
||||||
|
|
57
service/mgr/sleepyticker_test.go
Normal file
57
service/mgr/sleepyticker_test.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package mgr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSleepyTickerStop(t *testing.T) {
|
||||||
|
normalDuration := 100 * time.Millisecond
|
||||||
|
sleepDuration := 200 * time.Millisecond
|
||||||
|
|
||||||
|
st := NewSleepyTicker(normalDuration, sleepDuration)
|
||||||
|
st.Stop() // no panic expected here
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSleepyTicker(t *testing.T) {
|
||||||
|
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…
Add table
Reference in a new issue