mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 17:44:02 +03:00
FILE / ScuroNeko/mtg
mtglib/internal/doppel/clock_test.go
Исходный файл и его история в репозитории.
81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
package doppel
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type ClockTestSuite struct {
|
|
suite.Suite
|
|
|
|
clock Clock
|
|
wg sync.WaitGroup
|
|
ctx context.Context
|
|
ctxCancel context.CancelFunc
|
|
}
|
|
|
|
func (suite *ClockTestSuite) SetupTest() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
suite.ctx = ctx
|
|
suite.ctxCancel = cancel
|
|
suite.clock = Clock{
|
|
stats: &Stats{
|
|
k: StatsDefaultK,
|
|
lambda: StatsDefaultLambda,
|
|
},
|
|
tick: make(chan struct{}),
|
|
}
|
|
|
|
suite.wg.Go(func() {
|
|
suite.clock.Start(suite.ctx)
|
|
})
|
|
}
|
|
|
|
func (suite *ClockTestSuite) TearDownTest() {
|
|
suite.ctxCancel()
|
|
suite.wg.Wait()
|
|
}
|
|
|
|
func (suite *ClockTestSuite) TestTicks() {
|
|
received := 0
|
|
|
|
for range 3 {
|
|
select {
|
|
case <-suite.clock.tick:
|
|
received++
|
|
case <-time.After(2 * time.Second):
|
|
suite.Fail("timed out waiting for tick")
|
|
}
|
|
}
|
|
|
|
suite.Equal(3, received)
|
|
}
|
|
|
|
func (suite *ClockTestSuite) TestStopsOnCancel() {
|
|
select {
|
|
case <-suite.clock.tick:
|
|
case <-time.After(2 * time.Second):
|
|
suite.Fail("timed out waiting for first tick")
|
|
}
|
|
|
|
suite.ctxCancel()
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
select {
|
|
case <-suite.clock.tick:
|
|
suite.Fail("received tick after cancel")
|
|
default:
|
|
}
|
|
}
|
|
|
|
func TestClock(t *testing.T) {
|
|
t.Parallel()
|
|
suite.Run(t, &ClockTestSuite{})
|
|
}
|