forked from niondir/go-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_test.go
76 lines (63 loc) · 1.56 KB
/
func_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
package service_test
import (
"context"
"github.com/niondir/go-service"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestWithRunFunc(t *testing.T) {
c := service.NewContainer()
started := false
stopped := false
anon := func(ctx context.Context) error {
started = true
t.Logf("service started")
<-ctx.Done()
stopped = true
t.Logf("service stopped")
return nil
}
c.Register(service.WithRunFunc(anon))
ctx, cancel := context.WithCancel(context.Background())
err := c.StartAll(ctx)
t.Logf("Services: %v", c.ServiceNames())
require.NoError(t, err)
cancel()
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c.WaitAllStopped(shutdownCtx)
require.True(t, started)
require.True(t, stopped)
}
func TestWithFunc(t *testing.T) {
initialized := false
started := false
stopped := false
c := service.NewContainer()
init := func(ctx context.Context) error {
initialized = true
t.Logf("service initialized")
return nil
}
run := func(ctx context.Context) error {
started = true
t.Logf("service started")
<-ctx.Done()
stopped = true
t.Logf("service stopped")
return nil
}
c.Register(service.WithFunc(init, run))
ctx, cancel := context.WithCancel(context.Background())
err := c.StartAll(ctx)
t.Logf("Services: %v", c.ServiceNames())
require.NoError(t, err)
cancel()
shutdownCtx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c.WaitAllStopped(shutdownCtx)
require.True(t, initialized)
require.True(t, started)
require.True(t, stopped)
}