-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_catch_test.go
182 lines (154 loc) · 5.37 KB
/
event_catch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2023 The bpmn Authors
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library;
*/
package bpmn_test
import (
"context"
"testing"
"github.com/olive-io/bpmn/schema"
"github.com/olive-io/bpmn/v2"
"github.com/olive-io/bpmn/v2/pkg/event"
"github.com/olive-io/bpmn/v2/pkg/tracing"
"github.com/stretchr/testify/assert"
)
func TestSignalEvent(t *testing.T) {
testEvent(t, "testdata/intermediate_catch_event.bpmn", "signalCatch", nil, false, event.NewSignalEvent("global_sig1"))
}
func TestMessageEvent(t *testing.T) {
testEvent(t, "testdata/intermediate_catch_event.bpmn", "messageCatch", nil, false, event.NewMessageEvent("msg", nil))
}
func TestMultipleEvent(t *testing.T) {
// either
testEvent(t, "testdata/intermediate_catch_event_multiple.bpmn", "multipleCatch", nil, false, event.NewMessageEvent("msg", nil))
// or
testEvent(t, "testdata/intermediate_catch_event_multiple.bpmn", "multipleCatch", nil, false, event.NewSignalEvent("global_sig1"))
}
func TestMultipleParallelEvent(t *testing.T) {
// both
testEvent(t, "testdata/intermediate_catch_event_multiple_parallel.bpmn", "multipleParallelCatch",
nil, false, event.NewMessageEvent("msg", nil), event.NewSignalEvent("global_sig1"))
// either
testEvent(t, "testdata/intermediate_catch_event_multiple_parallel.bpmn", "multipleParallelCatch", nil, true, event.NewMessageEvent("msg", nil))
testEvent(t, "testdata/intermediate_catch_event_multiple_parallel.bpmn", "multipleParallelCatch", nil, true, event.NewSignalEvent("global_sig1"))
}
type eventInstance struct {
id string
}
func (e eventInstance) EventDefinition() schema.EventDefinitionInterface {
definition := schema.DefaultEventDefinition()
return &definition
}
type eventDefinitionInstanceBuilder struct{}
func (e eventDefinitionInstanceBuilder) NewEventDefinitionInstance(def schema.EventDefinitionInterface) (event.IDefinitionInstance, error) {
switch d := def.(type) {
case *schema.TimerEventDefinition:
id, _ := d.Id()
return eventInstance{id: *id}, nil
case *schema.ConditionalEventDefinition:
id, _ := d.Id()
return eventInstance{id: *id}, nil
default:
return event.WrapEventDefinition(d), nil
}
}
func TestTimerEvent(t *testing.T) {
i := eventInstance{id: "timer_1"}
b := eventDefinitionInstanceBuilder{}
testEvent(t, "testdata/intermediate_catch_event.bpmn", "timerCatch", &b, false, event.MakeTimerEvent(i))
}
func TestConditionalEvent(t *testing.T) {
i := eventInstance{id: "conditional_1"}
b := eventDefinitionInstanceBuilder{}
testEvent(t, "testdata/intermediate_catch_event.bpmn", "conditionalCatch", &b, false, event.MakeTimerEvent(i))
}
func testEvent(t *testing.T, filename string, nodeId string, eventDefinitionInstanceBuilder event.IDefinitionInstanceBuilder, eventObservationOnly bool, events ...event.IEvent) {
var testDoc schema.Definitions
LoadTestFile(filename, &testDoc)
processElement := (*testDoc.Processes())[0]
proc := bpmn.NewProcess(&processElement, &testDoc, bpmn.WithEventDefinitionInstanceBuilder(eventDefinitionInstanceBuilder))
tracer := tracing.NewTracer(context.Background())
traces := tracer.SubscribeChannel(make(chan tracing.ITrace, 64))
if inst, err := proc.Instantiate(bpmn.WithTracer(tracer)); err == nil {
err := inst.StartAll()
if err != nil {
t.Fatalf("failed to run the instance: %s", err)
}
resultChan := make(chan bool)
go func() {
for {
trace := tracing.Unwrap(<-traces)
switch trace := trace.(type) {
case bpmn.ActiveListeningTrace:
if id, present := trace.Node.Id(); present {
if *id == nodeId {
// listening
resultChan <- true
return
}
}
case bpmn.ErrorTrace:
t.Errorf("%#v", trace)
resultChan <- false
return
default:
//t.Logf("%#v", trace)
}
}
}()
assert.True(t, <-resultChan)
go func() {
defer inst.Tracer().Unsubscribe(traces)
eventsToObserve := events
for {
trace := tracing.Unwrap(<-traces)
switch trace := trace.(type) {
case bpmn.EventObservedTrace:
if eventObservationOnly {
for i := range eventsToObserve {
if eventsToObserve[i] == trace.Event {
eventsToObserve[i] = eventsToObserve[len(eventsToObserve)-1]
eventsToObserve = eventsToObserve[:len(eventsToObserve)-1]
break
}
}
if len(eventsToObserve) == 0 {
resultChan <- true
return
}
}
case bpmn.FlowTrace:
if id, present := trace.Source.Id(); present {
if *id == nodeId {
// success!
resultChan <- true
return
}
}
case bpmn.ErrorTrace:
t.Errorf("%#v", trace)
resultChan <- false
return
default:
//t.Logf("%#v", trace)
}
}
}()
for _, evt := range events {
_, err = inst.ConsumeEvent(evt)
assert.Nil(t, err)
}
assert.True(t, <-resultChan)
} else {
t.Fatalf("failed to instantiate the process: %s", err)
}
}