mirror of
https://github.com/luxfi/timer.git
synced 2026-10-30 15:15:40 +00:00
- Remove meter.go (duplicate Meter interface) - Keep concrete Meter struct in clock.go - Add new timer files (adaptive, staged, stopped)
48 lines
835 B
Go
48 lines
835 B
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package timer
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSingleStagedTimer(t *testing.T) {
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
ticks := 1
|
|
i := 0
|
|
timer := NewStagedTimer(func() (time.Duration, bool) {
|
|
defer wg.Done()
|
|
i++
|
|
return 0, false
|
|
})
|
|
go timer.Dispatch()
|
|
|
|
timer.SetTimeoutIn(time.Millisecond)
|
|
wg.Wait()
|
|
require.Equal(t, i, ticks)
|
|
}
|
|
|
|
func TestMultiStageTimer(t *testing.T) {
|
|
wg := sync.WaitGroup{}
|
|
ticks := 3
|
|
wg.Add(ticks)
|
|
|
|
i := 0
|
|
timer := NewStagedTimer(func() (time.Duration, bool) {
|
|
defer wg.Done()
|
|
i++
|
|
return time.Millisecond, i < ticks
|
|
})
|
|
go timer.Dispatch()
|
|
|
|
timer.SetTimeoutIn(time.Millisecond)
|
|
wg.Wait()
|
|
require.Equal(t, i, ticks)
|
|
}
|