2018-10-08 19:20:28 +00:00
|
|
|
package tunnel
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-25 21:51:58 +00:00
|
|
|
"sync"
|
2018-05-01 23:45:06 +00:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2018-05-01 23:45:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const tick = 100 * time.Millisecond
|
|
|
|
|
|
|
|
var (
|
|
|
|
serverErr = fmt.Errorf("server error")
|
|
|
|
shutdownErr = fmt.Errorf("receive shutdown")
|
|
|
|
graceShutdownErr = fmt.Errorf("receive grace shutdown")
|
|
|
|
)
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
func channelClosed(c chan struct{}) bool {
|
2018-05-01 23:45:06 +00:00
|
|
|
select {
|
|
|
|
case <-c:
|
2021-01-25 21:51:58 +00:00
|
|
|
return true
|
2018-05-01 23:45:06 +00:00
|
|
|
default:
|
2021-01-25 21:51:58 +00:00
|
|
|
return false
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
func TestSignalShutdown(t *testing.T) {
|
2020-11-25 06:55:13 +00:00
|
|
|
log := zerolog.Nop()
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
// Test handling SIGTERM & SIGINT
|
|
|
|
for _, sig := range []syscall.Signal{syscall.SIGTERM, syscall.SIGINT} {
|
2021-01-25 21:51:58 +00:00
|
|
|
graceShutdownC := make(chan struct{})
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
go func(sig syscall.Signal) {
|
|
|
|
// sleep for a tick to prevent sending signal before calling waitForSignal
|
|
|
|
time.Sleep(tick)
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = syscall.Kill(syscall.Getpid(), sig)
|
2018-05-01 23:45:06 +00:00
|
|
|
}(sig)
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
time.AfterFunc(time.Second, func() {
|
|
|
|
select {
|
|
|
|
case <-graceShutdownC:
|
|
|
|
default:
|
|
|
|
close(graceShutdownC)
|
|
|
|
t.Fatal("waitForSignal timed out")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
waitForSignal(graceShutdownC, &log)
|
|
|
|
assert.True(t, channelClosed(graceShutdownC))
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
func TestWaitForShutdown(t *testing.T) {
|
|
|
|
log := zerolog.Nop()
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
errC := make(chan error)
|
2021-01-25 21:51:58 +00:00
|
|
|
graceShutdownC := make(chan struct{})
|
|
|
|
const gracePeriod = 5 * time.Second
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
contextCancelled := false
|
|
|
|
cancel := func() {
|
|
|
|
contextCancelled = true
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// on, error stop immediately
|
|
|
|
contextCancelled = false
|
|
|
|
startTime := time.Now()
|
2018-05-01 23:45:06 +00:00
|
|
|
go func() {
|
|
|
|
errC <- serverErr
|
|
|
|
}()
|
2021-01-25 21:51:58 +00:00
|
|
|
err := waitToShutdown(&wg, cancel, errC, graceShutdownC, gracePeriod, &log)
|
2018-05-01 23:45:06 +00:00
|
|
|
assert.Equal(t, serverErr, err)
|
2021-01-25 21:51:58 +00:00
|
|
|
assert.True(t, contextCancelled)
|
|
|
|
assert.False(t, channelClosed(graceShutdownC))
|
|
|
|
assert.True(t, time.Now().Sub(startTime) < time.Second) // check that wait ended early
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
// on graceful shutdown, ignore error but stop as soon as an error arrives
|
|
|
|
contextCancelled = false
|
|
|
|
startTime = time.Now()
|
|
|
|
go func() {
|
|
|
|
close(graceShutdownC)
|
|
|
|
time.Sleep(tick)
|
|
|
|
errC <- serverErr
|
|
|
|
}()
|
|
|
|
err = waitToShutdown(&wg, cancel, errC, graceShutdownC, gracePeriod, &log)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, contextCancelled)
|
|
|
|
assert.True(t, time.Now().Sub(startTime) < time.Second) // check that wait ended early
|
|
|
|
|
|
|
|
// with graceShutdownC closed stop right away without grace period
|
|
|
|
contextCancelled = false
|
|
|
|
startTime = time.Now()
|
|
|
|
err = waitToShutdown(&wg, cancel, errC, graceShutdownC, 0, &log)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, contextCancelled)
|
|
|
|
assert.True(t, time.Now().Sub(startTime) < time.Second) // check that wait ended early
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2021-01-25 21:51:58 +00:00
|
|
|
|