-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_test.go
270 lines (217 loc) · 5.47 KB
/
kernel_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package kareless_test
import (
"context"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/janstoon/toolbox/bricks"
"github.com/stretchr/testify/assert"
"github.com/janstoon/toolbox/kareless"
)
func TestLifeCycle(t *testing.T) {
var (
am avmod
a1 app1
a2 app2
gw *commander1
)
k := kareless.Compile().
Feed(settings{
"port": "123",
"avstep": "5",
}).
Equip(func(_ *kareless.Settings) []kareless.InstrumentCatalogue {
return []kareless.InstrumentCatalogue{
{
Names: strings.Split("encryptor|decrypter", "|"),
Builder: func(ss *kareless.Settings, ib *kareless.InstrumentBank) kareless.Instrument {
am = newAvModifier(ss, ib)
return am
},
},
}
}).
Install(
func(ss *kareless.Settings, ib *kareless.InstrumentBank) kareless.Application {
a1 = newApp1(ss, ib)
return a1
},
func(ss *kareless.Settings, ib *kareless.InstrumentBank) kareless.Application {
a2 = newApp2(ss, ib)
return a2
},
).
Connect(func(ss *kareless.Settings, ib *kareless.InstrumentBank, apps []kareless.Application) kareless.Driver {
adapter := struct {
app1Commander1Adapter
app2Commander1Adapter
}{}
for _, v := range apps {
switch app := v.(type) {
case app1:
adapter.app1Commander1Adapter = newApp1Commander1Adapter(app)
case app2:
adapter.app2Commander1Adapter = newApp2Commander1Adapter(app)
}
}
gw = newCommander1(ss, ib, adapter)
return gw
})
started, stopped := make(chan bool), make(chan bool)
k = k.
AfterStart(
func(ctx context.Context, ss *kareless.Settings, ib *kareless.InstrumentBank, apps []kareless.Application) error {
started <- true
go func() {
<-ctx.Done()
stopped <- true
}()
return nil
},
)
assert.Nil(t, gw)
assert.Nil(t, a1.encryptor)
assert.Nil(t, a2.decrypter)
assert.EqualValues(t, 0, am)
ctx, stop := context.WithCancel(context.Background())
go func() { _ = k.Run(ctx) }()
select {
case <-started:
case <-time.After(500 * time.Millisecond):
assert.Fail(t, "expected post hook to run")
}
assert.NotNil(t, a1.encryptor)
assert.NotNil(t, a2.decrypter)
assert.EqualValues(t, 5, am)
assert.Equal(t, am, a2.decrypter)
assert.Equal(t, am, a1.encryptor)
assert.EqualValues(t, 123, gw.port)
assert.NotNil(t, gw.app)
assert.True(t, gw.isRunning())
assert.Equal(t, "app2.bar app1.foo hello", gw.Bar(ctx, gw.Foo(ctx, "hello")))
stop()
select {
case <-stopped:
case <-time.After(500 * time.Millisecond):
assert.Fail(t, "expected post hook context to get done")
}
assert.Eventually(t, func() bool { return !gw.isRunning() }, 500*time.Millisecond, 10*time.Millisecond)
}
type settings map[string]string
func (ss settings) Get(_ context.Context, key string) (any, error) {
v, ok := ss[key]
if ok {
return v, nil
}
return nil, bricks.ErrNotFound
}
type message struct {
payload string
}
type encryptor interface {
Encrypt(src string) string
}
type app1 struct {
encryptor encryptor
}
func newApp1(ss *kareless.Settings, ib *kareless.InstrumentBank) app1 {
return app1{
encryptor: kareless.ResolveInstrumentByType[encryptor](ib, "encryptor"),
}
}
func (a app1) Foo(ctx context.Context, msg message) string {
return a.encryptor.Encrypt(fmt.Sprintf("app1.foo %s", msg.payload))
}
type decrypter interface {
Decrypt(src string) string
}
type app2 struct {
decrypter decrypter
}
func newApp2(ss *kareless.Settings, ib *kareless.InstrumentBank) app2 {
return app2{
decrypter: kareless.ResolveInstrumentByType[decrypter](ib, "decrypter"),
}
}
func (a app2) Bar(ctx context.Context, msg message) string {
return fmt.Sprintf("app2.bar %s", a.decrypter.Decrypt(msg.payload))
}
type commander1App interface {
Foo(ctx context.Context, msg string) string
Bar(ctx context.Context, msg string) string
}
type commander1 struct {
sync.RWMutex
running bool
port int64
app commander1App
}
func newCommander1(ss *kareless.Settings, ib *kareless.InstrumentBank, app commander1App) *commander1 {
return &commander1{
port: ss.GetInt64("port"),
app: app,
}
}
func (c *commander1) Foo(ctx context.Context, msg string) string {
return c.app.Foo(ctx, msg)
}
func (c *commander1) Bar(ctx context.Context, msg string) string {
return c.app.Bar(ctx, msg)
}
func (c *commander1) Run(ctx context.Context) error {
c.Lock()
c.running = true
c.Unlock()
<-ctx.Done()
c.Lock()
c.running = false
c.Unlock()
return nil
}
func (c *commander1) isRunning() bool {
c.RLock()
defer c.RUnlock()
return c.running
}
type app1Commander1Adapter struct {
app app1
}
func newApp1Commander1Adapter(app app1) app1Commander1Adapter {
return app1Commander1Adapter{
app: app,
}
}
func (adp app1Commander1Adapter) Foo(ctx context.Context, msg string) string {
return adp.app.Foo(ctx, message{payload: msg})
}
type app2Commander1Adapter struct {
app app2
}
func newApp2Commander1Adapter(app app2) app2Commander1Adapter {
return app2Commander1Adapter{
app: app,
}
}
func (adp app2Commander1Adapter) Bar(ctx context.Context, msg string) string {
return adp.app.Bar(ctx, message{payload: msg})
}
type avmod byte
func newAvModifier(ss *kareless.Settings, ib *kareless.InstrumentBank) avmod {
return avmod(ss.GetByte("avstep"))
}
func (h avmod) Encrypt(src string) string {
var dst strings.Builder
for _, v := range src {
dst.WriteByte(byte(v) + byte(h))
}
return dst.String()
}
func (h avmod) Decrypt(src string) string {
var dst strings.Builder
for _, v := range src {
dst.WriteByte(byte(v) - byte(h))
}
return dst.String()
}