2023-06-18 18:33:17 -06:00
|
|
|
package graphvent
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-05-29 23:54:52 -06:00
|
|
|
"fmt"
|
2023-06-03 18:56:14 -06:00
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
2023-04-08 15:23:40 -06:00
|
|
|
)
|
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
type GraphTester testing.T
|
2023-06-02 17:31:29 -06:00
|
|
|
const listner_timeout = 50 * time.Millisecond
|
2023-04-08 15:23:40 -06:00
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
func (t * GraphTester) WaitForValue(listener chan GraphSignal, signal_type string, source GraphNode, timeout time.Duration, str string) GraphSignal {
|
2023-06-03 02:07:16 -06:00
|
|
|
timeout_channel := time.After(timeout)
|
|
|
|
for true {
|
|
|
|
select {
|
|
|
|
case signal := <- listener:
|
|
|
|
if signal.Type() == signal_type {
|
2023-06-18 18:11:59 -06:00
|
|
|
fmt.Printf("SIGNAL_TYPE_FOUND: %s - %s %+v\n", signal.Type(), signal.Source(), listener)
|
|
|
|
if signal.Source() == source.ID() {
|
|
|
|
return signal
|
2023-06-03 18:56:14 -06:00
|
|
|
}
|
2023-06-03 02:07:16 -06:00
|
|
|
}
|
|
|
|
case <-timeout_channel:
|
2023-06-03 18:56:14 -06:00
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
2023-06-03 02:07:16 -06:00
|
|
|
t.Fatal(str)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
func (t * GraphTester) CheckForValue(listener chan GraphSignal, str string) GraphSignal {
|
2023-04-08 15:23:40 -06:00
|
|
|
timeout := time.After(listner_timeout)
|
|
|
|
select {
|
2023-06-03 02:07:16 -06:00
|
|
|
case signal := <- listener:
|
|
|
|
return signal
|
2023-04-08 15:23:40 -06:00
|
|
|
case <-timeout:
|
2023-06-03 18:56:14 -06:00
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
2023-06-01 13:48:38 -06:00
|
|
|
t.Fatal(str)
|
2023-06-03 02:07:16 -06:00
|
|
|
return nil
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
func (t * GraphTester) CheckForNone(listener chan GraphSignal, str string) {
|
2023-04-08 15:23:40 -06:00
|
|
|
timeout := time.After(listner_timeout)
|
|
|
|
select {
|
2023-06-03 01:38:35 -06:00
|
|
|
case sig := <- listener:
|
2023-06-03 18:56:14 -06:00
|
|
|
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
|
|
t.Fatal(fmt.Sprintf("%s : %+v", str, sig))
|
2023-06-03 01:38:35 -06:00
|
|
|
case <-timeout:
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
func TestNewEventWithResource(t *testing.T) {
|
2023-04-08 15:23:40 -06:00
|
|
|
name := "Test Resource"
|
|
|
|
description := "A resource for testing"
|
|
|
|
children := []Resource{}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
test_resource, _ := NewResource(name, description, children)
|
|
|
|
root_event, err := NewEvent("root_event", "", []Resource{test_resource})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-04-08 15:23:40 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
res := FindRequiredResource(root_event, test_resource.ID())
|
2023-04-08 15:23:40 -06:00
|
|
|
if res == nil {
|
|
|
|
t.Fatal("Failed to find Resource in EventManager after adding")
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Name() != name || res.Description() != description {
|
|
|
|
t.Fatal("Name/description of returned resource did not match added resource")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDoubleResourceAdd(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
test_resource, _ := NewResource("", "", []Resource{})
|
|
|
|
_, err := NewEvent("", "", []Resource{test_resource, test_resource})
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
if err == nil {
|
2023-06-20 15:48:17 -06:00
|
|
|
t.Fatal("NewEvent didn't return an error")
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 16:51:34 -06:00
|
|
|
func TestTieredResource(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
r1, _ := NewResource("r1", "", []Resource{})
|
|
|
|
r2, err := NewResource("r2", "", []Resource{r1})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = NewEvent("", "", []Resource{r2})
|
2023-04-08 15:23:40 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to create event with tiered resources")
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceUpdate(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
r1, err := NewResource("r1", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r2, err := NewResource("r2", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r3, err := NewResource("r3", "", []Resource{r1, r2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r4, err := NewResource("r4", "", []Resource{r3})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewEvent("", "", []Resource{r3, r4})
|
|
|
|
if err != nil {
|
2023-04-08 15:23:40 -06:00
|
|
|
t.Fatal("Failed to add initial tiered resources for test")
|
|
|
|
}
|
|
|
|
|
|
|
|
r1_l := r1.UpdateChannel()
|
|
|
|
r2_l := r2.UpdateChannel()
|
|
|
|
r3_l := r3.UpdateChannel()
|
2023-04-08 15:26:04 -06:00
|
|
|
r4_l := r4.UpdateChannel()
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
// Calling Update() on the parent with no other parents should only notify node listeners
|
2023-06-02 17:31:29 -06:00
|
|
|
SendUpdate(r3, NewSignal(nil, "test"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).CheckForNone(r1_l, "Update on r1 after updating r3")
|
|
|
|
(*GraphTester)(t).CheckForNone(r2_l, "Update on r2 after updating r3")
|
|
|
|
(*GraphTester)(t).CheckForValue(r3_l, "No update on r3 after updating r3")
|
|
|
|
(*GraphTester)(t).CheckForValue(r4_l, "No update on r4 after updating r3")
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
// Calling Update() on a child should notify listeners of the parent and child, but not siblings
|
2023-06-02 17:31:29 -06:00
|
|
|
SendUpdate(r2, NewSignal(nil, "test"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).CheckForNone(r1_l, "Update on r1 after updating r2")
|
|
|
|
(*GraphTester)(t).CheckForValue(r2_l, "No update on r2 after updating r2")
|
|
|
|
(*GraphTester)(t).CheckForValue(r3_l, "No update on r3 after updating r2")
|
|
|
|
(*GraphTester)(t).CheckForValue(r4_l, "No update on r4 after updating r2")
|
2023-04-08 15:23:40 -06:00
|
|
|
|
|
|
|
// Calling Update() on a child should notify listeners of the parent and child, but not siblings
|
2023-06-02 17:31:29 -06:00
|
|
|
SendUpdate(r1, NewSignal(nil, "test"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).CheckForValue(r1_l, "No update on r1 after updating r1")
|
|
|
|
(*GraphTester)(t).CheckForNone(r2_l, "Update on r2 after updating r1")
|
|
|
|
(*GraphTester)(t).CheckForValue(r3_l, "No update on r3 after updating r1")
|
|
|
|
(*GraphTester)(t).CheckForValue(r4_l, "No update on r4 after updating r1")
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
|
|
|
|
2023-04-08 16:51:34 -06:00
|
|
|
func TestAddEvent(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
r1, _ := NewResource("r1", "", []Resource{})
|
|
|
|
r2, _ := NewResource("r2", "", []Resource{r1})
|
|
|
|
root_event, _ := NewEvent("", "", []Resource{r2})
|
2023-04-08 16:59:36 -06:00
|
|
|
|
|
|
|
name := "Test Event"
|
|
|
|
description := "A test event"
|
|
|
|
resources := []Resource{r2}
|
2023-06-20 15:48:17 -06:00
|
|
|
new_event, _ := NewEvent(name, description, resources)
|
2023-04-08 16:59:36 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
err := LinkEvent(root_event, new_event, nil)
|
2023-04-08 16:59:36 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add new_event to root_event: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
res := FindChild(root_event, new_event.ID())
|
2023-04-08 16:59:36 -06:00
|
|
|
if res == nil {
|
|
|
|
t.Fatalf("Failed to find new_event in event_manager: %s", err)
|
|
|
|
}
|
2023-04-08 16:51:34 -06:00
|
|
|
|
2023-04-08 16:59:36 -06:00
|
|
|
if res.Name() != name || res.Description() != description {
|
|
|
|
t.Fatal("Event found in event_manager didn't match added")
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
res_required := res.Resources()
|
2023-04-08 16:59:36 -06:00
|
|
|
if len(res_required) < 1 {
|
|
|
|
t.Fatal("Event found in event_manager didn't match added")
|
|
|
|
} else if res_required[0].ID() != r2.ID() {
|
|
|
|
t.Fatal("Event found in event_manager didn't match added")
|
|
|
|
}
|
2023-04-08 16:51:34 -06:00
|
|
|
}
|
|
|
|
|
2023-04-08 15:23:40 -06:00
|
|
|
func TestLockResource(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
r1, err := NewResource("r1", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r2, err := NewResource("r2", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r3, err := NewResource("r3", "", []Resource{r1, r2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
r4, err := NewResource("r4", "", []Resource{r1, r2})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
root_event, err := NewEvent("", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
test_event, err := NewEvent("", "", []Resource{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-04-08 15:51:42 -06:00
|
|
|
r1_l := r1.UpdateChannel()
|
2023-04-08 16:51:34 -06:00
|
|
|
rel := root_event.UpdateChannel()
|
2023-04-08 15:51:42 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
err = LockResource(r3, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to lock r3")
|
|
|
|
}
|
2023-06-18 18:11:59 -06:00
|
|
|
SendUpdate(r3, NewDownSignal(r3, "locked"))
|
2023-06-01 13:48:38 -06:00
|
|
|
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(r1_l, "locked", r3, time.Second, "Wasn't notified of r1 lock on r1 after r3 lock")
|
|
|
|
(*GraphTester)(t).WaitForValue(rel, "locked", r3, time.Second, "Wasn't notified of r1 lock on rel after r3 lock")
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = LockResource(r3, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Locked r3 after locking r3")
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = LockResource(r4, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Locked r4 after locking r3")
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = LockResource(r1, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Locked r1 after locking r3")
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = UnlockResource(r3, test_event)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unlocked r3 with event that didn't lock it")
|
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = UnlockResource(r3, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to unlock r3")
|
|
|
|
}
|
2023-06-18 18:11:59 -06:00
|
|
|
SendUpdate(r3, NewDownSignal(r3, "unlocked"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(r1_l, "unlocked", r3, time.Second * 2, "Wasn't notified of r1 unlock on r1 after r3 unlock")
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = LockResource(r4, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to lock r4 after unlocking r3")
|
|
|
|
}
|
2023-06-18 18:11:59 -06:00
|
|
|
SendUpdate(r4, NewDownSignal(r4, "locked"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(r1_l, "locked", r4, time.Second * 2, "Wasn't notified of r1 lock on r1 after r4 lock")
|
|
|
|
(*GraphTester)(t).WaitForValue(rel, "locked", r4, time.Second * 2, "Wasn't notified of r1 lock on r1 after r4 lock")
|
2023-04-08 15:47:51 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
err = UnlockResource(r4, root_event)
|
2023-04-08 15:47:51 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Failed to unlock r4")
|
|
|
|
}
|
2023-06-18 18:11:59 -06:00
|
|
|
SendUpdate(r4, NewDownSignal(r4, "unlocked"))
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(r1_l, "unlocked", r4, time.Second * 2, "Wasn't notified of r1 unlock on r1 after r4 lock")
|
2023-04-08 15:23:40 -06:00
|
|
|
}
|
2023-05-08 21:42:33 -06:00
|
|
|
|
|
|
|
func TestAddToEventQueue(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
queue, _ := NewEventQueue("q", "", []Resource{})
|
|
|
|
event_1, _ := NewEvent("1", "", []Resource{})
|
|
|
|
event_2, _ := NewEvent("2", "", []Resource{})
|
2023-05-08 21:42:33 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
err := LinkEvent(queue, event_1, nil)
|
2023-05-08 21:42:33 -06:00
|
|
|
if err == nil {
|
2023-05-29 19:17:52 -06:00
|
|
|
t.Fatal("suceeded in added nil info to queue")
|
2023-05-08 21:42:33 -06:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
err = LinkEvent(queue, event_1, &EventQueueInfo{priority: 0})
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to add valid event + info to queue")
|
2023-05-08 21:42:33 -06:00
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
err = LinkEvent(queue, event_2, &EventQueueInfo{priority: 1})
|
2023-05-08 21:42:33 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to add valid event + info to queue")
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 19:17:52 -06:00
|
|
|
|
|
|
|
func TestStartBaseEvent(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
event_1, _ := NewEvent("TestStartBaseEvent event_1", "", []Resource{})
|
2023-05-29 23:54:52 -06:00
|
|
|
r := event_1.DoneResource()
|
2023-05-29 19:17:52 -06:00
|
|
|
|
|
|
|
e_l := event_1.UpdateChannel()
|
|
|
|
r_l := r.UpdateChannel()
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).CheckForNone(e_l, "Update on event_1 before starting")
|
|
|
|
(*GraphTester)(t).CheckForNone(r_l, "Update on r_1 before starting")
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
if r.Owner().ID() != event_1.ID() {
|
2023-05-29 19:17:52 -06:00
|
|
|
t.Fatal("r is not owned by event_1")
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:48:48 -06:00
|
|
|
err := LockResources(event_1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RunEvent(event_1)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Check that the update channels for the event and resource have updates
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(e_l, "event_start", event_1, 1*time.Second, "No event_start on e_l")
|
|
|
|
(*GraphTester)(t).WaitForValue(e_l, "event_done", event_1, 1*time.Second, "No event_start on e_l")
|
|
|
|
(*GraphTester)(t).WaitForValue(r_l, "unlocked", event_1, 1*time.Second, "No unlocked on r_l")
|
2023-05-29 19:17:52 -06:00
|
|
|
|
|
|
|
if r.Owner() != nil {
|
|
|
|
t.Fatal("r still owned after event completed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 23:54:52 -06:00
|
|
|
func TestAbortEventQueue(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
r1, _ := NewResource("r1", "", []Resource{})
|
|
|
|
root_event, _ := NewEventQueue("root_event", "", []Resource{})
|
2023-05-29 23:54:52 -06:00
|
|
|
r := root_event.DoneResource()
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
LockResource(r1, root_event)
|
2023-06-20 15:48:17 -06:00
|
|
|
|
|
|
|
e1, _ := NewEvent("event_1", "", []Resource{r1})
|
2023-05-29 23:54:52 -06:00
|
|
|
e1_info := NewEventQueueInfo(1)
|
|
|
|
// Add an event so that the queue doesn't auto complete
|
2023-06-20 15:48:17 -06:00
|
|
|
err := LinkEvent(root_event, e1, e1_info)
|
2023-05-29 23:54:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
// Now that the event is constructed with a queue and 3 basic events
|
2023-05-29 23:54:52 -06:00
|
|
|
// start the queue and check that all the events are executed
|
|
|
|
go func() {
|
2023-06-02 17:39:28 -06:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2023-06-18 18:11:59 -06:00
|
|
|
abort_signal := NewDownSignal(nil, "abort")
|
2023-06-06 23:04:49 -06:00
|
|
|
SendUpdate(root_event, abort_signal)
|
2023-05-29 23:54:52 -06:00
|
|
|
}()
|
|
|
|
|
2023-06-20 20:48:48 -06:00
|
|
|
err = LockResources(root_event)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = RunEvent(root_event)
|
2023-05-29 23:54:52 -06:00
|
|
|
if err == nil {
|
2023-06-20 15:48:17 -06:00
|
|
|
t.Fatal("root_event completed without error")
|
2023-05-29 23:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Owner() == nil {
|
|
|
|
t.Fatal("root event was finished after starting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:48:48 -06:00
|
|
|
func TestStartWithoutLocking(t * testing.T) {
|
|
|
|
test_resource, _ := NewResource("test_resource", "", []Resource{})
|
|
|
|
root_event, _ := NewEvent("root_event", "", []Resource{test_resource})
|
|
|
|
|
|
|
|
err := RunEvent(root_event)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Event ran without error without locking resources")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 19:17:52 -06:00
|
|
|
func TestStartEventQueue(t * testing.T) {
|
2023-06-20 15:48:17 -06:00
|
|
|
root_event, _ := NewEventQueue("root_event", "", []Resource{})
|
2023-05-29 23:54:52 -06:00
|
|
|
r := root_event.DoneResource()
|
2023-05-29 19:17:52 -06:00
|
|
|
rel := root_event.UpdateChannel();
|
2023-06-20 15:48:17 -06:00
|
|
|
res_1, _ := NewResource("test_resource_1", "", []Resource{})
|
|
|
|
res_2, _ := NewResource("test_resource_2", "", []Resource{})
|
2023-05-30 21:50:59 -06:00
|
|
|
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
e1, _ := NewEvent("e1", "", []Resource{res_1, res_2})
|
2023-06-07 00:36:40 -06:00
|
|
|
e1_l := e1.UpdateChannel()
|
2023-05-29 23:54:52 -06:00
|
|
|
e1_r := e1.DoneResource()
|
2023-05-29 19:17:52 -06:00
|
|
|
e1_info := NewEventQueueInfo(1)
|
2023-06-20 15:48:17 -06:00
|
|
|
err := LinkEvent(root_event, e1, e1_info)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
2023-06-20 15:48:17 -06:00
|
|
|
t.Fatal("Failed to add e1 to root_event")
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(rel, "child_added", root_event, time.Second, "No update on root_event after adding e1")
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
e2, _ := NewEvent("e2", "", []Resource{res_1})
|
2023-06-07 00:36:40 -06:00
|
|
|
e2_l := e2.UpdateChannel()
|
2023-05-29 23:54:52 -06:00
|
|
|
e2_r := e2.DoneResource()
|
2023-05-29 19:17:52 -06:00
|
|
|
e2_info := NewEventQueueInfo(2)
|
2023-06-20 15:48:17 -06:00
|
|
|
err = LinkEvent(root_event, e2, e2_info)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
2023-06-20 15:48:17 -06:00
|
|
|
t.Fatal("Failed to add e2 to root_event")
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(rel, "child_added", root_event, time.Second, "No update on root_event after adding e2")
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
e3, _ := NewEvent("e3", "", []Resource{res_2})
|
2023-06-07 00:36:40 -06:00
|
|
|
e3_l := e3.UpdateChannel()
|
2023-05-29 23:54:52 -06:00
|
|
|
e3_r := e3.DoneResource()
|
2023-05-29 19:17:52 -06:00
|
|
|
e3_info := NewEventQueueInfo(3)
|
2023-06-20 15:48:17 -06:00
|
|
|
err = LinkEvent(root_event, e3, e3_info)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
2023-06-20 15:48:17 -06:00
|
|
|
t.Fatal("Failed to add e3 to root_event")
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(rel, "child_added", root_event, time.Second, "No update on root_event after adding e3")
|
2023-05-29 19:17:52 -06:00
|
|
|
|
2023-05-29 23:54:52 -06:00
|
|
|
// Abort the event after 5 seconds just in case
|
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second)
|
2023-06-02 17:31:29 -06:00
|
|
|
if r.Owner() != nil {
|
2023-06-18 18:11:59 -06:00
|
|
|
abort_signal := NewDownSignal(nil, "abort")
|
2023-06-06 23:04:49 -06:00
|
|
|
SendUpdate(root_event, abort_signal)
|
2023-06-02 17:31:29 -06:00
|
|
|
}
|
2023-05-29 23:54:52 -06:00
|
|
|
}()
|
|
|
|
|
2023-06-20 15:48:17 -06:00
|
|
|
// Now that a root_event is constructed with a queue and 3 basic events
|
2023-05-29 19:17:52 -06:00
|
|
|
// start the queue and check that all the events are executed
|
2023-06-06 23:04:49 -06:00
|
|
|
go func() {
|
2023-06-18 18:39:52 -06:00
|
|
|
(*GraphTester)(t).WaitForValue(e1_l, "event_done", e1, time.Second, "No event_done for e3")
|
|
|
|
(*GraphTester)(t).WaitForValue(e2_l, "event_done", e2, time.Second, "No event_done for e3")
|
|
|
|
(*GraphTester)(t).WaitForValue(e3_l, "event_done", e3, time.Second, "No event_done for e3")
|
2023-06-18 18:11:59 -06:00
|
|
|
signal := NewDownSignal(nil, "cancel")
|
2023-06-06 23:04:49 -06:00
|
|
|
SendUpdate(root_event, signal)
|
|
|
|
}()
|
|
|
|
|
2023-06-20 20:48:48 -06:00
|
|
|
err = LockResources(root_event)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = RunEvent(root_event)
|
2023-05-29 19:17:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Owner() != nil {
|
2023-05-29 23:54:52 -06:00
|
|
|
fmt.Printf("root_event.DoneResource(): %p", root_event.DoneResource())
|
2023-05-29 19:17:52 -06:00
|
|
|
t.Fatal("root event was not finished after starting")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e1_r.Owner() != nil {
|
|
|
|
t.Fatal("e1 was not completed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e2_r.Owner() != nil {
|
2023-06-03 18:56:14 -06:00
|
|
|
t.Fatal(fmt.Sprintf("e2 was not completed"))
|
2023-05-29 19:17:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if e3_r.Owner() != nil {
|
|
|
|
t.Fatal("e3 was not completed")
|
|
|
|
}
|
|
|
|
}
|