2023-10-29 18:26:14 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-11-04 18:56:35 -06:00
|
|
|
"crypto/ed25519"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"crypto/rand"
|
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"})
|
2024-03-03 15:45:45 -07:00
|
|
|
err := RegisterExtension[TestEventExt](ctx, nil)
|
2023-10-30 19:40:30 -06:00
|
|
|
fatalErr(t, err)
|
|
|
|
|
|
|
|
|
2023-11-04 18:56:35 -06:00
|
|
|
event_public, event_private, err := ed25519.GenerateKey(rand.Reader)
|
2023-10-29 18:26:14 -06:00
|
|
|
event_listener := NewListenerExt(100)
|
2024-03-08 00:22:51 -07:00
|
|
|
event, err := NewNode(ctx, event_private, "Base", 100, NewEventExt(KeyID(event_public), "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
|
|
|
}
|