-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
370 lines (309 loc) · 7.73 KB
/
context_test.go
File metadata and controls
370 lines (309 loc) · 7.73 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package statechartx_test
import (
"context"
"fmt"
"sync"
"testing"
. "github.com/comalice/statechartx"
)
func TestContextBasic(t *testing.T) {
ctx := NewContext()
// Test Set/Get
ctx.Set("key", "value")
if got := ctx.Get("key"); got != "value" {
t.Errorf("expected 'value', got %v", got)
}
// Test missing key returns nil
if got := ctx.Get("missing"); got != nil {
t.Errorf("expected nil for missing key, got %v", got)
}
// Test Delete
ctx.Delete("key")
if got := ctx.Get("key"); got != nil {
t.Errorf("expected nil after delete, got %v", got)
}
}
func TestContextTypes(t *testing.T) {
ctx := NewContext()
// Test different types
ctx.Set("string", "value")
ctx.Set("int", 42)
ctx.Set("bool", true)
ctx.Set("slice", []string{"a", "b", "c"})
ctx.Set("map", map[string]int{"x": 1})
if ctx.Get("string") != "value" {
t.Error("string value mismatch")
}
if ctx.Get("int") != 42 {
t.Error("int value mismatch")
}
if ctx.Get("bool") != true {
t.Error("bool value mismatch")
}
}
func TestContextConcurrency(t *testing.T) {
ctx := NewContext()
var wg sync.WaitGroup
// 100 concurrent writers
for i := 0; i < 100; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
ctx.Set(fmt.Sprintf("key%d", id), id)
}(i)
}
// 100 concurrent readers
for i := 0; i < 100; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
_ = ctx.Get(fmt.Sprintf("key%d", id))
}(i)
}
// 50 concurrent deleters
for i := 0; i < 50; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
ctx.Delete(fmt.Sprintf("key%d", id))
}(i)
}
wg.Wait()
// No race conditions (run with -race flag)
}
func TestContextGetAll(t *testing.T) {
ctx := NewContext()
ctx.Set("a", 1)
ctx.Set("b", 2)
ctx.Set("c", 3)
all := ctx.GetAll()
if len(all) != 3 {
t.Errorf("expected 3 items, got %d", len(all))
}
if all["a"] != 1 || all["b"] != 2 || all["c"] != 3 {
t.Errorf("GetAll mismatch: %v", all)
}
// Mutation of snapshot doesn't affect original
all["d"] = 4
if ctx.Get("d") != nil {
t.Error("GetAll should return defensive copy")
}
// Original still has 3 items
all2 := ctx.GetAll()
if len(all2) != 3 {
t.Error("original context should be unchanged")
}
}
func TestContextLoadAll(t *testing.T) {
ctx := NewContext()
ctx.Set("old", "value")
ctx.Set("also_old", "data")
newData := map[string]any{
"new": "data",
"another": 123,
}
ctx.LoadAll(newData)
// Old keys should be gone
if ctx.Get("old") != nil {
t.Error("LoadAll should replace, not merge - old key still exists")
}
if ctx.Get("also_old") != nil {
t.Error("LoadAll should replace, not merge - also_old key still exists")
}
// New keys should exist
if ctx.Get("new") != "data" {
t.Error("LoadAll should set new data")
}
if ctx.Get("another") != 123 {
t.Error("LoadAll should set all new data")
}
}
func TestContextLoadAllNil(t *testing.T) {
ctx := NewContext()
ctx.Set("key", "value")
// LoadAll with nil should clear everything
ctx.LoadAll(nil)
if ctx.Get("key") != nil {
t.Error("LoadAll(nil) should clear context")
}
all := ctx.GetAll()
if len(all) != 0 {
t.Error("context should be empty after LoadAll(nil)")
}
}
func TestRuntimeCtxAccessor(t *testing.T) {
root := &State{ID: 1}
machine, err := NewMachine(root)
if err != nil {
t.Fatal(err)
}
// Auto-created Context when ext is nil
rt := NewRuntime(machine, nil)
ctx := rt.Ctx()
if ctx == nil {
t.Fatal("expected auto-created Context")
}
// Should be able to use the context
ctx.Set("test", "value")
if ctx.Get("test") != "value" {
t.Error("Context should work through Ctx() accessor")
}
// Custom ext (not a Context)
customExt := map[string]string{"custom": "data"}
rt2 := NewRuntime(machine, customExt)
if rt2.Ctx() != nil {
t.Error("Ctx() should return nil for non-Context ext")
}
// Explicitly created Context
explicitCtx := NewContext()
explicitCtx.Set("explicit", "value")
rt3 := NewRuntime(machine, explicitCtx)
if rt3.Ctx() != explicitCtx {
t.Error("Ctx() should return the explicitly provided Context")
}
if rt3.Ctx().Get("explicit") != "value" {
t.Error("explicit Context should preserve data")
}
}
func TestContextOverwrite(t *testing.T) {
ctx := NewContext()
ctx.Set("key", "first")
if ctx.Get("key") != "first" {
t.Error("first set failed")
}
ctx.Set("key", "second")
if ctx.Get("key") != "second" {
t.Error("overwrite failed")
}
ctx.Set("key", 42)
if ctx.Get("key") != 42 {
t.Error("type change failed")
}
}
func TestContextDeleteNonExistent(t *testing.T) {
ctx := NewContext()
// Deleting non-existent key should not panic
ctx.Delete("nonexistent")
// Should still be able to use context
ctx.Set("key", "value")
if ctx.Get("key") != "value" {
t.Error("context should still work after deleting non-existent key")
}
}
// ==================== FromContext Tests ====================
func TestFromContext_WithoutContext(t *testing.T) {
goCtx := context.Background()
retrieved := FromContext(goCtx)
if retrieved != nil {
t.Fatal("expected nil context when not stored")
}
}
func TestFromContext_WithNilContext(t *testing.T) {
retrieved := FromContext(nil)
if retrieved != nil {
t.Fatal("expected nil context when passed nil")
}
}
func TestContextInEntryActionSimple(t *testing.T) {
var capturedCtx *Context
root := &State{
ID: 1,
EntryAction: func(ctx context.Context, evt *Event, from StateID, to StateID) error {
capturedCtx = FromContext(ctx)
return nil
},
}
machine, err := NewMachine(root)
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
if err := rt.Start(context.Background()); err != nil {
t.Fatal(err)
}
defer rt.Stop()
if capturedCtx == nil {
t.Fatal("entry action did not receive context")
}
// Verify the context is the runtime's context
if capturedCtx != rt.Ctx() {
t.Fatal("captured context is not the runtime's context")
}
}
func TestContextInCompoundState(t *testing.T) {
var parentCtx, childCtx *Context
child := &State{
ID: 2,
EntryAction: func(ctx context.Context, evt *Event, from StateID, to StateID) error {
childCtx = FromContext(ctx)
return nil
},
}
parent := &State{
ID: 1,
Initial: 2,
Children: map[StateID]*State{
2: child,
},
EntryAction: func(ctx context.Context, evt *Event, from StateID, to StateID) error {
parentCtx = FromContext(ctx)
return nil
},
}
child.Parent = parent
machine, err := NewMachine(parent)
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, nil)
if err := rt.Start(context.Background()); err != nil {
t.Fatal(err)
}
defer rt.Stop()
if parentCtx == nil {
t.Fatal("parent entry action did not receive context")
}
if childCtx == nil {
t.Fatal("child entry action did not receive context")
}
if parentCtx != childCtx {
t.Fatal("parent and child should receive same context")
}
}
func TestContextPreservesValues(t *testing.T) {
var capturedCtx *Context
// Create context with initial values
initialCtx := NewContext()
initialCtx.Set("initial_key", "initial_value")
root := &State{
ID: 1,
EntryAction: func(ctx context.Context, evt *Event, from StateID, to StateID) error {
capturedCtx = FromContext(ctx)
// Set a value from within the action
if capturedCtx != nil {
capturedCtx.Set("action_key", "action_value")
}
return nil
},
}
machine, err := NewMachine(root)
if err != nil {
t.Fatal(err)
}
rt := NewRuntime(machine, initialCtx)
if err := rt.Start(context.Background()); err != nil {
t.Fatal(err)
}
defer rt.Stop()
if capturedCtx == nil {
t.Fatal("entry action did not receive context")
}
// Verify initial value is preserved
if capturedCtx.Get("initial_key") != "initial_value" {
t.Fatal("initial value not preserved")
}
// Verify action-set value exists
if capturedCtx.Get("action_key") != "action_value" {
t.Fatal("action-set value not found")
}
}