-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_test.go
More file actions
121 lines (102 loc) · 2.97 KB
/
runtime_test.go
File metadata and controls
121 lines (102 loc) · 2.97 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package core_test
import (
"context"
"testing"
. "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
// --- ServiceRuntime ---
type testOpts struct {
URL string
Timeout int
}
func TestRuntime_ServiceRuntime_Good(t *testing.T) {
c := New()
opts := testOpts{URL: "https://api.lthn.ai", Timeout: 30}
rt := NewServiceRuntime(c, opts)
assert.Equal(t, c, rt.Core())
assert.Equal(t, opts, rt.Options())
assert.Equal(t, "https://api.lthn.ai", rt.Options().URL)
assert.NotNil(t, rt.Config())
}
// --- NewWithFactories ---
func TestRuntime_NewWithFactories_Good(t *testing.T) {
r := NewWithFactories(nil, map[string]ServiceFactory{
"svc1": func() Result { return Result{Value: Service{}, OK: true} },
"svc2": func() Result { return Result{Value: Service{}, OK: true} },
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
assert.NotNil(t, rt.Core)
}
func TestRuntime_NewWithFactories_NilFactory_Good(t *testing.T) {
r := NewWithFactories(nil, map[string]ServiceFactory{
"bad": nil,
})
assert.True(t, r.OK) // nil factories skipped
}
func TestRuntime_NewRuntime_Good(t *testing.T) {
r := NewRuntime(nil)
assert.True(t, r.OK)
}
func TestRuntime_ServiceName_Good(t *testing.T) {
r := NewRuntime(nil)
rt := r.Value.(*Runtime)
assert.Equal(t, "Core", rt.ServiceName())
}
// --- Lifecycle via Runtime ---
func TestRuntime_Lifecycle_Good(t *testing.T) {
started := false
r := NewWithFactories(nil, map[string]ServiceFactory{
"test": func() Result {
return Result{Value: Service{
OnStart: func() Result { started = true; return Result{OK: true} },
}, OK: true}
},
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
result := rt.ServiceStartup(context.Background(), nil)
assert.True(t, result.OK)
assert.True(t, started)
}
func TestRuntime_ServiceShutdown_Good(t *testing.T) {
stopped := false
r := NewWithFactories(nil, map[string]ServiceFactory{
"test": func() Result {
return Result{Value: Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { stopped = true; return Result{OK: true} },
}, OK: true}
},
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
rt.ServiceStartup(context.Background(), nil)
result := rt.ServiceShutdown(context.Background())
assert.True(t, result.OK)
assert.True(t, stopped)
}
func TestRuntime_ServiceShutdown_NilCore_Good(t *testing.T) {
rt := &Runtime{}
result := rt.ServiceShutdown(context.Background())
assert.True(t, result.OK)
}
func TestCore_ServiceShutdown_Good(t *testing.T) {
stopped := false
c := New()
c.Service("test", Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { stopped = true; return Result{OK: true} },
})
c.ServiceStartup(context.Background(), nil)
result := c.ServiceShutdown(context.Background())
assert.True(t, result.OK)
assert.True(t, stopped)
}
func TestCore_Context_Good(t *testing.T) {
c := New()
c.ServiceStartup(context.Background(), nil)
assert.NotNil(t, c.Context())
c.ServiceShutdown(context.Background())
}