Added 'ready' state between init and start for events, and added String for more signals

gql_cataclysm
noah metz 2023-11-02 20:58:38 -06:00
parent 3a6e562390
commit 8e3510129c
3 changed files with 38 additions and 11 deletions

@ -50,6 +50,10 @@ type EventControlSignal struct {
Command string Command string
} }
func (signal EventControlSignal) String() string {
return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command)
}
func NewEventControlSignal(command string) *EventControlSignal { func NewEventControlSignal(command string) *EventControlSignal {
return &EventControlSignal{ return &EventControlSignal{
NewSignalHeader(Direct), NewSignalHeader(Direct),
@ -76,19 +80,29 @@ func (ext *EventExt) Process(ctx *Context, node *Node, source NodeID, signal Sig
return messages, changes return messages, changes
} }
type TestEventExt struct {} type TestEventExt struct {
Length time.Duration
}
var test_event_transitions = map[string]struct{ var test_event_transitions = map[string]struct{
from_state string from_state string
to_state string to_state string
}{ }{
"start": { "ready?": {
"init", "init",
"ready",
},
"start": {
"ready",
"running", "running",
}, },
"abort": {
"ready",
"init",
},
"stop": { "stop": {
"running", "running",
"init", "stopped",
}, },
"finish": { "finish": {
"running", "running",
@ -114,6 +128,9 @@ func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal
event_ext.State = info.to_state event_ext.State = info.to_state
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id)) messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
node.QueueSignal(time.Now(), NewEventStateSignal(node.ID, event_ext.State, time.Now())) node.QueueSignal(time.Now(), NewEventStateSignal(node.ID, event_ext.State, time.Now()))
if event_ext.State == "running" {
node.QueueSignal(time.Now().Add(ext.Length), NewEventControlSignal("finish"))
}
} else { } else {
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_state")) messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_state"))
} }

@ -7,16 +7,16 @@ import (
) )
func TestEvent(t *testing.T) { func TestEvent(t *testing.T) {
ctx := logTestContext(t, []string{"event", "listener"}) ctx := logTestContext(t, []string{"event", "listener", "listener_debug"})
err := ctx.RegisterExtension(reflect.TypeOf(&TestEventExt{}), NewExtType("TEST_EVENT"), nil) err := ctx.RegisterExtension(reflect.TypeOf(&TestEventExt{}), NewExtType("TEST_EVENT"), nil)
fatalErr(t, err) fatalErr(t, err)
event_listener := NewListenerExt(100) event_listener := NewListenerExt(100)
event, err := NewNode(ctx, nil, BaseNodeType, 100, nil, NewEventExt(nil, "Test Event"), &TestEventExt{}, event_listener) event, err := NewNode(ctx, nil, BaseNodeType, 100, nil, NewEventExt(nil, "Test Event"), &TestEventExt{time.Second}, event_listener)
fatalErr(t, err) fatalErr(t, err)
response, signals := testSend(t, ctx, NewEventControlSignal("start"), event, event) response, signals := testSend(t, ctx, NewEventControlSignal("ready?"), event, event)
switch resp := response.(type) { switch resp := response.(type) {
case *SuccessSignal: case *SuccessSignal:
case *ErrorSignal: case *ErrorSignal:
@ -28,7 +28,7 @@ func TestEvent(t *testing.T) {
var state_signal *EventStateSignal = nil var state_signal *EventStateSignal = nil
for _, signal := range(signals) { for _, signal := range(signals) {
event_state, is_event_state := signal.(*EventStateSignal) event_state, is_event_state := signal.(*EventStateSignal)
if is_event_state == true && event_state.Source == event.ID && event_state.State == "running" { if is_event_state == true && event_state.Source == event.ID && event_state.State == "ready" {
state_signal = event_state state_signal = event_state
break break
} }
@ -36,12 +36,12 @@ func TestEvent(t *testing.T) {
if state_signal == nil { if state_signal == nil {
state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool { state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool {
return sig.Source == event.ID && sig.State == "running" return sig.Source == event.ID && sig.State == "ready"
}) })
fatalErr(t, err) fatalErr(t, err)
} }
response, signals = testSend(t, ctx, NewEventControlSignal("finish"), event, event) response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event)
switch resp := response.(type) { switch resp := response.(type) {
case *SuccessSignal: case *SuccessSignal:
case *ErrorSignal: case *ErrorSignal:
@ -53,7 +53,7 @@ func TestEvent(t *testing.T) {
state_signal = nil state_signal = nil
for _, signal := range(signals) { for _, signal := range(signals) {
event_state, is_event_state := signal.(*EventStateSignal) event_state, is_event_state := signal.(*EventStateSignal)
if is_event_state == true && event_state.Source == event.ID && event_state.State == "done" { if is_event_state == true && event_state.Source == event.ID && event_state.State == "running" {
state_signal = event_state state_signal = event_state
break break
} }
@ -61,11 +61,16 @@ func TestEvent(t *testing.T) {
if state_signal == nil { if state_signal == nil {
state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool { state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool {
return sig.Source == event.ID && sig.State == "done" return sig.Source == event.ID && sig.State == "running"
}) })
fatalErr(t, err) fatalErr(t, err)
} }
_, err = WaitForSignal(event_listener.Chan, time.Second * 2, func(sig *EventStateSignal) bool {
return sig.Source == event.ID && sig.State == "done"
})
fatalErr(t, err)
response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event) response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event)
switch resp := response.(type) { switch resp := response.(type) {
case *SuccessSignal: case *SuccessSignal:

@ -202,6 +202,11 @@ func NewStopSignal() *StopSignal {
type SuccessSignal struct { type SuccessSignal struct {
ResponseHeader ResponseHeader
} }
func (signal SuccessSignal) String() string {
return fmt.Sprintf("SuccessSignal(%s)", signal.SignalHeader)
}
func (signal SuccessSignal) Permission() Tree { func (signal SuccessSignal) Permission() Tree {
return Tree{ return Tree{
ResponseType: { ResponseType: {