-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrchestrationHistoryTest.php
More file actions
248 lines (219 loc) · 9.56 KB
/
Copy pathOrchestrationHistoryTest.php
File metadata and controls
248 lines (219 loc) · 9.56 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/*
* Copyright ©2024 Robert Landers
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Bottledcode\DurablePhp\Tests\Unit;
use Bottledcode\DurablePhp\Events\RaiseEvent;
use Bottledcode\DurablePhp\Events\StartExecution;
use Bottledcode\DurablePhp\Events\TaskCompleted;
use Bottledcode\DurablePhp\Events\TaskFailed;
use Bottledcode\DurablePhp\Events\WithOrchestration;
use Bottledcode\DurablePhp\OrchestrationContext;
use Bottledcode\DurablePhp\OrchestrationContextInterface;
use Bottledcode\DurablePhp\SerializedArray;
use Bottledcode\DurablePhp\State\Attributes\EntryPoint;
use Bottledcode\DurablePhp\State\RuntimeStatus;
use Bottledcode\DurablePhp\State\Serializer;
use Bottledcode\DurablePhp\Testing\ActivityMock;
use Bottledcode\DurablePhp\Testing\DummyOrchestrationContext;
use Exception;
use function Bottledcode\DurablePhp\OrchestrationInstance;
it('can be started', function (): void {
$instance = getOrchestration('test', fn() => true, [], $nextEvent);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Completed);
});
class SerializedType
{
public function __construct(private string $data) {}
}
it('real: can handle oop orchestration', function (): void {
$orchestration = new class (null) {
public function __construct(private ?OrchestrationContextInterface $orchestrationContext) {}
#[EntryPoint]
public function entry(string $test, SerializedType $type): string
{
return $test;
}
};
$instance = getOrchestration(id: 'test', orchestration: $orchestration, input: [
'test' => 'hello world',
'type' => Serializer::serialize(new SerializedType('test')),
], nextEvent: $nextEvent);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Completed);
});
it('example: can handle oop orchestration', function (): void {
$context = new DummyOrchestrationContext(null, []);
$orchestration = new class ($context) {
public function __construct(private ?OrchestrationContextInterface $orchestrationContext) {}
#[EntryPoint]
public function entry(string $test, SerializedType $type): string
{
return $test;
}
};
$result = $orchestration->entry('test', new SerializedType('hello world'));
expect($result)->toBe('test');
});
it('returns a result to the parent', function (): void {
$instance = getOrchestration(
'test',
fn() => true,
[],
$nextEvent,
StartExecution::asChild(OrchestrationInstance('parent', 'parent'), [], []),
);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
expect($result)
->toHaveCount(1)->and($instance)->toHaveStatus(RuntimeStatus::Completed);
});
it('properly delays when using timers', function (): void {
$instance = getOrchestration('test', function (OrchestrationContextInterface $context) {
$start = $context->getCurrentTime();
$interval = $context->createInterval(hours: 1);
$timeout = $context->createTimer($start->add($interval));
$context->waitOne($timeout);
return true;
}, [], $nextEvent);
$timer = processEvent($nextEvent, $instance->applyStartOrchestration(...));
expect($timer)->toHaveCount(1);
$instance->resetState();
$result = processEvent($timer[0], $instance->applyRaiseEvent(...));
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Completed)->and(getStatusOutput($instance))
->toBeTrue();
});
it('properly delays when using timers (example)', function (): void {
$instance = function (OrchestrationContextInterface $context) {
$start = $context->getCurrentTime();
$interval = $context->createInterval(hours: 1);
$timeout = $context->createTimer($start->add($interval));
$context->waitOne($timeout);
return true;
};
$context = new DummyOrchestrationContext($instance, []);
$result = $instance($context);
expect($result)->toBeTrue();
});
it('can wait for a signal after starting', function (): void {
$instance = getOrchestration('test', function (OrchestrationContextInterface $context) {
$waiter = [];
for ($i = 0; $i < 3; $i++) {
$waiter[] = $context->waitForExternalEvent('test');
}
$context->waitAll(...$waiter);
return true;
}, [], $nextEvent);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
$instance->resetState();
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Running);
$result = processEvent(
WithOrchestration::forInstance(
$instance->id,
new RaiseEvent('', 'test', SerializedArray::fromArray([])->toArray()),
),
$instance->applyRaiseEvent(...),
);
$instance->resetState();
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Running);
$result = processEvent(
WithOrchestration::forInstance($instance->id, new RaiseEvent('', 'test', [])),
$instance->applyRaiseEvent(...),
);
$instance->resetState();
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Running);
$result = processEvent(
WithOrchestration::forInstance($instance->id, new RaiseEvent('', 'test', [])),
$instance->applyRaiseEvent(...),
);
$instance->resetState();
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Completed)->and(getStatusOutput($instance))
->toBeTrue();
});
it('can wait for a signal after starting (example)', function (): void {
$instance = function (OrchestrationContextInterface $context) {
$waiter = [];
for ($i = 0; $i < 3; $i++) {
$waiter[] = $context->waitForExternalEvent('test');
}
$context->waitAll(...$waiter);
return true;
};
$context = new DummyOrchestrationContext($instance, []);
$context->handleEvent('test', '');
$result = $instance($context);
expect($result)->toBeTrue();
});
it('can call an activity with a successful result', function (): void {
$instance = getOrchestration(
'test',
fn(OrchestrationContext $context) => $context->waitOne($context->callActivity('test', null, null, args: 'hello world')),
[],
$nextEvent,
);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
$instance->resetState();
expect($result)->toHaveCount(1);
$result = processEvent(
WithOrchestration::forInstance($instance->id, TaskCompleted::forId($result[0]->eventId, 'pretty colors')),
$instance->applyTaskCompleted(...),
);
expect($result)
->toBeEmpty()->and($instance)->toHaveOutput('pretty colors')->and($instance)->toHaveStatus(
RuntimeStatus::Completed,
);
});
it('can call an activity with a successful result (example)', function (): void {
$instance = fn(OrchestrationContextInterface $context) => $context->waitOne($context->callActivity('test', args: 'hello world'));
$context = new DummyOrchestrationContext($instance, []);
$context->handleActivities(new ActivityMock('test', 'pretty colors'));
expect($instance($context))->toBe(['pretty colors']);
});
it('can call an activity with a failed result (example)', function (): void {
$instance = fn(OrchestrationContextInterface $context) => $context->waitOne($context->callActivity('test', args: 'hello world'));
$context = new DummyOrchestrationContext($instance, []);
$context->handleActivities(new ActivityMock('test', new Exception('hello world')));
expect(fn() => $instance($context))->toThrow(Exception::class, 'hello world');
});
it('can call an activity with a failed result', function (): void {
$instance = getOrchestration(
'test',
fn(OrchestrationContext $context) => $context->waitOne($context->callActivity('test', args: 'hello world')),
[],
$nextEvent,
);
$result = processEvent($nextEvent, $instance->applyStartOrchestration(...));
$instance->resetState();
expect($result)->toHaveCount(1);
$result = processEvent(
WithOrchestration::forInstance($instance->id, TaskFailed::forTask($result[0]->eventId, 'pretty colors')),
$instance->applyTaskFailed(...),
);
expect($result)
->toBeEmpty()->and($instance)->toHaveStatus(RuntimeStatus::Failed);
});