Skip to content

[Console] Rename some methods related to redraw frequency #33732

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

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CHANGELOG
-----

* added `Question::setTrimmable` default to true to allow the answer to be trimmed
* added method `preventRedrawFasterThan()` and `forceRedrawSlowerThan()` on `ProgressBar`
* added method `minSecondsBetweenRedraws()` and `maxSecondsBetweenRedraws()` on `ProgressBar`
* `Application` implements `ResetInterface`
* marked all dispatched event classes as `@final`
* added support for displaying table horizontally
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ public function setRedrawFrequency(?int $freq)
$this->redrawFreq = null !== $freq ? max(1, $freq) : null;
}

public function preventRedrawFasterThan(float $intervalInSeconds): void
public function minSecondsBetweenRedraws(float $seconds): void
{
$this->minSecondsBetweenRedraws = $intervalInSeconds;
$this->minSecondsBetweenRedraws = $seconds;
}

public function forceRedrawSlowerThan(float $intervalInSeconds): void
public function maxSecondsBetweenRedraws(float $seconds): void
{
$this->maxSecondsBetweenRedraws = $intervalInSeconds;
$this->maxSecondsBetweenRedraws = $seconds;
}

/**
Expand Down
40 changes: 32 additions & 8 deletions src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,23 +943,47 @@ public function testBarWidthWithMultilineFormat()
putenv('COLUMNS=120');
}

public function testForceRedrawSlowerThan(): void
public function testMinAndMaxSecondsBetweenRedraws(): void
{
$bar = new ProgressBar($output = $this->getOutputStream());
$bar->setRedrawFrequency(1);
$bar->minSecondsBetweenRedraws(5);
$bar->maxSecondsBetweenRedraws(10);

$bar->start();
$bar->setProgress(1);
sleep(10);
$bar->setProgress(2);
sleep(20);
$bar->setProgress(3);

rewind($output->getStream());
$this->assertEquals(
' 0 [>---------------------------]'.
$this->generateOutput(' 2 [-->-------------------------]').
$this->generateOutput(' 3 [--->------------------------]'),
stream_get_contents($output->getStream())
);
}

public function testMaxSecondsBetweenRedraws(): void
{
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
$bar->setRedrawFrequency(4); // disable step based redraws
$bar->start();

$bar->setProgress(1); // No treshold hit, no redraw
$bar->forceRedrawSlowerThan(2);
$bar->maxSecondsBetweenRedraws(2);
sleep(1);
$bar->setProgress(2); // Still no redraw because redraw is forced after 2 seconds only
$bar->setProgress(2); // Still no redraw because it takes 2 seconds for a redraw
sleep(1);
$bar->setProgress(3); // 1+1 = 2 -> redraw finally
$bar->setProgress(4); // step based redraw freq hit, redraw even without sleep
$bar->setProgress(5); // No treshold hit, no redraw
$bar->preventRedrawFasterThan(3);
$bar->maxSecondsBetweenRedraws(3);
sleep(2);
$bar->setProgress(6); // No redraw even though 2 seconds passed. Throttling has priority
$bar->preventRedrawFasterThan(2);
$bar->maxSecondsBetweenRedraws(2);
$bar->setProgress(7); // Throttling relaxed, draw

rewind($output->getStream());
Expand All @@ -972,16 +996,16 @@ public function testForceRedrawSlowerThan(): void
);
}

public function testPreventRedrawFasterThan()
public function testMinSecondsBetweenRedraws()
{
$bar = new ProgressBar($output = $this->getOutputStream(), 0, 0);
$bar->setRedrawFrequency(1);
$bar->preventRedrawFasterThan(1);
$bar->minSecondsBetweenRedraws(1);
$bar->start();
$bar->setProgress(1); // Too fast, should not draw
sleep(1);
$bar->setProgress(2); // 1 second passed, draw
$bar->preventRedrawFasterThan(2);
$bar->minSecondsBetweenRedraws(2);
sleep(1);
$bar->setProgress(3); // 1 second passed but we changed threshold, should not draw
sleep(1);
Expand Down