package utils
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
type testService struct {
key string
killCalls atomic.Int32
}
func newTestService(key string) *testService {
return &testService{key: key}
}
func (s *testService) Kill() { s.killCalls.Inc() }
func TestMultitonService(t *testing.T) {
t.Run("start and stop are called", func(t *testing.T) {
t.Parallel()
r := MultitonService[string]{}
svc := newTestService("foo")
stop := r.Replace("test", svc)
time.Sleep(time.Millisecond)
stop()
time.Sleep(time.Millisecond)
require.EqualValues(t, 1, svc.killCalls.Load())
})
t.Run("stop is idempotent", func(t *testing.T) {
t.Parallel()
r := MultitonService[string]{}
svc := newTestService("foo")
stop := r.Replace("test", svc)
for i := 0; i < 3; i++ {
time.Sleep(time.Millisecond)
stop()
}
require.EqualValues(t, 1, svc.killCalls.Load())
})
t.Run("replaced services are stopped", func(t *testing.T) {
t.Parallel()
r := MultitonService[string]{}
svc0 := newTestService("foo")
r.Replace("test", svc0)
time.Sleep(time.Millisecond)
svc1 := newTestService("foo")
r.Replace("test", svc1)
time.Sleep(time.Millisecond)
require.EqualValues(t, 1, svc0.killCalls.Load())
require.EqualValues(t, 0, svc1.killCalls.Load())
})
t.Run("stop funcs for replaced services are neutered", func(t *testing.T) {
t.Parallel()
r := MultitonService[string]{}
svc0 := newTestService("foo")
stop0 := r.Replace("test", svc0)
time.Sleep(time.Millisecond)
svc1 := newTestService("foo")
r.Replace("test", svc1)
time.Sleep(time.Millisecond)
stop0()
time.Sleep(time.Millisecond)
require.EqualValues(t, 1, svc0.killCalls.Load())
require.EqualValues(t, 0, svc1.killCalls.Load())
})
}