github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/automation/trigger/runtime_test.go (about) 1 package trigger_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/qri-io/qri/automation/spec" 8 "github.com/qri-io/qri/automation/trigger" 9 "github.com/qri-io/qri/automation/workflow" 10 "github.com/qri-io/qri/event" 11 "github.com/qri-io/qri/profile" 12 ) 13 14 func TestRuntimeTrigger(t *testing.T) { 15 rt := trigger.NewEmptyRuntimeTrigger() 16 adv := rt.ToMap() 17 adv["advanceCount"] = 1 18 spec.AssertTrigger(t, rt, adv) 19 } 20 21 func TestRuntimeListener(t *testing.T) { 22 wf := &workflow.Workflow{ 23 ID: workflow.ID("test workflow id"), 24 OwnerID: "test Owner id", 25 Active: true, 26 } 27 listenerConstructor := func(ctx context.Context, bus event.Bus) (trigger.Listener, func(), func()) { 28 rl := trigger.NewRuntimeListener(ctx, bus) 29 triggerOpts := map[string]interface{}{ 30 "active": true, 31 "type": trigger.RuntimeType, 32 } 33 34 trig, err := rl.ConstructTrigger(triggerOpts) 35 if err != nil { 36 t.Fatalf("RuntimeListener.ConstructTrigger unexpected error: %s", err) 37 } 38 rt, ok := trig.(*trigger.RuntimeTrigger) 39 if !ok { 40 t.Fatal("RuntimeListener.ConstructTrigger did not return a RuntimeTrigger") 41 } 42 activateTrigger := func() { 43 if rl.TriggerCh == nil { 44 return 45 } 46 wtp := event.WorkflowTriggerEvent{ 47 OwnerID: wf.OwnerID, 48 WorkflowID: wf.ID.String(), 49 TriggerID: rt.ID(), 50 } 51 rl.TriggerCh <- wtp 52 } 53 advanceTrigger := func() {} 54 55 wf.Triggers = []map[string]interface{}{rt.ToMap()} 56 if err := rl.Listen(wf); err != nil { 57 t.Fatalf("RuntimeListener.Listen unexpected error: %s", err) 58 } 59 return rl, activateTrigger, advanceTrigger 60 } 61 spec.AssertListener(t, listenerConstructor) 62 63 ctx := context.Background() 64 l, _, _ := listenerConstructor(ctx, event.NilBus) 65 rl, ok := l.(*trigger.RuntimeListener) 66 if !ok { 67 t.Fatal("RuntimeListener unexpected assertion error, listenerConstructor should return a runtimeListener") 68 } 69 wf.Triggers = []map[string]interface{}{} 70 if err := rl.Listen(wf); err != nil { 71 t.Fatalf("RuntimeListener.Listen unexpected error: %s", err) 72 } 73 if rl.TriggersExists(wf) { 74 t.Errorf("RuntimeListener.Listen error: should remove triggers from its internal store when given an updated workflow with a no longer active trigger") 75 } 76 } 77 78 func TestRuntimeListenerListen(t *testing.T) { 79 ctx := context.Background() 80 bus := event.NewBus(ctx) 81 rl := trigger.NewRuntimeListener(ctx, bus) 82 83 aID := profile.ID("a") 84 wfA1 := &workflow.Workflow{ 85 OwnerID: aID, 86 ID: workflow.ID("workflow 1"), 87 Active: true, 88 } 89 bID := profile.ID("b") 90 wfB1 := &workflow.Workflow{ 91 OwnerID: bID, 92 ID: workflow.ID("workflow 1"), 93 Active: true, 94 } 95 if err := rl.Listen([]trigger.Source{wfA1, wfB1}...); err != nil { 96 t.Fatal(err) 97 } 98 if rl.TriggersExists(wfA1) || rl.TriggersExists(wfB1) { 99 t.Fatal("workflow with no triggers should not exist in the Listener") 100 } 101 trig1 := trigger.NewEmptyRuntimeTrigger() 102 trig2 := trigger.NewEmptyRuntimeTrigger() 103 wfA1.Triggers = []map[string]interface{}{trig1.ToMap(), trig2.ToMap()} 104 if err := rl.Listen([]trigger.Source{wfA1}...); err != nil { 105 t.Fatal(err) 106 } 107 if rl.TriggersExists(wfA1) { 108 t.Fatal("workflow with no active triggers should not exist in the Listener") 109 } 110 trig1.SetActive(true) 111 wfA1.Triggers = []map[string]interface{}{trig1.ToMap(), trig2.ToMap()} 112 if err := rl.Listen([]trigger.Source{wfA1}...); err != nil { 113 t.Fatal(err) 114 } 115 if !rl.TriggersExists(wfA1) { 116 t.Fatal("workflow with an active trigger should exist in the listener") 117 } 118 trig2.SetActive(true) 119 wfA1.Triggers = []map[string]interface{}{trig1.ToMap(), trig2.ToMap()} 120 121 if rl.TriggersExists(wfA1) { 122 t.Fatal("workflow with non matching trigger list should not exist in the listener") 123 } 124 125 if err := rl.Listen([]trigger.Source{wfA1}...); err != nil { 126 t.Fatal(err) 127 } 128 if !rl.TriggersExists(wfA1) { 129 t.Fatal("Listen did not update triggers for wfA1") 130 } 131 132 wfA2 := &workflow.Workflow{ 133 OwnerID: aID, 134 ID: workflow.ID("workflow 2"), 135 Triggers: []map[string]interface{}{trig1.ToMap(), trig2.ToMap()}, 136 Active: true, 137 } 138 139 wfB1.Triggers = []map[string]interface{}{trig1.ToMap()} 140 if err := rl.Listen([]trigger.Source{wfB1, wfA2}...); err != nil { 141 t.Fatal(err) 142 } 143 if !rl.TriggersExists(wfA2) { 144 t.Fatal("Listen did not add wfA2") 145 } 146 if !rl.TriggersExists(wfA1) { 147 t.Fatal("Listen erroneously removed wfA1") 148 } 149 if !rl.TriggersExists(wfB1) { 150 t.Fatal("Listen did not add wfB1") 151 } 152 wfA1.Triggers = []map[string]interface{}{} 153 if err := rl.Listen([]trigger.Source{wfA1}...); err != nil { 154 t.Fatal(err) 155 } 156 if rl.TriggersExists(wfA1) { 157 t.Fatal("Listen did not remove wfA1 when wfA1 had no triggers") 158 } 159 }