Skip to content

Commit

Permalink
Fix infinite loop bug if error thrown in final station.
Browse files Browse the repository at this point in the history
  • Loading branch information
mavickers committed Mar 12, 2022
1 parent e31621b commit e544833
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
24 changes: 23 additions & 1 deletion Cargo.Tests/Integration/Finalize.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using System;
using System.Linq;
using System.Runtime.Remoting;
using LightPath.Cargo.Tests.Integration.Common;
using LightPath.Cargo.Tests.Unit;
using Xunit;
Expand Down Expand Up @@ -86,5 +88,25 @@ public void Scenario3()
Assert.False(bus.Package.IsAborted);
Assert.Equal(21, content.IntVal);
}

/// <summary>
/// Bug test case
/// https://github.com/mavickers/Cargo/issues/5
/// </summary>
[Fact]
public void Scenario4()
{
var content = new ContentModel2();
var bus = Bus.New<ContentModel2>()
.WithStationRepeatLimit(1)
.WithStation<Stations.Finalize.Station2>()
.WithFinalStation<Stations.Finalize.FinalStationCrasher>();

bus.Go(content);

Assert.True(bus.Package.IsErrored);
Assert.False(bus.Package.IsAborted);
Assert.True(bus.Package.Results.Last().Exception is System.NotImplementedException);
}
}
}
12 changes: 12 additions & 0 deletions Cargo.Tests/Integration/Stations/Finalize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,17 @@ public override void Process()
Contents.IntVal += 20;
}
}

/// <summary>
/// Purposefully throw an exception in the final station
/// https://github.com/mavickers/Cargo/issues/5
/// </summary>
public class FinalStationCrasher : Station<ContentModel2>
{
public override void Process()
{
throw new NotImplementedException();
}
}
}
}
3 changes: 2 additions & 1 deletion Cargo/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public TContent Go(TContent content, Func<TContent, TContent> callback = null)
while (currentStationIndex < stationList.Count)
{
var currentStation = (Station<TContent>) Activator.CreateInstance(stationList[currentStationIndex]);
var isFinalStation = _finalStation != null && stationList[currentStationIndex] == stationList.Last();

if (currentStation == null) throw new Exception($"Unable to instantiate {stationList[currentStationIndex].FullName}");

Expand Down Expand Up @@ -97,7 +98,7 @@ public TContent Go(TContent content, Func<TContent, TContent> callback = null)
// there is no final station configured then set the index to exceed the station count so that
// no more stations are processed.

currentStationIndex = _package.LastStationResult.WasAborted || (_package.LastStationResult.WasFail && _withAbortOnError)
currentStationIndex = _package.LastStationResult.WasAborted || (_package.LastStationResult.WasFail && _withAbortOnError && !isFinalStation)
? stationList.Count + (stationList.Last() == _finalStation ? -1 : 1)
: currentStationIndex + 1;
}
Expand Down
6 changes: 3 additions & 3 deletions Cargo/Cargo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
<PackageTags>COR</PackageTags>
<Description>A lightweight chain of responsibility library for .Net.</Description>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Version>1.2.4</Version>
<AssemblyVersion>1.2.4.0</AssemblyVersion>
<FileVersion>1.2.4.0</FileVersion>
<Version>1.2.5</Version>
<AssemblyVersion>1.2.5.0</AssemblyVersion>
<FileVersion>1.2.5.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e544833

Please sign in to comment.