diff --git a/event.go b/event.go index c68c421..a516aa2 100644 --- a/event.go +++ b/event.go @@ -50,6 +50,10 @@ type EventControlSignal struct { Command string } +func (signal EventControlSignal) String() string { + return fmt.Sprintf("EventControlSignal(%s, %s)", signal.SignalHeader, signal.Command) +} + func NewEventControlSignal(command string) *EventControlSignal { return &EventControlSignal{ NewSignalHeader(Direct), @@ -76,19 +80,29 @@ func (ext *EventExt) Process(ctx *Context, node *Node, source NodeID, signal Sig return messages, changes } -type TestEventExt struct {} +type TestEventExt struct { + Length time.Duration +} var test_event_transitions = map[string]struct{ from_state string to_state string }{ - "start": { + "ready?": { "init", + "ready", + }, + "start": { + "ready", "running", }, + "abort": { + "ready", + "init", + }, "stop": { "running", - "init", + "stopped", }, "finish": { "running", @@ -114,6 +128,9 @@ func (ext *TestEventExt) Process(ctx *Context, node *Node, source NodeID, signal event_ext.State = info.to_state messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id)) 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 { messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "bad_state")) } diff --git a/event_test.go b/event_test.go index e0256f4..4fcd8cc 100644 --- a/event_test.go +++ b/event_test.go @@ -7,16 +7,16 @@ import ( ) 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) fatalErr(t, err) 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) - response, signals := testSend(t, ctx, NewEventControlSignal("start"), event, event) + response, signals := testSend(t, ctx, NewEventControlSignal("ready?"), event, event) switch resp := response.(type) { case *SuccessSignal: case *ErrorSignal: @@ -28,7 +28,7 @@ func TestEvent(t *testing.T) { var state_signal *EventStateSignal = nil for _, signal := range(signals) { 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 break } @@ -36,12 +36,12 @@ func TestEvent(t *testing.T) { if state_signal == nil { 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) } - response, signals = testSend(t, ctx, NewEventControlSignal("finish"), event, event) + response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event) switch resp := response.(type) { case *SuccessSignal: case *ErrorSignal: @@ -53,7 +53,7 @@ func TestEvent(t *testing.T) { state_signal = nil for _, signal := range(signals) { 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 break } @@ -61,11 +61,16 @@ func TestEvent(t *testing.T) { if state_signal == nil { 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) } + _, 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) switch resp := response.(type) { case *SuccessSignal: diff --git a/signal.go b/signal.go index 4442953..eee44ee 100644 --- a/signal.go +++ b/signal.go @@ -202,6 +202,11 @@ func NewStopSignal() *StopSignal { type SuccessSignal struct { ResponseHeader } + +func (signal SuccessSignal) String() string { + return fmt.Sprintf("SuccessSignal(%s)", signal.SignalHeader) +} + func (signal SuccessSignal) Permission() Tree { return Tree{ ResponseType: {