-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
CommandSchedulingTest.php
209 lines (167 loc) · 5.55 KB
/
CommandSchedulingTest.php
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
<?php
namespace Illuminate\Tests\Integration\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;
class CommandSchedulingTest extends TestCase
{
/**
* Each run of this test is assigned a random ID to ensure that separate runs
* do not interfere with each other.
*
* @var string
*/
protected $id;
/**
* The path to the file that execution logs will be written to.
*
* @var string
*/
protected $logfile;
/**
* Just in case Testbench starts to ship an `artisan` script, we'll check and save a backup.
*
* @var string|null
*/
protected $originalArtisan;
/**
* The Filesystem instance for writing stubs and logs.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $fs;
protected function setUp(): void
{
parent::setUp();
$this->fs = new Filesystem;
$this->id = Str::random();
$this->logfile = storage_path("logs/command_scheduling_test_{$this->id}.log");
$this->writeArtisanScript();
}
protected function tearDown(): void
{
$this->fs->delete($this->logfile);
$this->fs->delete(base_path('artisan'));
if (! is_null($this->originalArtisan)) {
$this->fs->put(base_path('artisan'), $this->originalArtisan);
}
parent::tearDown();
}
/**
* @dataProvider executionProvider
*/
public function testExecutionOrder($background)
{
$event = $this->app->make(Schedule::class)
->command("test:{$this->id}")
->onOneServer()
->after(function () {
$this->fs->append($this->logfile, "after\n");
})
->before(function () {
$this->fs->append($this->logfile, "before\n");
});
if ($background) {
$event->runInBackground();
}
// We'll trigger the scheduler three times to simulate multiple servers
$this->artisan('schedule:run');
$this->artisan('schedule:run');
$this->artisan('schedule:run');
if ($background) {
// Since our command is running in a separate process, we need to wait
// until it has finished executing before running our assertions.
$this->waitForLogMessages('before', 'handled', 'after');
}
$this->assertLogged('before', 'handled', 'after');
}
public function executionProvider()
{
return [
'Foreground' => [false],
'Background' => [true],
];
}
protected function waitForLogMessages(...$messages)
{
$tries = 0;
$sleep = 100000; // 100K microseconds = 0.1 second
$limit = 50; // 0.1s * 50 = 5 second wait limit
do {
$log = $this->fs->get($this->logfile);
if (Str::containsAll($log, $messages)) {
return;
}
$tries++;
usleep($sleep);
} while ($tries < $limit);
}
protected function assertLogged(...$messages)
{
$log = trim($this->fs->get($this->logfile));
$this->assertEquals(implode("\n", $messages), $log);
}
protected function writeArtisanScript()
{
$path = base_path('artisan');
// Save existing artisan script if there is one
if ($this->fs->exists($path)) {
$this->originalArtisan = $this->fs->get($path);
}
$thisFile = __FILE__;
$logfile = var_export($this->logfile, true);
$script = <<<PHP
#!/usr/bin/env php
<?php
// This is a custom artisan script made specifically for:
//
// {$thisFile}
//
// It should be automatically cleaned up when the tests have finished executing.
// If you are seeing this file, an unexpected error must have occurred. Please
// manually remove it.
define('LARAVEL_START', microtime(true));
require __DIR__.'/../../../autoload.php';
\$app = require_once __DIR__.'/bootstrap/app.php';
\$kernel = \$app->make(Illuminate\Contracts\Console\Kernel::class);
// Here is our custom command for the test
class CommandSchedulingTestCommand_{$this->id} extends Illuminate\Console\Command
{
protected \$signature = 'test:{$this->id}';
public function handle()
{
\$logfile = {$logfile};
(new Illuminate\Filesystem\Filesystem)->append(\$logfile, "handled\\n");
}
}
// Register command with Kernel
Illuminate\Console\Application::starting(function (\$artisan) {
\$artisan->add(new CommandSchedulingTestCommand_{$this->id});
});
// Add command to scheduler so that the after() callback is trigger in our spawned process
Illuminate\Foundation\Application::getInstance()
->booted(function (\$app) {
\$app->resolving(Illuminate\Console\Scheduling\Schedule::class, function(\$schedule) {
\$fs = new Illuminate\Filesystem\Filesystem;
\$schedule->command("test:{$this->id}")
->after(function() use (\$fs) {
\$logfile = {$logfile};
\$fs->append(\$logfile, "after\\n");
})
->before(function() use (\$fs) {
\$logfile = {$logfile};
\$fs->append(\$logfile, "before\\n");
});
});
});
\$status = \$kernel->handle(
\$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
\$kernel->terminate(\$input, \$status);
exit(\$status);
PHP;
$this->fs->put($path, $script);
}
}