Repository navigation
Expand file tree
/
Copy pathruntime_example_test.go
More file actions
79 lines (76 loc) · 2.87 KB
/
Copy pathruntime_example_test.go
File metadata and controls
79 lines (76 loc) · 2.87 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
package statepro_test
import (
"context"
"fmt"
"time"
"github.com/rendis/statepro/v3"
"github.com/rendis/statepro/v3/builtin"
"github.com/rendis/statepro/v3/instrumentation"
"github.com/rendis/statepro/v3/theoretical"
)
// Application budgets are examples; choose them for the application's workload.
func ExampleNewQuantumMachineWithOptions() {
if err := builtin.RegisterObserver("example:allow", func(ctx context.Context, _ instrumentation.ObserverExecutorArgs) (bool, error) {
if err := ctx.Err(); err != nil {
return false, err
}
return true, nil
}); err != nil {
panic(err)
}
if err := builtin.RegisterAction("example:capture", func(ctx context.Context, args instrumentation.ActionExecutorArgs) error {
if err := ctx.Err(); err != nil {
return err
}
// Action arguments capture safely without reentering the owner's lock.
_, err := instrumentation.GetSnapshotWithError(args)
return err
}); err != nil {
panic(err)
}
if err := builtin.RegisterInvoke("example:wait", func(ctx context.Context, _ instrumentation.InvokeExecutorArgs) {
// Replace this with work that observes ctx.Done() or passes ctx to I/O.
<-ctx.Done()
}); err != nil {
panic(err)
}
initial := "idle"
machine, err := statepro.NewQuantumMachineWithOptions(&theoretical.QuantumMachineModel{
ID: "example", Initials: []string{"U:main"}, Universes: map[string]*theoretical.UniverseModel{
"main": {ID: "main", CanonicalName: "main", Initial: &initial, Realities: map[string]*theoretical.RealityModel{
"idle": {ID: "idle", Type: theoretical.RealityTypeTransition, Observers: []*theoretical.ObserverModel{{Src: "example:allow"}}, EntryActions: []*theoretical.ActionModel{{Src: "example:capture"}}, EntryInvokes: []*theoretical.InvokeModel{{Src: "example:wait"}}, On: map[string][]*theoretical.TransitionModel{"GO": {{Targets: []string{"done"}}}}},
"done": {ID: "done", Type: theoretical.RealityTypeFinal},
}},
},
}, instrumentation.RuntimeOptions{
MaxConcurrentInvokes: 8, CancelInvokesOnExit: true,
MaxAccumulatedEvents: 128, MaxTrackingEntries: 64, StrictObservers: true,
})
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := machine.Init(ctx, nil); err != nil {
panic(err)
}
if _, err := machine.SendEvent(ctx, statepro.NewEventBuilder("GO").Build()); err != nil {
panic(err)
}
snapshot, err := instrumentation.GetSnapshotContext(ctx, machine)
if err != nil {
panic(err)
}
fmt.Println(snapshot.Resume.FinalizedUniverses["main"])
// Use a fresh deadline for shutdown even if the event context is canceled.
lifecycle := machine.(instrumentation.RuntimeLifecycle)
if err := lifecycle.Close(); err != nil {
panic(err)
}
shutdownCtx, stop := context.WithTimeout(context.Background(), time.Second)
defer stop()
if err := lifecycle.WaitInvokes(shutdownCtx); err != nil {
panic(err)
}
// Output: done
}