Improve Start-Sleep cmdlet to accept fractional seconds (#8537)

This commit is contained in:
Jani 2019-01-14 20:04:44 +02:00 committed by Aditya Patwardhan
parent e399b06cd2
commit 812456f84c
2 changed files with 21 additions and 15 deletions

View file

@ -43,8 +43,8 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "Seconds", ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateRangeAttribute(0, int.MaxValue / 1000)]
public int Seconds { get; set; }
[ValidateRangeAttribute(0.0, (double)(int.MaxValue / 1000))]
public double Seconds { get; set; }
/// <summary>
/// Allows sleep time to be specified in milliseconds.
@ -97,7 +97,7 @@ namespace Microsoft.PowerShell.Commands
switch (ParameterSetName)
{
case "Seconds":
sleepTime = Seconds * 1000;
sleepTime = (int)(Seconds * 1000);
break;
case "Milliseconds":

View file

@ -3,14 +3,13 @@
Describe "Start-Sleep DRT Unit Tests" -Tags "CI" {
# WaitHandle.WaitOne(milliseconds, exitContext) is not accurate.
# The wait time varies from 980ms to 1150ms from observation, so
# the tests here are changed to be greater than 950ms.
$minTime = 950
$maxTime = 1200
# The actual wait time can vary from 1450ms to 1700ms.
$minTime = 1450
$maxTime = 1700
It "Should work properly when sleeping with Second" {
$watch = [System.Diagnostics.Stopwatch]::StartNew()
Start-Sleep -Seconds 1
Start-Sleep -Seconds 1.5
$watch.Stop()
$watch.ElapsedMilliseconds | Should -BeGreaterThan $minTime
$watch.ElapsedMilliseconds | Should -BeLessThan $maxTime
@ -18,7 +17,7 @@ Describe "Start-Sleep DRT Unit Tests" -Tags "CI" {
It "Should work properly when sleeping with Milliseconds" {
$watch = [System.Diagnostics.Stopwatch]::StartNew()
Start-Sleep -Milliseconds 1000
Start-Sleep -Milliseconds 1500
$watch.Stop()
$watch.ElapsedMilliseconds | Should -BeGreaterThan $minTime
$watch.ElapsedMilliseconds | Should -BeLessThan $maxTime
@ -26,7 +25,15 @@ Describe "Start-Sleep DRT Unit Tests" -Tags "CI" {
It "Should work properly when sleeping with ms alias" {
$watch = [System.Diagnostics.Stopwatch]::StartNew()
Start-Sleep -ms 1000
Start-Sleep -ms 1500
$watch.Stop()
$watch.ElapsedMilliseconds | Should -BeGreaterThan $minTime
$watch.ElapsedMilliseconds | Should -BeLessThan $maxTime
}
It "Should work properly when sleeping without parameters" {
$watch = [System.Diagnostics.Stopwatch]::StartNew()
Start-Sleep 1.5
$watch.Stop()
$watch.ElapsedMilliseconds | Should -BeGreaterThan $minTime
$watch.ElapsedMilliseconds | Should -BeLessThan $maxTime
@ -34,11 +41,10 @@ Describe "Start-Sleep DRT Unit Tests" -Tags "CI" {
}
Describe "Start-Sleep" -Tags "CI" {
Context "Validate Start-Sleep works properly" {
It "Should only sleep for at least 1 second" {
$result = Measure-Command { Start-Sleep -s 1 }
$result.TotalSeconds | Should -BeGreaterThan 0.25
}
It "Should only sleep for at least 1 second" {
$result = Measure-Command { Start-Sleep -s 1 }
$result.TotalSeconds | Should -BeGreaterThan 0.25
}
}
}