1
Fork 0
go-shutdowner/shutdown_test.go

76 lines
1.3 KiB
Go

package goshutdowner
import (
"os"
"testing"
"time"
)
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()
if testBool1 != true {
t.Fail()
}
if testBool2 != true {
t.Fail()
}
if testBool3 != false {
t.Fail()
}
})
t.Run("Signal test", func(t *testing.T) {
var s Shutdowner
var testBool1 bool
s.Add(func() {
time.Sleep(1 * time.Second)
testBool1 = true
})
s.quit <- os.Interrupt
s.Wait()
if testBool1 != true {
t.Fail()
}
})
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()
}
})
}