forked from niondir/go-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_test.go
54 lines (46 loc) · 1.13 KB
/
builder_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
package service_test
import (
"context"
"github.com/niondir/go-service"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestServiceBuilder(t *testing.T) {
c := service.NewContainer()
initialized := false
run := false
stopped := false
service.New("My Service").
Init(func(ctx context.Context) error {
initialized = true
return nil
}).
Run(func(ctx context.Context) error {
run = true
// Implement your service here. Try to keep it running, only return fatal errors.
<-ctx.Done()
// Gracefully shut down your service here
stopped = true
return nil
}).
Register(c)
ctx := context.Background()
err := c.StartAll(ctx)
require.NoError(t, err)
c.StopAll()
c.WaitAllStopped(ctx)
assert.Len(t, c.ServiceErrors(), 0)
assert.True(t, initialized)
assert.True(t, run)
assert.True(t, stopped)
}
func TestCtx(t *testing.T) {
parent, cancelParent := context.WithTimeout(context.Background(), time.Second)
defer cancelParent()
_, cancel := context.WithCancel(parent)
cancel()
<-parent.Done()
assert.Equal(t, context.DeadlineExceeded, parent.Err())
}