2023-10-29 18:26:14 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-10-30 19:40:30 -06:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-10-29 18:26:14 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvent(t *testing.T) {
|
2023-11-02 20:58:38 -06:00
|
|
|
ctx := logTestContext(t, []string{"event", "listener", "listener_debug"})
|
2023-10-30 19:40:30 -06:00
|
|
|
err := ctx.RegisterExtension(reflect.TypeOf(&TestEventExt{}), NewExtType("TEST_EVENT"), nil)
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
|
2023-10-29 18:26:14 -06:00
|
|
|
event_listener := NewListenerExt(100)
|
2023-11-02 20:58:38 -06:00
|
|
|
event, err := NewNode(ctx, nil, BaseNodeType, 100, nil, NewEventExt(nil, "Test Event"), &TestEventExt{time.Second}, event_listener)
|
2023-10-29 18:26:14 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-11-02 20:58:38 -06:00
|
|
|
response, signals := testSend(t, ctx, NewEventControlSignal("ready?"), event, event)
|
2023-10-30 19:40:30 -06:00
|
|
|
switch resp := response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
case *ErrorSignal:
|
|
|
|
t.Fatalf("Error response %+v", resp.Error)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unexpected response %+v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var state_signal *EventStateSignal = nil
|
|
|
|
for _, signal := range(signals) {
|
|
|
|
event_state, is_event_state := signal.(*EventStateSignal)
|
2023-11-02 20:58:38 -06:00
|
|
|
if is_event_state == true && event_state.Source == event.ID && event_state.State == "ready" {
|
2023-10-30 19:40:30 -06:00
|
|
|
state_signal = event_state
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state_signal == nil {
|
|
|
|
state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool {
|
2023-11-02 20:58:38 -06:00
|
|
|
return sig.Source == event.ID && sig.State == "ready"
|
2023-10-30 19:40:30 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:58:38 -06:00
|
|
|
response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event)
|
2023-10-30 19:40:30 -06:00
|
|
|
switch resp := response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
case *ErrorSignal:
|
|
|
|
t.Fatalf("Error response %+v", resp.Error)
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unexpected response %+v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
state_signal = nil
|
|
|
|
for _, signal := range(signals) {
|
|
|
|
event_state, is_event_state := signal.(*EventStateSignal)
|
2023-11-02 20:58:38 -06:00
|
|
|
if is_event_state == true && event_state.Source == event.ID && event_state.State == "running" {
|
2023-10-30 19:40:30 -06:00
|
|
|
state_signal = event_state
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state_signal == nil {
|
|
|
|
state_signal, err = WaitForSignal(event_listener.Chan, 10*time.Millisecond, func(sig *EventStateSignal) bool {
|
2023-11-02 20:58:38 -06:00
|
|
|
return sig.Source == event.ID && sig.State == "running"
|
2023-10-30 19:40:30 -06:00
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
}
|
|
|
|
|
2023-11-02 20:58:38 -06:00
|
|
|
_, err = WaitForSignal(event_listener.Chan, time.Second * 2, func(sig *EventStateSignal) bool {
|
|
|
|
return sig.Source == event.ID && sig.State == "done"
|
|
|
|
})
|
|
|
|
fatalErr(t, err)
|
|
|
|
|
2023-10-30 19:40:30 -06:00
|
|
|
response, signals = testSend(t, ctx, NewEventControlSignal("start"), event, event)
|
|
|
|
switch resp := response.(type) {
|
|
|
|
case *SuccessSignal:
|
|
|
|
t.Fatalf("Success response starting finished TestEventExt")
|
|
|
|
case *ErrorSignal:
|
|
|
|
default:
|
|
|
|
t.Fatalf("Unexpected response %+v", resp)
|
|
|
|
}
|
2023-10-29 18:26:14 -06:00
|
|
|
}
|