From 9dead8ed70dd68310d78e4d0da29ceee1258bae0 Mon Sep 17 00:00:00 2001 From: David Dreschner Date: Mon, 19 Oct 2026 15:37:36 +0200 Subject: [PATCH 1/2] perf(LogController): Use streamed responses to send one entry at a time At the moment, the endpoints /api/poll and /api/log collect all entries they return and then JSON-encode them all at once. In a test scenario with ten 25 MB entries among the newest 50, the request needed 720 MB and failed on the memory limit, leaving the log viewer empty. Since Nextcloud 33, there is a better way to do it: using StreamTraversableResponse and sending one entry at a time. That way, the same test scenario only needs 95 MB at peak. Such large entries can occur after an upgrade, as the upgrader sets the log level to debug, and exceptions are then logged with all their arguments. As the response is already being sent while the entries are read, values JSON cannot represent are replaced instead of failing halfway, and a poll reads at most the newest 100,000 entries like a search. LogIteratorFactory now only returns lines that decode to an entry with a level, so lines without a level are no longer shown. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: David Dreschner --- lib/Controller/LogController.php | 119 +++++++++++---- lib/Log/LogIteratorFactory.php | 3 +- tests/Unit/Controller/LogControllerTest.php | 156 +++++++++++++++++++- tests/Unit/Log/LogIteratorFactoryTest.php | 79 ++++++++++ 4 files changed, 322 insertions(+), 35 deletions(-) create mode 100644 tests/Unit/Log/LogIteratorFactoryTest.php diff --git a/lib/Controller/LogController.php b/lib/Controller/LogController.php index 1a5109a83..84cd5bcaa 100644 --- a/lib/Controller/LogController.php +++ b/lib/Controller/LogController.php @@ -16,6 +16,7 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\StreamTraversableResponse; use OCP\IRequest; use Psr\Log\LoggerInterface; @@ -26,6 +27,11 @@ * @psalm-api */ class LogController extends Controller { + /** + * Maximum number of entries of the shown levels a search or a poll goes through, hidden ones are not counted. + * A search does not find older entries, see poll() for what happens to them there. + */ + private const int MAX_SCANNED_ENTRIES = 100000; public function __construct( $appName, @@ -41,10 +47,9 @@ public function __construct( * @param string $query * @param int $count * @param int $offset - * @return JSONResponse */ #[AuthorizedAdminSetting(settings: Admin::class)] - public function get($query = '', $count = 50, $offset = 0): JSONResponse { + public function get($query = '', $count = 50, $offset = 0): JSONResponse|StreamTraversableResponse { $logType = $this->settingsService->getLoggingType(); // we only support web access when `log_type` is set to `file` (the default) if ($logType !== 'file') { @@ -55,7 +60,7 @@ public function get($query = '', $count = 50, $offset = 0): JSONResponse { $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); if ($query !== '') { - $iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times + $iterator = new \LimitIterator($iterator, 0, self::MAX_SCANNED_ENTRIES); $iterator->rewind(); $iterator = new SearchFilter($iterator, $query); $iterator->rewind(); @@ -87,9 +92,13 @@ private function getLastItem() { * - a combination of reqid and counting the messages for that specific reqid * will work in some cases but not when there are more than 50 messages of that * request. + * + * @note Only the newest MAX_SCANNED_ENTRIES messages are sent. If there are more since + * $lastReqId, the log viewer misses the older ones and loads some messages twice, until + * it is reloaded. */ #[AuthorizedAdminSetting(settings: Admin::class)] - public function poll(string $lastReqId): JSONResponse { + public function poll(string $lastReqId): JSONResponse|StreamTraversableResponse { $logType = $this->settingsService->getLoggingType(); // we only support web access when `log_type` is set to `file` (the default) if ($logType !== 'file') { @@ -98,49 +107,95 @@ public function poll(string $lastReqId): JSONResponse { } $lastItem = $this->getLastItem(); - if ($lastItem === null || $lastItem['reqId'] === $lastReqId) { + if ($lastItem === null || ($lastItem['reqId'] ?? null) === $lastReqId) { return new JSONResponse([]); } $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); $iterator->rewind(); - $data = []; - - while ($iterator->valid()) { - $line = $iterator->current(); - - if ($line['reqId'] === $lastReqId) { - break; - } + return $this->streamJson($this->encodeEntriesNewerThan($iterator, $lastReqId)); + } - if (!is_null($line)) { - $line['id'] = uniqid(); - $data[] = $line; - } + protected function responseFromIterator(\Iterator $iterator, int $count, int $offset): StreamTraversableResponse { + $iterator->rewind(); + for ($i = 0; $i < $offset; $i++) { $iterator->next(); } + return $this->streamJson($this->encodePage($iterator, $count)); + } - return new JSONResponse($data); + /** + * @return \Generator + */ + private function encodeEntriesNewerThan(\Iterator $iterator, string $lastReqId): \Generator { + yield '['; + yield from $this->encodeEntries($this->takeEntriesNewerThan($iterator, $lastReqId)); + yield ']'; } - protected function responseFromIterator(\Iterator $iterator, $count, $offset): JSONResponse { - $iterator->rewind(); - for ($i = 0; $i < $offset; $i++) { - $iterator->next(); + /** + * @return \Generator + */ + private function encodePage(\Iterator $iterator, int $count): \Generator { + yield '{"data":['; + yield from $this->encodeEntries($this->takeEntries($iterator, $count)); + yield '],"remain":' . ($iterator->valid() ? 'true' : 'false') . '}'; + } + + /** + * One part per entry, so only a single entry is held in memory at a time + * + * @param iterable $entries + * @return \Generator + */ + private function encodeEntries(iterable $entries): \Generator { + $separator = ''; + foreach ($entries as $entry) { + $entry['id'] = uniqid(); + yield $separator . $this->encodeJson($entry); + $separator = ','; } - $data = []; + } + + /** + * Leaves the iterator behind the last taken entry, so its valid() tells whether entries remain + * + * @return \Generator + */ + private function takeEntries(\Iterator $iterator, int $count): \Generator { for ($i = 0; $i < $count && $iterator->valid(); $i++) { - $line = $iterator->current(); - if (!is_null($line)) { - $line['id'] = uniqid(); - $data[] = $line; - } + yield $iterator->current(); $iterator->next(); } - return new JSONResponse([ - 'data' => $data, - 'remain' => $iterator->valid() - ]); + } + + /** + * @return \Generator + */ + private function takeEntriesNewerThan(\Iterator $iterator, string $lastReqId): \Generator { + foreach ($this->takeEntries($iterator, self::MAX_SCANNED_ENTRIES) as $entry) { + if (($entry['reqId'] ?? null) === $lastReqId) { + return; + } + yield $entry; + } + } + + /** + * @param \Generator $json + */ + private function streamJson(\Generator $json): StreamTraversableResponse { + return new StreamTraversableResponse($json, Http::STATUS_OK, ['Content-Type' => 'application/json; charset=utf-8']); + } + + /** + * Like JSONResponse::render(), but never fails and replaces values JSON cannot represent (e.g. INF), as the + * response is already being sent and skipping the entry would shift the offsets of the next pages + * + * @psalm-suppress FalsableReturnStatement, InvalidFalsableReturnType + */ + private function encodeJson(mixed $data): string { + return json_encode($data, JSON_HEX_TAG | JSON_PARTIAL_OUTPUT_ON_ERROR, 2048); } } diff --git a/lib/Log/LogIteratorFactory.php b/lib/Log/LogIteratorFactory.php index 95fcc4ed5..a8bcdd511 100644 --- a/lib/Log/LogIteratorFactory.php +++ b/lib/Log/LogIteratorFactory.php @@ -40,7 +40,8 @@ public function getLogIterator(array $levels): \Iterator { $handle = fopen($log->getLogFilePath(), 'rb'); if ($handle) { $iterator = new LogIterator($handle, $dateFormat, $timezone); - return new \CallbackFilterIterator($iterator, fn ($logItem) => $logItem && in_array($logItem['level'], $levels)); + // isset() also rules out lines that decode to a scalar + return new \CallbackFilterIterator($iterator, fn ($logItem) => isset($logItem['level']) && in_array($logItem['level'], $levels)); } else { throw new \Exception('Error while opening ' . $log->getLogFilePath()); } diff --git a/tests/Unit/Controller/LogControllerTest.php b/tests/Unit/Controller/LogControllerTest.php index 635564b57..143332d6a 100644 --- a/tests/Unit/Controller/LogControllerTest.php +++ b/tests/Unit/Controller/LogControllerTest.php @@ -12,6 +12,10 @@ use OCA\LogReader\Log\LogIterator; use OCA\LogReader\Log\LogIteratorFactory; use OCA\LogReader\Service\SettingsService; +use OCP\AppFramework\Http\IOutput; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Http\StreamTraversableResponse; use OCP\IRequest; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -70,6 +74,113 @@ private function mockLogWithEntries(string $log): void { ->willReturnCallback(fn () => $this->getLogIterator($log)); } + private function getLogWithEntries(int $count): string { + $lines = []; + for ($reqId = 1; $reqId <= $count; $reqId++) { + $lines[] = '{"reqId":"' . $reqId . '","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Entry ' . $reqId . '"}'; + } + return implode("\n", $lines); + } + + /** + * @return list the parts the response sends, in order + */ + private function getOutputParts(StreamTraversableResponse $response): array { + $parts = []; + $output = $this->createStub(IOutput::class); + $output->method('setOutput') + ->willReturnCallback(function (string $part) use (&$parts): void { + $parts[] = $part; + }); + $response->callback($output); + return $parts; + } + + private function getResponseData(Response $response): mixed { + if ($response instanceof JSONResponse) { + return $response->getData(); + } + $this->assertInstanceOf(StreamTraversableResponse::class, $response); + return json_decode(implode('', $this->getOutputParts($response)), true, flags: JSON_THROW_ON_ERROR); + } + + public function testGetReturnsNewestEntriesFirst(): void { + $this->mockLogWithEntries($this->getLogWithEntries(3)); + + $response = $this->logController->get(); + $data = $this->getResponseData($response); + + $this->assertSame('application/json; charset=utf-8', $response->getHeaders()['Content-Type']); + $this->assertSame(['3', '2', '1'], array_column($data['data'], 'reqId')); + $this->assertSame('Entry 3', $data['data'][0]['message']); + $this->assertCount(3, array_unique(array_column($data['data'], 'id'))); + $this->assertFalse($data['remain']); + } + + public function testGetReturnsRequestedPage(): void { + $this->mockLogWithEntries($this->getLogWithEntries(5)); + + $data = $this->getResponseData($this->logController->get('', 2, 1)); + + $this->assertSame(['4', '3'], array_column($data['data'], 'reqId')); + $this->assertTrue($data['remain']); + } + + public function testGetReturnsEmptyPageForEmptyLog(): void { + $this->mockLogWithEntries(''); + + $data = $this->getResponseData($this->logController->get()); + + $this->assertSame(['data' => [], 'remain' => false], $data); + } + + public function testGetFiltersBySearchQuery(): void { + $this->mockLogWithEntries($this->getLogWithEntries(12)); + + $data = $this->getResponseData($this->logController->get('Entry 1')); + + $this->assertSame(['12', '11', '10', '1'], array_column($data['data'], 'reqId')); + } + + public function testGetSendsEachEntrySeparately(): void { + $this->mockLogWithEntries($this->getLogWithEntries(3)); + + $response = $this->logController->get(); + + $this->assertInstanceOf(StreamTraversableResponse::class, $response); + // The opening, one part per entry and the closing with "remain" + $this->assertCount(5, $this->getOutputParts($response)); + } + + public function testGetEscapesHtmlLikeJsonResponse(): void { + $this->mockLogWithEntries('{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":""}'); + + $response = $this->logController->get(); + + $this->assertInstanceOf(StreamTraversableResponse::class, $response); + $json = implode('', $this->getOutputParts($response)); + $this->assertStringNotContainsString(''))->render(), '"'), $json); + } + + public function testGetKeepsPagesConsecutiveWithAnEntryThatCannotBeEncoded(): void { + // 1e999 is too large for a float and decodes to INF, which JSON cannot represent + $this->mockLogWithEntries(implode("\n", [ + '{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Entry 1"}', + '{"reqId":"2","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Entry 2"}', + '{"reqId":"3","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":1e999}', + '{"reqId":"4","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Entry 4"}', + ])); + + // Like the frontend, request the next page at the number of entries received so far + $firstPage = $this->getResponseData($this->logController->get('', 2, 0)); + $secondPage = $this->getResponseData($this->logController->get('', 2, count($firstPage['data']))); + + $this->assertSame(['4', '3', '2', '1'], array_column([...$firstPage['data'], ...$secondPage['data']], 'reqId')); + $this->assertSame(0, $firstPage['data'][1]['message']); + $this->assertFalse($secondPage['remain']); + } + public function testPollIncludesTheNewestLogEntry(): void { $log = '{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"comments"}' . "\n" . '{"reqId":"2","level":3,"time":"2019-11-04T18:50:58+00:00","app":"gallery"}' . "\n" @@ -77,7 +188,7 @@ public function testPollIncludesTheNewestLogEntry(): void { $this->mockLogWithEntries($log); $response = $this->logController->poll('1'); - $data = $response->getData(); + $data = $this->getResponseData($response); $this->assertCount(2, $data); // sorted newest first, the last written entry (reqId 3) must be included @@ -91,6 +202,47 @@ public function testPollReturnsEmptyWhenThereIsNoNewEntry(): void { $response = $this->logController->poll('1'); - $this->assertEquals([], $response->getData()); + $this->assertEquals([], $this->getResponseData($response)); + } + + public function testPollSendsEachNewEntrySeparately(): void { + $this->mockLogWithEntries($this->getLogWithEntries(4)); + + $response = $this->logController->poll('2'); + + $this->assertInstanceOf(StreamTraversableResponse::class, $response); + $parts = $this->getOutputParts($response); + // The opening bracket, one part per new entry and the closing bracket + $this->assertCount(4, $parts); + $this->assertSame(['4', '3'], array_column(json_decode(implode('', $parts), true), 'reqId')); + } + + public function testPollHandlesEntriesWithoutReqIdWithoutWarnings(): void { + $this->mockLogWithEntries(implode("\n", [ + '{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Entry 1"}', + '{"level":3,"time":"2019-11-04T18:50:58+00:00","app":"files","message":"No request id"}', + '{"level":3,"time":"2019-11-04T18:50:59+00:00","app":"files","message":"No request id either"}', + ])); + + set_error_handler(static function (int $level, string $message): never { + throw new \ErrorException($message, 0, $level); + }); + try { + $data = $this->getResponseData($this->logController->poll('1')); + } finally { + restore_error_handler(); + } + + $this->assertSame(['No request id either', 'No request id'], array_column($data, 'message')); + } + + public function testPollReadsAtMostOneHundredThousandEntries(): void { + $this->mockLogWithEntries($this->getLogWithEntries(100001)); + + $data = $this->getResponseData($this->logController->poll('unknown')); + + $this->assertCount(100000, $data); + $this->assertSame('100001', $data[0]['reqId']); + $this->assertSame('2', $data[99999]['reqId']); } } diff --git a/tests/Unit/Log/LogIteratorFactoryTest.php b/tests/Unit/Log/LogIteratorFactoryTest.php new file mode 100644 index 000000000..06a1f5fa3 --- /dev/null +++ b/tests/Unit/Log/LogIteratorFactoryTest.php @@ -0,0 +1,79 @@ +logFile = tempnam(sys_get_temp_dir(), 'logreader'); + + $config = $this->createStub(IConfig::class); + $config->method('getSystemValue') + ->willReturnCallback(fn (string $key, mixed $default) => $default); + + $log = $this->createStubForIntersectionOfInterfaces([IWriter::class, IFileBased::class]); + $log->method('getLogFilePath') + ->willReturn($this->logFile); + + $logFactory = $this->createStub(ILogFactory::class); + $logFactory->method('get') + ->willReturn($log); + + $this->logIteratorFactory = new LogIteratorFactory($config, $logFactory); + } + + protected function tearDown(): void { + unlink($this->logFile); + parent::tearDown(); + } + + public static function levelsProvider(): array { + return [ + 'all levels, the default' => ['levels' => [0, 1, 2, 3, 4], 'expectedReqIds' => ['8', '7', '1']], + 'warning and above' => ['levels' => [2, 3, 4], 'expectedReqIds' => ['8', '1']], + ]; + } + + #[DataProvider(methodName: 'levelsProvider')] + public function testReturnsOnlyLogEntriesOfTheRequestedLevels(array $levels, array $expectedReqIds): void { + file_put_contents($this->logFile, implode("\n", [ + '{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Error"}', + '"text"', + '123', + '[1,2]', + 'not json', + '{"reqId":"6","time":"2019-11-04T18:50:57+00:00","app":"files","message":"No level"}', + '{"reqId":"7","level":1,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Info"}', + '{"reqId":"8","level":2,"time":"2019-11-04T18:50:57+00:00","app":"files","message":"Warning"}', + ])); + + set_error_handler(static function (int $level, string $message): never { + throw new \ErrorException($message, 0, $level); + }); + try { + $entries = iterator_to_array($this->logIteratorFactory->getLogIterator($levels), false); + } finally { + restore_error_handler(); + } + + $this->assertSame($expectedReqIds, array_column($entries, 'reqId')); + } +} From 6820ebe2ae40dee346f8cd753de10e9a9e66e033 Mon Sep 17 00:00:00 2001 From: David Dreschner Date: Mon, 19 Oct 2026 16:03:22 +0200 Subject: [PATCH 2/2] perf(LogController): Read the log only once per search and poll A search rewound its iterator in the SearchFilter constructor, in get() and again in responseFromIterator(). Each rewind of a filter reads the log up to the first matching entry, so a search without a match read the whole log several times. A poll opened the log once to compare the newest entry with lastReqId, and then a second time to send the new entries. Skipping to the offset of a page also went on after the end of the log. Rewind a search only in responseFromIterator(), use a single iterator for a poll, and stop skipping at the end of the log. The streamed response already answers with an empty list when the newest entry is the last known one. A search without a match over 100,000 entries now takes 0.20 s instead of 0.40 s. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: David Dreschner --- lib/Controller/LogController.php | 23 +------ lib/Log/SearchFilter.php | 1 - tests/Unit/Controller/LogControllerTest.php | 76 +++++++++++++++++++++ 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/lib/Controller/LogController.php b/lib/Controller/LogController.php index 84cd5bcaa..18bf708c7 100644 --- a/lib/Controller/LogController.php +++ b/lib/Controller/LogController.php @@ -60,26 +60,12 @@ public function get($query = '', $count = 50, $offset = 0): JSONResponse|StreamT $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); if ($query !== '') { - $iterator = new \LimitIterator($iterator, 0, self::MAX_SCANNED_ENTRIES); - $iterator->rewind(); - $iterator = new SearchFilter($iterator, $query); - $iterator->rewind(); - return $this->responseFromIterator($iterator, $count, $offset); + $iterator = new SearchFilter(new \LimitIterator($iterator, 0, self::MAX_SCANNED_ENTRIES), $query); } return $this->responseFromIterator($iterator, $count, $offset); } - /** - * @brief Gets the last item in the log, bypassing any cache. - * @return mixed - */ - private function getLastItem() { - $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); - $iterator->rewind(); - return $iterator->current(); - } - /** * @brief Use to poll for new log messages since $lastReqId. * @@ -106,11 +92,6 @@ public function poll(string $lastReqId): JSONResponse|StreamTraversableResponse return new JSONResponse([], Http::STATUS_FAILED_DEPENDENCY); } - $lastItem = $this->getLastItem(); - if ($lastItem === null || ($lastItem['reqId'] ?? null) === $lastReqId) { - return new JSONResponse([]); - } - $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); $iterator->rewind(); @@ -119,7 +100,7 @@ public function poll(string $lastReqId): JSONResponse|StreamTraversableResponse protected function responseFromIterator(\Iterator $iterator, int $count, int $offset): StreamTraversableResponse { $iterator->rewind(); - for ($i = 0; $i < $offset; $i++) { + for ($i = 0; $i < $offset && $iterator->valid(); $i++) { $iterator->next(); } return $this->streamJson($this->encodePage($iterator, $count)); diff --git a/lib/Log/SearchFilter.php b/lib/Log/SearchFilter.php index 855ec0c1a..10d1a075d 100644 --- a/lib/Log/SearchFilter.php +++ b/lib/Log/SearchFilter.php @@ -24,7 +24,6 @@ class SearchFilter extends \FilterIterator { public function __construct(\Iterator $iterator, string $query) { parent::__construct($iterator); - $this->rewind(); $this->query = strtolower($query); $this->levels = ['Debug', 'Info', 'Warning', 'Error', 'Fatal']; } diff --git a/tests/Unit/Controller/LogControllerTest.php b/tests/Unit/Controller/LogControllerTest.php index 143332d6a..c492c9f6e 100644 --- a/tests/Unit/Controller/LogControllerTest.php +++ b/tests/Unit/Controller/LogControllerTest.php @@ -17,6 +17,7 @@ use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\StreamTraversableResponse; use OCP\IRequest; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Test\TestCase; @@ -82,6 +83,23 @@ private function getLogWithEntries(int $count): string { return implode("\n", $lines); } + /** + * A log iterator that counts how often it moves on to the next entry + * + * @return \IteratorIterator&object{steps: int} + */ + private function getStepCountingLogIterator(int $entryCount): \IteratorIterator { + return new class($this->getLogIterator($this->getLogWithEntries($entryCount))) extends \IteratorIterator { + public int $steps = 0; + + #[\Override] + public function next(): void { + $this->steps++; + parent::next(); + } + }; + } + /** * @return list the parts the response sends, in order */ @@ -245,4 +263,62 @@ public function testPollReadsAtMostOneHundredThousandEntries(): void { $this->assertSame('100001', $data[0]['reqId']); $this->assertSame('2', $data[99999]['reqId']); } + + public static function pollProvider(): array { + return [ + 'new entries' => ['lastReqId' => '1', 'expectedReqIds' => ['3', '2']], + 'no new entry' => ['lastReqId' => '3', 'expectedReqIds' => []], + ]; + } + + #[DataProvider(methodName: 'pollProvider')] + public function testPollOpensTheLogOnce(string $lastReqId, array $expectedReqIds): void { + $log = $this->getLogWithEntries(3); + $opened = 0; + $this->logIteratorFactory->method('getLogIterator') + ->willReturnCallback(function () use ($log, &$opened) { + $opened++; + // Like the factory's filter, this needs a rewind before the first entry can be read + return new \CallbackFilterIterator($this->getLogIterator($log), fn () => true); + }); + + $data = $this->getResponseData($this->logController->poll($lastReqId)); + + $this->assertSame($expectedReqIds, array_column($data, 'reqId')); + $this->assertSame(1, $opened); + } + + public function testSearchReadsThroughTheLogOnce(): void { + $iterator = $this->getStepCountingLogIterator(20); + $this->logIteratorFactory->method('getLogIterator') + ->willReturn($iterator); + + $data = $this->getResponseData($this->logController->get('no entry contains this')); + + $this->assertSame(['data' => [], 'remain' => false], $data); + $this->assertSame(20, $iterator->steps); + } + + public function testGetStopsSkippingToTheOffsetAtTheEndOfTheLog(): void { + $iterator = $this->getStepCountingLogIterator(20); + $this->logIteratorFactory->method('getLogIterator') + ->willReturn($iterator); + + $data = $this->getResponseData($this->logController->get('', 50, 1000)); + + $this->assertSame(['data' => [], 'remain' => false], $data); + $this->assertSame(20, $iterator->steps); + } + + public function testSearchReadsThroughTheLogOnceWhenNoEntryHasAShownLevel(): void { + $iterator = $this->getStepCountingLogIterator(20); + // Like the factory's level filter, here with none of the entries' levels shown + $this->logIteratorFactory->method('getLogIterator') + ->willReturn(new \CallbackFilterIterator($iterator, fn () => false)); + + $data = $this->getResponseData($this->logController->get('Entry')); + + $this->assertSame(['data' => [], 'remain' => false], $data); + $this->assertSame(20, $iterator->steps); + } }