Skip to content

Commit

Permalink
improve SSE Apache FPM flush & testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 5, 2024
1 parent 7be74e6 commit bcdf8b0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/JsSse.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,17 @@ protected function flush(): void

private function outputEventResponse(string $content): void
{
// workaround flush() ignored by Apache mod_proxy_fcgi
// https://stackoverflow.com/questions/30707792/how-to-disable-buffering-with-apache2-and-mod-proxy-fcgi#36298336
// https://bz.apache.org/bugzilla/show_bug.cgi?id=68827
$content .= ': ' . str_repeat('x', 4_096) . "\n\n";

if ($this->echoFunction) {
($this->echoFunction)($content);

return;
}

// workaround flush() ignored by Apache mod_proxy_fcgi
// https://stackoverflow.com/questions/30707792/how-to-disable-buffering-with-apache2-and-mod-proxy-fcgi#36298336
// https://bz.apache.org/bugzilla/show_bug.cgi?id=68827
$content .= ': ' . str_repeat('x', 4_096) . "\n\n";

$app = $this->getApp();
\Closure::bind(static function () use ($app, $content): void {
$app->outputResponse($content);
Expand Down
19 changes: 2 additions & 17 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class DemosTest extends TestCase
|(?<array>\[(?:(?&json)(?:,(?&json))*|\s*)\])
|(?<object>\{(?:(?<pair>\s*(?&string)\s*:(?&json))(?:,(?&pair))*|\s*)\})
)\s*)$~sx';
protected static string $regexSseLine = '~^(event|data|(?=: x{4096}$)): .*$~';
protected static string $regexSse = '~^(event: [^\n]+\n(data: [^\n]*\n)+\n: x{4096}\n\n)+$~';

#[\Override]
public static function setUpBeforeClass(): void
Expand Down Expand Up @@ -452,22 +452,7 @@ public function testDemoSseResponse(string $path): void

$response = $this->getResponseFromRequest($path);
self::assertSame(200, $response->getStatusCode());

$outputLines = array_filter(
explode("\n", $response->getBody()->getContents()),
static fn ($v) => $v !== ''
);

// check SSE Syntax
self::assertGreaterThan(0, count($outputLines));
foreach ($outputLines as $index => $line) {
preg_match_all(self::$regexSseLine, $line, $matchesAll);
self::assertSame(
$line,
implode('', $matchesAll[0] ?? ['error']),
'Testing SSE response line ' . $index . ' with content ' . $line
);
}
self::assertMatchesRegularExpression(self::$regexSse, $response->getBody()->getContents());
}

public static function provideDemoJsonResponsePostCases(): iterable
Expand Down

0 comments on commit bcdf8b0

Please sign in to comment.