error-pages/internal/breaker/os_signal_test.go
2023-02-23 21:49:45 +04:00

58 lines
1.1 KiB
Go

package breaker_test
import (
"context"
"os"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gh.tarampamp.am/error-pages/internal/breaker"
)
func TestNewOSSignals(t *testing.T) {
oss := breaker.NewOSSignals(context.Background())
gotSignal := make(chan os.Signal, 1)
oss.Subscribe(func(signal os.Signal) {
gotSignal <- signal
}, syscall.SIGUSR2)
defer oss.Stop()
proc, err := os.FindProcess(os.Getpid())
assert.NoError(t, err)
assert.NoError(t, proc.Signal(syscall.SIGUSR2)) // send the signal
time.Sleep(time.Millisecond * 5)
assert.Equal(t, syscall.SIGUSR2, <-gotSignal)
}
func TestNewOSSignalCtxCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
oss := breaker.NewOSSignals(ctx)
gotSignal := make(chan os.Signal, 1)
oss.Subscribe(func(signal os.Signal) {
gotSignal <- signal
}, syscall.SIGUSR2)
defer oss.Stop()
proc, err := os.FindProcess(os.Getpid())
assert.NoError(t, err)
cancel()
assert.NoError(t, proc.Signal(syscall.SIGUSR2)) // send the signal
assert.Empty(t, gotSignal)
}