mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-20 18:42:50 +08:00
Fix OverflowException when estimating the progress remaining time (#404)
Fix OverflowException when estimating the progress remaining time
This commit is contained in:
parent
cb8dc97847
commit
bfdaef95d6
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
using Spectre.Console.Testing;
|
using Spectre.Console.Testing;
|
||||||
@ -241,5 +242,30 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
" \n" + // bottom padding
|
" \n" + // bottom padding
|
||||||
"[?25h"); // show cursor
|
"[?25h"); // show cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Report_Max_Remaining_Time_For_Extremely_Small_Progress()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new TestConsole()
|
||||||
|
.Interactive();
|
||||||
|
|
||||||
|
var task = default(ProgressTask);
|
||||||
|
var progress = new Progress(console)
|
||||||
|
.Columns(new[] { new RemainingTimeColumn() })
|
||||||
|
.AutoRefresh(false)
|
||||||
|
.AutoClear(false);
|
||||||
|
|
||||||
|
// When
|
||||||
|
progress.Start(ctx =>
|
||||||
|
{
|
||||||
|
task = ctx.AddTask("foo");
|
||||||
|
task.Increment(double.Epsilon);
|
||||||
|
task.Increment(double.Epsilon);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then
|
||||||
|
task.RemainingTime.ShouldBe(TimeSpan.MaxValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -286,20 +286,20 @@ namespace Spectre.Console
|
|||||||
}
|
}
|
||||||
|
|
||||||
var speed = GetSpeed();
|
var speed = GetSpeed();
|
||||||
if (speed == null)
|
if (speed == null || speed == 0)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the speed is zero, the estimate below
|
// If the speed is near zero, the estimate below causes the
|
||||||
// will return infinity (since it's a double),
|
// TimeSpan creation to throw an OverflowException. Just return
|
||||||
// so let's set the speed to 1 in that case.
|
// the maximum possible remaining time instead of overflowing.
|
||||||
if (speed == 0)
|
var estimate = (MaxValue - Value) / speed.Value;
|
||||||
|
if (estimate > TimeSpan.MaxValue.TotalSeconds)
|
||||||
{
|
{
|
||||||
speed = 1;
|
return TimeSpan.MaxValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var estimate = (MaxValue - Value) / speed.Value;
|
|
||||||
return TimeSpan.FromSeconds(estimate);
|
return TimeSpan.FromSeconds(estimate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user