forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeoutsTest.php
More file actions
131 lines (111 loc) · 3.79 KB
/
TimeoutsTest.php
File metadata and controls
131 lines (111 loc) · 3.79 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
<?php
namespace Tests\Feature\Docker;
use Illuminate\Support\Facades\Event;
use ProcessMaker\Events\ScriptResponseEvent;
use ProcessMaker\Exception\ScriptTimeoutException;
use ProcessMaker\Models\Script;
use ProcessMaker\Models\ScriptExecutor;
use Tests\Feature\Shared\BenchmarkHelper;
use Tests\Feature\Shared\LoggingHelper;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class TimeoutsTest extends TestCase
{
use BenchmarkHelper, LoggingHelper, RequestHelper;
// How long our test scripts should be allowed to run before timing out
const TIMEOUT_LENGTH = 3;
// How long to sleep our test scripts that should exceed timeout
const SLEEP_EXCEED = 6;
// How long to sleep our test scripts that should not exceed timeout
const SLEEP_NOT_EXCEED = 1;
/**
* Make sure we have a personal access client set up
*/
public function setUpWithPersonalAccessClient()
{
$this->withPersonalAccessClient();
}
/**
* Run a test script and assert that the specified timeout is exceeded
*/
private function assertTimeoutExceeded($data)
{
Event::fake([
ScriptResponseEvent::class,
]);
$this->assertLogIsEmpty();
$url = route(
'api.scripts.preview',
$this->getScript($data['language'], $data['timeout'])->id
);
$this->benchmarkStart();
$response = $this->apiCall('POST', $url, ['code' => '', 'data' => '']);
$this->benchmarkEnd();
$this->assertLogMessageExists('Script timed out');
$this->assertLessThan(intval($data['timeout']) + 2, $this->benchmark());
// Assertion: An exception is notified to usr through broadcast channel
Event::assertDispatched(ScriptResponseEvent::class, function ($event) {
$response = $event->response;
return $response['exception'] === ScriptTimeoutException::class;
});
}
/**
* Run a test script and assert that the specified timeout is not exceeded
*/
private function assertTimeoutNotExceeded($data)
{
Event::fake([
ScriptResponseEvent::class,
]);
$this->benchmarkStart();
$url = route(
'api.scripts.preview',
$this->getScript($data['language'], $data['timeout'])->id
);
$response = $this->apiCall('POST', $url, ['code' => '', 'data' => '']);
$this->benchmarkEnd();
$this->assertLessThan(intval($data['timeout']) + 2, $this->benchmark());
$response->assertStatus(200);
// Assertion: The script output is sent to usr through broadcast channel
Event::assertDispatched(ScriptResponseEvent::class, function ($event) {
$response = $event->response;
return !array_key_exists('exception', $response);
});
}
/**
* Test to ensure PHP scripts timeout
*/
public function testPhpScriptTimeoutExceeded()
{
config(['simulate_timeout' => true]);
$this->assertTimeoutExceeded([
'language' => 'php',
'timeout' => self::TIMEOUT_LENGTH,
]);
}
/**
* Test to ensure PHP scripts do not timeout if they do not exceed limits
*/
public function testPhpScriptTimeoutNotExceeded()
{
$this->assertTimeoutNotExceeded([
'language' => 'php',
'timeout' => self::TIMEOUT_LENGTH,
]);
}
/**
* A helper method to generate a script object from the factory
*
* @param string $language
* @param int $timeout
* @return Script
*/
private function getScript($language, $timeout)
{
return Script::factory()->create([
'run_as_user_id' => $this->user->id,
'language' => $language,
'timeout' => $timeout,
]);
}
}