Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giftkugel committed Aug 30, 2024
1 parent bfdecae commit eef9ef6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetSignalChannel() chan os.Signal {
if signalSingleton == nil {
sigs = make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
signalSingleton = new(byte)
}
return sigs
}
Expand Down
58 changes: 58 additions & 0 deletions internal/system/system_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package system

import (
"errors"
"os"
"sync"
"testing"
)

func Test_System(t *testing.T) {
t.Run("Critical error", func(t *testing.T) {
var exitCode *int
exitFunc = func(code int) {
exitCode = &code
}

CriticalError(errors.New("foo"))

if exitCode == nil || *exitCode != 1 {
t.Errorf("Expected exit code 1, got %d", exitCode)
}
})

t.Run("Error", func(t *testing.T) {
var exitCode *int
exitFunc = func(code int) {
exitCode = &code
}

wg := sync.WaitGroup{}
var signal *os.Signal

s := GetSignalChannel()

wg.Add(1)
go func() {
defer wg.Done()
current := <-s
signal = &current
}()

wg.Add(1)
go func() {
defer wg.Done()
Error(errors.New("foo"))
}()

wg.Wait()

if exitCode != nil {
t.Error("Exit should not be called")
}

if signal == nil {
t.Error("Signal should not be called")
}
})
}

0 comments on commit eef9ef6

Please sign in to comment.