Skip to content

Commit

Permalink
ConcurrentTeskProgress
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Dec 1, 2017
1 parent dfab985 commit 0e90f00
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public void AddTest()
Assert.AreEqual(1, first.TaskCount);
Assert.AreEqual(next, first.Next);

Assert.AreEqual(10.0, first.GetTaskProgress(10));

Assert.AreEqual(0, next.Duration);
Assert.AreEqual(0, next.TaskCount);
Assert.AreEqual(null, next.Next);
Expand All @@ -31,6 +33,8 @@ public void AddTest()
Assert.AreEqual(next2, first.Next);
Assert.AreEqual(next, first.Next);

Assert.AreEqual(20.0, first.GetTaskProgress(40));

Assert.AreEqual(0, next2.Duration);
Assert.AreEqual(0, next2.TaskCount);
Assert.AreEqual(null, next2.Next);
Expand All @@ -40,11 +44,15 @@ public void AddTest()
Assert.AreEqual(3, first.TaskCount);
Assert.AreEqual(next3, first.Next);

Assert.AreEqual(3.0, first.GetTaskProgress(9));

Assert.AreEqual(20, next3.Duration);
Assert.AreEqual(2, next3.TaskCount);
Assert.AreEqual(next2, next3.Next);
Assert.AreEqual(next, next3.Next);

Assert.AreEqual(10.0 + 5, first.GetTaskProgress(40));

Assert.AreEqual(0, next2.Duration);
Assert.AreEqual(0, next2.TaskCount);
Assert.AreEqual(null, next2.Next);
Expand All @@ -54,6 +62,8 @@ public void AddTest()
Assert.AreEqual(1, next2.TaskCount);
Assert.AreEqual(next4, next2.Next);

Assert.AreEqual((30.0 / 4) + (20.0 / 3) + 20, first.GetTaskProgress(70));

Assert.AreEqual(0, next4.Duration);
Assert.AreEqual(0, next4.TaskCount);
Assert.AreEqual(null, next4.Next);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="IResourceConfig.cs" />
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IShouldProcess.cs" />
<Compile Include="ConcurrentTaskProgress.cs" />
<Compile Include="TimeSlot.cs" />
<Compile Include="StateOperationContext.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Microsoft.Azure.Commands.Common.Strategies
{
public sealed class ConcurrentTaskProgress
{
public TimeSlot TimeSlot { get; }

public int Begin { get; }

public ConcurrentTaskProgress(TimeSlot timeSlot, int begin)
{
TimeSlot = timeSlot;
Begin = begin;
}

public ConcurrentTaskProgress AddTask(int duration)
=> new ConcurrentTaskProgress(TimeSlot.AddTask(duration), Begin + duration);
}
}
19 changes: 16 additions & 3 deletions src/ResourceManager/Common/Commands.Common.Strategies/TimeSlot.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
namespace Microsoft.Azure.Commands.Common.Strategies
{
/// <summary>
/// TimeSlot is a node of a singly linked list of TimeSlots.
/// The last node of the list is always an empty time slot
/// - Duration = 0.
/// - Next = null.
/// - TaskCount = 0.
/// </summary>
public sealed class TimeSlot
{
public int Duration { get; private set; }
Expand All @@ -10,9 +17,7 @@ public sealed class TimeSlot

public bool IsLast => Next == null;

public TimeSlot() : this(0, 0, null)
{
}
public TimeSlot() : this(0, 0, null) { }

TimeSlot(int duration, int taskCount, TimeSlot next)
{
Expand Down Expand Up @@ -47,5 +52,13 @@ public TimeSlot AddTask(int duration)
return Next.AddTask(duration - Duration);
}
}

public double GetTaskProgress(int duration)
=> duration <= Duration
? GetProgress(duration)
: GetProgress(Duration) + Next.GetTaskProgress(duration - Duration);

double GetProgress(double duration)
=> duration / TaskCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
class TimeSlotExtensions
{
public TimeSlot
// public TimeSlot
}
}

0 comments on commit 0e90f00

Please sign in to comment.