-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_test.go
More file actions
63 lines (55 loc) · 1.42 KB
/
app_test.go
File metadata and controls
63 lines (55 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package app
import (
"context"
"testing"
"time"
"github.com/arquivei/go-app/logger"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
)
func init() {
zerolog.SetGlobalLevel(zerolog.Disabled)
}
func newAppTestingConfig() Config {
cfg := Config{}
cfg.App.Log = logger.Config{
Level: "disabled",
}
cfg.App.AdminServer.Enabled = false
cfg.App.Shutdown.GracePeriod = 3 * time.Second
cfg.App.Shutdown.Timeout = 5 * time.Second
return cfg
}
func TestRunAndWait(t *testing.T) {
assert.Panics(t, func() {
a := App{}
main := func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}
a.RunAndWait(main)
a.RunAndWait(main)
}, "Panics if RunAndWait is called more than once.")
assert.NotPanics(t, func() {
a := New(newAppTestingConfig())
a.RunAndWait(func(ctx context.Context) error {
return nil
})
}, "Calling RunAndWait once should not Panic.")
}
func TestAppShutdown(t *testing.T) {
assert.NotPanics(t, func() {
var shutdownHandlerCalled bool
a := New(Config{})
a.RegisterShutdownHandler(&ShutdownHandler{
Name: "testing_handler",
Handler: func(ctx context.Context) error {
shutdownHandlerCalled = true
return nil
},
})
err := a.Shutdown(context.Background())
assert.NoError(t, err, "Shutdown should not fail.")
assert.True(t, shutdownHandlerCalled, "Shutdown handler should be executed during shutdown.")
}, "Calling RunAndWait once should not Panic")
}