-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[9.x] Fix takeUntilTimeout
method of LazyCollection
#41354
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1413,8 +1413,20 @@ public function takeUntilTimeout(DateTimeInterface $timeout) | |
{ | ||
$timeout = $timeout->getTimestamp(); | ||
|
||
return $this->takeWhile(function () use ($timeout) { | ||
return $this->now() < $timeout; | ||
return new static(function () use ($timeout) { | ||
$iterator = $this->getIterator(); | ||
|
||
if (! $iterator->valid() || $this->now() > $timeout) { | ||
return; | ||
} | ||
|
||
yield $iterator->key() => $iterator->current(); | ||
|
||
while ($iterator->valid() && $this->now() < $timeout) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. It is always pulling an extra item. |
||
$iterator->next(); | ||
|
||
yield $iterator->key() => $iterator->current(); | ||
} | ||
Comment on lines
+1423
to
+1429
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified into a single |
||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,32 @@ public function testTakeUntilTimeout() | |
m::close(); | ||
} | ||
|
||
public function testTakeUntilTimeoutWithPastTimeout() | ||
{ | ||
$timeout = Carbon::now()->subMinute(); | ||
|
||
$mock = m::mock(LazyCollection::class.'[now]'); | ||
|
||
$results = $mock | ||
->times(10) | ||
->tap(function ($collection) use ($mock, $timeout) { | ||
tap($collection) | ||
->mockery_init($mock->mockery_getContainer()) | ||
->shouldAllowMockingProtectedMethods() | ||
->shouldReceive('now') | ||
->times(1) | ||
->andReturn( | ||
(clone $timeout)->add(1, 'minute')->getTimestamp(), | ||
); | ||
}) | ||
->takeUntilTimeout($timeout) | ||
->all(); | ||
|
||
$this->assertSame([], $results); | ||
|
||
m::close(); | ||
} | ||
Comment on lines
+199
to
+223
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't actually test that it's lazy. It simply tests the returned result. The actual values pulled out of the original generator are not inspected here. Look at the In fact, creating a laziness test for the code in this PR would fail, since it still always pulls an extra unnecessary item from the original generator. |
||
|
||
public function testTapEach() | ||
{ | ||
$data = LazyCollection::times(10); | ||
|
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 is actually still pulling that extra element. The iterator's
valid
method always pulls the next item 😞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.
Ah sorry, I did not know that 😞