-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
460 lines (373 loc) · 10.1 KB
/
example_test.go
File metadata and controls
460 lines (373 loc) · 10.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package introspection_test
import (
"context"
"fmt"
"time"
introspection "github.com/aretw0/introspection"
)
// Example demonstrates basic usage of the introspection package
// for observing and visualizing component state.
func Example() {
// Define a simple component state
type ServiceState struct {
Name string
Status string
}
// Create a component that implements TypedWatcher
service := &simpleWatcher[ServiceState]{
state: ServiceState{Name: "API", Status: "Running"},
ch: make(chan introspection.StateChange[ServiceState], 1),
}
// Watch for state changes
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
changes := service.Watch(ctx)
// Trigger a state change
service.UpdateState(ServiceState{Name: "API", Status: "Stopped"})
// Observe the change
select {
case change := <-changes:
fmt.Printf("State changed: %s -> %s\n", change.OldState.Status, change.NewState.Status)
case <-time.After(50 * time.Millisecond):
}
// Output:
// State changed: Running -> Stopped
}
// ExampleTreeDiagram demonstrates generating a Mermaid diagram
// from a hierarchical data structure.
func ExampleTreeDiagram() {
// Define a tree structure for tasks
type Task struct {
Name string
Status string
Children []Task
}
// Create a task hierarchy
root := Task{
Name: "Project",
Status: "Active",
Children: []Task{
{Name: "Backend", Status: "Running"},
{Name: "Frontend", Status: "Running"},
},
}
// Generate diagram with configuration
config := introspection.DefaultDiagramConfig()
config.SecondaryID = "tasks"
diagram := introspection.TreeDiagram(root, config)
// The diagram contains Mermaid markup
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleComponentDiagram demonstrates creating a diagram showing
// relationships between two components.
func ExampleComponentDiagram() {
type Controller struct {
Name string
}
type Worker struct {
ID int
Status string
}
controller := Controller{Name: "MainController"}
worker := Worker{ID: 1, Status: "Active"}
config := introspection.DefaultDiagramConfig()
config.PrimaryID = "controller"
config.PrimaryLabel = "Controller"
config.SecondaryID = "worker"
config.SecondaryLabel = "Worker"
config.ConnectionLabel = "manages"
diagram := introspection.ComponentDiagram(controller, worker, config)
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleStateMachineDiagram demonstrates generating a state machine
// visualization for a component.
func ExampleStateMachineDiagram() {
type ProcessState struct {
Running bool
Stopped bool
}
state := ProcessState{Running: true}
config := introspection.DefaultStateMachineConfig()
config.InitialState = "Idle"
config.GracefulState = "Stopping"
config.ForcedState = "Killed"
config.InitialToGraceful = "STOP"
config.GracefulToForced = "KILL"
config.GracefulToFinal = "Exit"
diagram := introspection.StateMachineDiagram(state, config)
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleAggregateWatchers demonstrates combining state changes
// from multiple components into a single stream.
func ExampleAggregateWatchers() {
type ServiceState struct {
Name string
Status string
}
// Create multiple watchers
service1 := &simpleWatcher[ServiceState]{
id: "service-1",
state: ServiceState{Name: "API", Status: "Running"},
ch: make(chan introspection.StateChange[ServiceState], 1),
}
service2 := &simpleWatcher[ServiceState]{
id: "service-2",
state: ServiceState{Name: "Worker", Status: "Running"},
ch: make(chan introspection.StateChange[ServiceState], 1),
}
// Aggregate state changes from both services
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
snapshots := introspection.AggregateWatchers(ctx, service1, service2)
// Trigger state changes
service1.UpdateState(ServiceState{Name: "API", Status: "Stopped"})
service2.UpdateState(ServiceState{Name: "Worker", Status: "Idle"})
// Collect snapshots
count := 0
for range snapshots {
count++
if count >= 2 {
cancel()
}
}
fmt.Printf("Received %d snapshots\n", count)
// Output:
// Received 2 snapshots
}
// ExampleNewWatcherAdapter demonstrates wrapping a TypedWatcher
// to convert it to StateSnapshot stream for aggregation.
func ExampleNewWatcherAdapter() {
type ServiceState struct {
Name string
Status string
}
service := &simpleWatcher[ServiceState]{
id: "api",
state: ServiceState{Name: "API", Status: "Running"},
ch: make(chan introspection.StateChange[ServiceState], 1),
}
// Create an adapter
adapter := introspection.NewWatcherAdapter("service", service)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
snapshots := adapter.Snapshots(ctx)
// Trigger a state change
service.UpdateState(ServiceState{Name: "API", Status: "Stopped"})
// Observe the snapshot
select {
case snapshot := <-snapshots:
fmt.Printf("Snapshot from: %s, Type: %s\n", snapshot.ComponentID, snapshot.ComponentType)
case <-time.After(50 * time.Millisecond):
}
// Output:
// Snapshot from: api, Type: service
}
// simpleWatcher is a minimal TypedWatcher implementation for examples.
type simpleWatcher[S any] struct {
id string
state S
ch chan introspection.StateChange[S]
}
func (w *simpleWatcher[S]) ComponentType() string {
return "service"
}
func (w *simpleWatcher[S]) State() S {
return w.state
}
func (w *simpleWatcher[S]) Watch(ctx context.Context) <-chan introspection.StateChange[S] {
out := make(chan introspection.StateChange[S])
go func() {
defer close(out)
for {
select {
case change := <-w.ch:
select {
case out <- change:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
return out
}
func (w *simpleWatcher[S]) UpdateState(newState S) {
oldState := w.state
w.state = newState
w.ch <- introspection.StateChange[S]{
ComponentID: w.id,
ComponentType: "service",
OldState: oldState,
NewState: newState,
Timestamp: time.Now(),
}
}
// ExampleWorkerTreeDiagram demonstrates the legacy WorkerTreeDiagram function
// for backward compatibility with the worker/signal domain.
func ExampleWorkerTreeDiagram() {
// Define worker state (legacy domain)
type WorkerState struct {
Name string
Status string
PID int
Metadata map[string]string
Children []WorkerState
}
root := WorkerState{
Name: "supervisor",
Status: "Running",
Children: []WorkerState{
{Name: "worker-1", Status: "Running", PID: 1001},
{Name: "worker-2", Status: "Idle", PID: 1002},
},
}
diagram := introspection.WorkerTreeDiagram(root)
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleSignalStateMachine demonstrates the legacy SignalStateMachine function
// for backward compatibility with the signal domain.
func ExampleSignalStateMachine() {
type SignalState struct {
Enabled bool
Stopping bool
ForceExitThreshold int
}
state := SignalState{
Enabled: true,
ForceExitThreshold: 2,
}
diagram := introspection.SignalStateMachine(state)
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleSystemDiagram demonstrates the legacy SystemDiagram function
// that combines signal context and worker tree.
func ExampleSystemDiagram() {
type SignalState struct {
Enabled bool
Stopping bool
}
type WorkerState struct {
Name string
Status string
Children []WorkerState
}
signal := SignalState{Enabled: true}
worker := WorkerState{
Name: "root",
Status: "Running",
Children: []WorkerState{
{Name: "child-1", Status: "Running"},
},
}
diagram := introspection.SystemDiagram(signal, worker)
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// ExampleAggregateEvents demonstrates combining events from multiple
// event sources into a single stream.
func ExampleAggregateEvents() {
// Create mock event sources
source1 := &mockEventSource{
id: "source-1",
ch: make(chan introspection.ComponentEvent, 1),
}
source2 := &mockEventSource{
id: "source-2",
ch: make(chan introspection.ComponentEvent, 1),
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
events := introspection.AggregateEvents(ctx, source1, source2)
// Send events from both sources
source1.SendEvent(&mockEvent{id: "source-1", eventType: "started"})
source2.SendEvent(&mockEvent{id: "source-2", eventType: "connected"})
// Collect events
count := 0
for range events {
count++
if count >= 2 {
cancel()
}
}
fmt.Printf("Received %d events\n", count)
// Output:
// Received 2 events
}
// ExampleWithStyles demonstrates customizing Mermaid diagram styles.
func ExampleWithStyles() {
type Task struct {
Name string
Status string
Children []Task
}
root := Task{
Name: "Main",
Status: "Running",
}
customStyles := `
classDef running fill:#90EE90
classDef failed fill:#FFB6C1
`
config := introspection.DefaultDiagramConfig()
diagram := introspection.TreeDiagram(root, config, introspection.WithStyles(customStyles))
fmt.Println(len(diagram) > 0)
// Output:
// true
}
// mockEventSource is a simple EventSource implementation for examples.
type mockEventSource struct {
id string
ch chan introspection.ComponentEvent
}
func (m *mockEventSource) Events(ctx context.Context) <-chan introspection.ComponentEvent {
out := make(chan introspection.ComponentEvent)
go func() {
defer close(out)
for {
select {
case event := <-m.ch:
select {
case out <- event:
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}()
return out
}
func (m *mockEventSource) SendEvent(event introspection.ComponentEvent) {
m.ch <- event
}
// mockEvent is a simple ComponentEvent implementation for examples.
type mockEvent struct {
id string
eventType string
}
func (e *mockEvent) ComponentID() string {
return e.id
}
func (e *mockEvent) ComponentType() string {
return "service"
}
func (e *mockEvent) Timestamp() time.Time {
return time.Now()
}
func (e *mockEvent) EventType() string {
return e.eventType
}