-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke_test.go
More file actions
62 lines (54 loc) · 1.55 KB
/
invoke_test.go
File metadata and controls
62 lines (54 loc) · 1.55 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
package gstate
import (
"context"
"errors"
"testing"
"time"
)
func TestActorInvoke(t *testing.T) {
m := New[StateID, EventID, Context]("invoke").
Initial("loading").
State("loading", func(s *StateBuilder[StateID, EventID, Context]) {
s.Invoke(func(ctx context.Context, c Context) error {
time.Sleep(20 * time.Millisecond)
return nil
}, "success", "failure")
}).
State("success", func(s *StateBuilder[StateID, EventID, Context]) {
s.Type(Final)
}).
State("failure", func(s *StateBuilder[StateID, EventID, Context]) {
s.Type(Final)
}).
Build()
actor := Start(m, Context{})
if actor.State() != "loading" {
t.Errorf("Expected state loading, got %s", actor.State())
}
time.Sleep(50 * time.Millisecond)
if actor.State() != "success" {
t.Errorf("Expected state success after invocation, got %s", actor.State())
}
}
func TestActorInvokeError(t *testing.T) {
m := New[StateID, EventID, Context]("invoke_error").
Initial("loading").
State("loading", func(s *StateBuilder[StateID, EventID, Context]) {
s.Invoke(func(ctx context.Context, c Context) error {
time.Sleep(20 * time.Millisecond)
return errors.New("fail")
}, "success", "failure")
}).
State("success", func(s *StateBuilder[StateID, EventID, Context]) {
s.Type(Final)
}).
State("failure", func(s *StateBuilder[StateID, EventID, Context]) {
s.Type(Final)
}).
Build()
actor := Start(m, Context{})
time.Sleep(50 * time.Millisecond)
if actor.State() != "failure" {
t.Errorf("Expected state failure after error, got %s", actor.State())
}
}