-
Notifications
You must be signed in to change notification settings - Fork 30
fix: Rewind iterator before calling current/next #2218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CarlSchwan
wants to merge
1
commit into
master
Choose a base branch
from
carl/rewind
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\LogReader\Tests\Unit\Controller; | ||
|
|
||
| use OCA\LogReader\Controller\LogController; | ||
| use OCA\LogReader\Log\LogIterator; | ||
| use OCA\LogReader\Log\LogIteratorFactory; | ||
| use OCA\LogReader\Service\SettingsService; | ||
| use OCP\IRequest; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Psr\Log\LoggerInterface; | ||
| use Test\TestCase; | ||
|
|
||
| class LogControllerTest extends TestCase { | ||
|
|
||
| private LogController $logController; | ||
|
|
||
| /** @var LogIteratorFactory|MockObject */ | ||
| private $logIteratorFactory; | ||
|
|
||
| /** @var SettingsService|MockObject */ | ||
| private $settingsService; | ||
|
|
||
| /** @var LoggerInterface|MockObject */ | ||
| private $logger; | ||
|
|
||
| /** @var IRequest|MockObject */ | ||
| private $request; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->logIteratorFactory = $this->createStub(LogIteratorFactory::class); | ||
| $this->settingsService = $this->createStub(SettingsService::class); | ||
| $this->logger = $this->createStub(LoggerInterface::class); | ||
| $this->request = $this->createStub(IRequest::class); | ||
|
|
||
| $this->settingsService->method('getLoggingType')->willReturn('file'); | ||
| $this->settingsService->method('getShownLevels')->willReturn([0, 1, 2, 3, 4]); | ||
|
|
||
| $this->logController = new LogController( | ||
| 'logreader', | ||
| $this->request, | ||
| $this->logIteratorFactory, | ||
| $this->settingsService, | ||
| $this->logger, | ||
| ); | ||
| } | ||
|
|
||
| private function getLogIterator(string $log): LogIterator { | ||
| $handle = fopen('php://temp', 'r+'); | ||
| fwrite($handle, $log); | ||
| rewind($handle); | ||
| return new LogIterator($handle, \DateTime::ATOM, 'UTC'); | ||
| } | ||
|
|
||
| /** | ||
| * Every call to the factory must return a freshly rewound iterator over | ||
| * the same log content, mirroring how the real factory opens the log | ||
| * file anew for each call. | ||
| */ | ||
| private function mockLogWithEntries(string $log): void { | ||
| $this->logIteratorFactory->method('getLogIterator') | ||
| ->willReturnCallback(fn () => $this->getLogIterator($log)); | ||
| } | ||
|
|
||
| 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" | ||
| . '{"reqId":"3","level":3,"time":"2019-11-04T18:50:59+00:00","app":"files"}'; | ||
| $this->mockLogWithEntries($log); | ||
|
|
||
| $response = $this->logController->poll('1'); | ||
| $data = $response->getData(); | ||
|
|
||
| $this->assertCount(2, $data); | ||
| // sorted newest first, the last written entry (reqId 3) must be included | ||
| $this->assertEquals('3', $data[0]['reqId']); | ||
| $this->assertEquals('2', $data[1]['reqId']); | ||
| } | ||
|
|
||
| public function testPollReturnsEmptyWhenThereIsNoNewEntry(): void { | ||
| $log = '{"reqId":"1","level":3,"time":"2019-11-04T18:50:57+00:00","app":"comments"}'; | ||
| $this->mockLogWithEntries($log); | ||
|
|
||
| $response = $this->logController->poll('1'); | ||
|
|
||
| $this->assertEquals([], $response->getData()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This already does rewind on creating from class, and calling next() once - so it should be on the latest line already:
What does it fix? I remember investigating something with it, but didn't finish. But not sure that current code brings something to it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before the fix I always got null has return from
getLastItemThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is something weird going on because I agree with @Antreesy the code is supposed to return a rewinded iterator already from the factory