1
Fork 0
go-shutdowner/shutdown_test.go

76 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-06-17 14:26:09 +00:00
package goshutdowner
import (
"os"
"testing"
2021-06-19 21:18:56 +00:00
"time"
2021-06-17 14:26:09 +00:00
)
func Test_shutdowner(t *testing.T) {
t.Run("Simple test", func(t *testing.T) {
var s Shutdowner
var testBool1, testBool2, testBool3 bool
s.Add(func() {
testBool1 = true
})
s.Add(func() {
testBool2 = true
})
s.ShutdownAndWait()
2021-06-17 19:58:14 +00:00
if testBool1 != true {
t.Fail()
}
if testBool2 != true {
t.Fail()
}
if testBool3 != false {
t.Fail()
}
2021-06-17 14:26:09 +00:00
})
t.Run("Signal test", func(t *testing.T) {
var s Shutdowner
var testBool1 bool
s.Add(func() {
2021-06-19 21:18:56 +00:00
time.Sleep(1 * time.Second)
2021-06-17 14:26:09 +00:00
testBool1 = true
})
s.quit <- os.Interrupt
s.Wait()
2021-06-17 19:58:14 +00:00
if testBool1 != true {
t.Fail()
}
2021-06-17 14:26:09 +00:00
})
2021-06-19 21:18:56 +00:00
t.Run("Cancel shutdown", func(t *testing.T) {
var s Shutdowner
var testBool1 bool
s.Add(func() {
time.Sleep(10 * time.Second)
testBool1 = true
})
go func() {
time.Sleep(1 * time.Second)
s.Shutdown()
}()
s.ShutdownAndWait()
if testBool1 == true {
t.Fail()
}
})
t.Run("Cancel shutdown using signal", func(t *testing.T) {
var s Shutdowner
var testBool1 bool
s.Add(func() {
time.Sleep(10 * time.Second)
testBool1 = true
})
go func() {
time.Sleep(1 * time.Second)
s.quit <- os.Interrupt
}()
s.ShutdownAndWait()
if testBool1 == true {
t.Fail()
}
})
2021-06-17 14:26:09 +00:00
}