Skip to content

Commit

Permalink
feat: add TUI debugger
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta committed Feb 22, 2024
1 parent d51db0f commit 3fc1e60
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ go.work

# local dev
/.dev
/pkg/debugger
/tools/debugger
/docs/cookbook.md
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
run:
skip-files:
- "examples/.*"
- "pkg/debugger/.*"

linters:
enable:
Expand Down
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module github.com/pancsta/asyncmachine-go
go 1.21

require (
code.rocketnine.space/tslocum/cbind v0.1.5
github.com/gdamore/tcell/v2 v2.7.0
github.com/google/uuid v1.5.0
github.com/hibiken/asynq v0.24.1
github.com/lithammer/dedent v1.1.0
github.com/pancsta/cview v1.5.10
github.com/samber/lo v1.39.0
github.com/stretchr/testify v1.8.4
)
Expand All @@ -14,14 +17,22 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/redis/go-redis/v9 v9.0.3 // indirect
github.com/rivo/uniseg v0.4.7-0.20240127222946-601bbb3750c2 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

//replace github.com/pancsta/cview => ../cview
53 changes: 52 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
code.rocketnine.space/tslocum/cbind v0.1.5 h1:i6NkeLLNPNMS4NWNi3302Ay3zSU6MrqOT+yJskiodxE=
code.rocketnine.space/tslocum/cbind v0.1.5/go.mod h1:LtfqJTzM7qhg88nAvNhx+VnTjZ0SXBJtxBObbfBWo/M=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
Expand All @@ -9,6 +11,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/gdamore/tcell/v2 v2.7.0 h1:I5LiGTQuwrysAt1KS9wg1yFfOI3arI3ucFrxtd/xqaA=
github.com/gdamore/tcell/v2 v2.7.0/go.mod h1:hl/KtAANGBecfIPxk+FzKvThTqI84oplgbPEmVX60b8=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand All @@ -27,10 +34,24 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pancsta/cview v1.5.10 h1:Lmj5+klFAAubvd3jAjpmPFW4aQfh+FUtR8t5kFH7IDQ=
github.com/pancsta/cview v1.5.10/go.mod h1:BkP0JtxfBHYZuc4CmK/O5H5fNCEdGAgndVeJlGiS3TQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7-0.20240127222946-601bbb3750c2 h1:tcc3ZFBvjydcgrAxavZRYqFqCKzy0FJ+UY4ATq4QVXk=
github.com/rivo/uniseg v0.4.7-0.20240127222946-601bbb3750c2/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
Expand All @@ -43,36 +64,66 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309040221-94ec62e08169/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
13 changes: 12 additions & 1 deletion pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ func (m *Machine) processQueue() Result {
m.queueLock.RUnlock()
}

m.emit("queue-end", nil, nil)
m.emit(EventQueueEnd, nil, nil)
m.Transition = nil
// release the atomic lock
m.queueProcessing.Swap(false)
Expand Down Expand Up @@ -1296,3 +1296,14 @@ func (m *Machine) Inspect(states S) string {
}
return ret
}

// Switch returns the first state from the passed list that is currently active,
// making it useful for switch statements.
func (m *Machine) Switch(states ...string) string {
for _, state := range states {
if lo.Contains(m.ActiveStates, state) {
return state
}
}
return ""
}
15 changes: 13 additions & 2 deletions pkg/machine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ func (h *TestNegotiationCancelHandlers) DEnter(_ *am.Event) bool {
return false
}

// TODO when transition is canceled, it should not set the auto states
func TestNegotiationCancel(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -952,21 +951,33 @@ func TestClock(t *testing.T) {
// ()[]
m.Add(S{"A", "B"}, nil)
// (A B)[A1 B1]
assertStates(t, m, S{"A", "B"})

m.Add(S{"A", "B"}, nil)
// (A B)[A1 B2]
assertStates(t, m, S{"A", "B"})

m.Add(S{"A", "B", "C"}, nil)
// (A B C)[A1 B3 C1]
assertStates(t, m, S{"A", "B", "C"})

m.Set(S{"D"}, nil)
// (D)[A1 B3 C1 D1]
assertStates(t, m, S{"D"})

m.Add(S{"D", "C"}, nil)
// (D C)[A1 B3 C2 D1]
assertStates(t, m, S{"D", "C"})

m.Remove(S{"B", "C"}, nil)
// (D)[A1 B3 C2 D1]
assertStates(t, m, S{"D"})

m.Add(S{"A", "B"}, nil)
// (D A B)[A2 B4 C2 D1]
assertStates(t, m, S{"D", "A", "B"})

// assert
assertStates(t, m, S{"A", "B", "D"})
assert.Equal(t, m.Time(S{"A", "B", "C", "D"}), T{2, 4, 2, 1})

// test 2
Expand Down
21 changes: 17 additions & 4 deletions pkg/machine/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ func (r Result) String() string {
return ""
}

const (
EventTransitionEnd string = "transition-end"
EventTransitionStart string = "transition-start"
EventTransitionCancel string = "transition-cancel"
EventQueueEnd string = "queue-end"
EventTick string = "tick"
)

// MutationType enum
type MutationType int

Expand Down Expand Up @@ -110,13 +118,15 @@ type Mutation struct {
}

// TransitionStepType enum
// TODO rename to StepType
type TransitionStepType int

const (
TransitionStepTypeRelation TransitionStepType = 1 << iota
TransitionStepTypeTransition
TransitionStepTypeSet
TransitionStepTypeRemove
// TODO rename to StepTypeRemoveNotSet
TransitionStepTypeNoSet
TransitionStepTypeRequested
TransitionStepTypeCancel
Expand All @@ -127,12 +137,14 @@ func (tt TransitionStepType) String() string {
case TransitionStepTypeRelation:
return "rel"
case TransitionStepTypeTransition:
// TODO rename to TransitionStepTypeHandler ?
return "transition"
case TransitionStepTypeSet:
return "set"
case TransitionStepTypeRemove:
return "remove"
case TransitionStepTypeNoSet:
// TODO rename to TransitionStepTypeRemoveTry ?
return "no-set"
case TransitionStepTypeRequested:
return "requested"
Expand Down Expand Up @@ -167,8 +179,8 @@ const (
RelationRemove
)

func (r Relation) String(relation Relation) string {
switch relation {
func (r Relation) String() string {
switch r {
case RelationAfter:
return "after"
case RelationAdd:
Expand Down Expand Up @@ -283,11 +295,12 @@ type ExceptionArgsPanic struct {
func (eh *ExceptionHandler) ExceptionState(e *Event) {
if e.Machine.PrintExceptions {
err := e.Args["err"].(error)
details := e.Args["panic"].(*ExceptionArgsPanic)
if details == nil {
_, detailsOK := e.Args["panic"]
if !detailsOK {
e.Machine.log(LogChanges, "[error:%s] %s (%s)", err)
return
}
details := e.Args["panic"].(*ExceptionArgsPanic)
mutType := details.Transition.Mutation.Type
e.Machine.log(LogChanges, "[error:%s] %s (%s)", mutType,
j(details.CalledStates), err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/machine/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ func (rr *DefaultRelationsResolver) getMissingRequires(
t := rr.Transition
ret := S{}
for _, req := range state.Require {
t.addSteps(newStep(req, name, TransitionStepTypeRelation,
t.addSteps(newStep(name, req, TransitionStepTypeRelation,
RelationRequire))
if lo.Contains(states, req) {
continue
}
ret = append(ret, req)
t.addSteps(newStep(name, "", TransitionStepTypeNoSet, nil))
if lo.Contains(t.Mutation.CalledStates, name) {
t.addSteps(newStep(req, "",
t.addSteps(newStep("", req,
TransitionStepTypeCancel, nil))
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/machine/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func newTransition(m *Machine, item *Mutation) *Transition {
}
// set early to catch the logs
m.Transition = t
// set early to catch the logs
m.Transition = t
states := t.CalledStates()
mutType := t.Type()
t.addSteps(newSteps("", states, TransitionStepTypeRequested, nil)...)
Expand Down Expand Up @@ -178,6 +176,7 @@ func (t *Transition) setupExitEnter() {
for _, s := range t.TargetStates {
// enter activate state only for multi states called directly
// not implied by Add
// TODO m.is()
if m.Is(S{s}) && !(m.States[s].Multi && lo.Contains(t.CalledStates(), s)) {
continue
}
Expand Down Expand Up @@ -291,11 +290,12 @@ func (t *Transition) emitEvents() Result {
if !t.Accepted {
result = Canceled
}
// TODO struct type
txArgs := A{"transition": t}
hasStateChanged := false

// start emitting
m.emit("transition-start", txArgs, nil)
m.emit(EventTransitionStart, txArgs, nil)

// NEGOTIATION CALLS PHASE (cancellable)
// FooFoo handlers
Expand All @@ -318,13 +318,13 @@ func (t *Transition) emitEvents() Result {
t.IsCompleted = true
hasStateChanged = m.HasStateChanged(t.StatesBefore, t.ClocksBefore)
if hasStateChanged {
m.emit("tick", A{"before": t.StatesBefore}, nil)
m.emit(EventTick, A{"before": t.StatesBefore}, nil)
}
}

// AUTO STATES
if result == Canceled {
m.emit("transition-cancel", txArgs, nil)
m.emit(EventTransitionCancel, txArgs, nil)
} else if hasStateChanged && !t.IsAuto() {
autoMutation := m.Resolver.GetAutoMutation()
if autoMutation != nil {
Expand All @@ -340,7 +340,7 @@ func (t *Transition) emitEvents() Result {
txArgs["pre_logs"] = m.logEntries
m.logEntries = []string{}
m.logEntriesLock.Unlock()
m.emit("transition-end", txArgs, nil)
m.emit(EventTransitionEnd, txArgs, nil)

if result == Canceled {
return Canceled
Expand Down

0 comments on commit 3fc1e60

Please sign in to comment.