This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
default_test.go
98 lines (75 loc) · 2.08 KB
/
default_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package helium
import (
"context"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/dig"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
"github.com/im-kulikov/helium/module"
"github.com/im-kulikov/helium/service"
)
type errService struct {
start bool
stop bool
*atomic.Error
}
func (e *errService) Start(_ context.Context) error {
if !e.start {
return nil
}
return errTest
}
func (e *errService) Stop(context.Context) {
if e.stop {
e.Store(errTest)
}
}
func (e errService) Name() string { return "errService" }
func TestDefaultApp(t *testing.T) {
t.Run("create new helium with default application", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
h, err := New(&Settings{},
DefaultApp,
module.New(viper.New),
module.New(zap.NewNop),
module.New(func() context.Context { return ctx }),
)
require.NotNil(t, h)
require.NoError(t, err)
cancel()
require.NoError(t, h.Run())
})
t.Run("default application with start err", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
h, err := New(&Settings{},
DefaultApp,
module.New(viper.New),
module.New(zap.NewNop),
module.New(func() context.Context { return ctx }),
module.New(func() service.Service { return &errService{start: true} }, dig.Group("services")),
)
require.NotNil(t, h)
require.NoError(t, err)
require.EqualError(t, h.Run(), errTest.Error())
cancel()
})
t.Run("default application with stop err", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
svc := &errService{stop: true, Error: atomic.NewError(nil)}
h, err := New(&Settings{},
DefaultApp,
module.New(viper.New),
module.New(func() context.Context { return ctx }),
module.New(func() *zap.Logger { return zaptest.NewLogger(t) }),
module.New(func() service.Service { return svc }, dig.Group("services")),
)
require.NotNil(t, h)
require.NoError(t, err)
cancel()
require.NoError(t, h.Run())
require.EqualError(t, svc.Load(), errTest.Error())
})
}