Skip to content

Commit

Permalink
Merge pull request #19 from Cysharp/feature/ReturnValueAsExitCode
Browse files Browse the repository at this point in the history
Treat the return value as the exit code.
  • Loading branch information
neuecc authored Oct 4, 2019
2 parents e8bd374 + a856b44 commit 8262211
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
24 changes: 24 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ You can call like here.
> SampleApp.exe -array [10,20,30] -person {"Age":10,"Name":"foo"}
```

Exit Code
---
If the batch method returns `int` or `Task<int>` value, BatchEngine will set the return value to the exit code.

```csharp
public class ExampleBatch : BatchBase
{
[Command(nameof(ExitCodeWithTask))]
public async Task<int> ExitCodeWithTask()
{
return 54321;
}

[Command(nameof(ExitCode))]
public int ExitCode()
{
return 12345;
}
}
```

> **NOTE**: If the method throws an unhandled exception, BatchEngine always set `1` to the exit code.


Daemon
---

Expand Down
12 changes: 10 additions & 2 deletions src/MicroBatchFramework/BatchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string[]
try
{
var result = methodInfo.Invoke(instance, invokeArgs);
if (result is Task t)
switch (result)
{
await t;
case int exitCode:
Environment.ExitCode = exitCode;
break;
case Task<int> taskWithExitCode:
Environment.ExitCode = await taskWithExitCode;
break;
case Task task:
await task;
break;
}
}
catch (Exception ex)
Expand Down
3 changes: 3 additions & 0 deletions tests/MicroBatchFramework.Tests/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
114 changes: 114 additions & 0 deletions tests/MicroBatchFramework.Tests/ExitCodeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using FluentAssertions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace MicroBatchFramework.Tests
{
public class ExitCodeTest
{
public class ExitCodeTestBatch : BatchBase
{
[Command(nameof(NoExitCode))]
public void NoExitCode()
{
}

[Command(nameof(NoExitCodeException))]
public void NoExitCodeException()
{
throw new Exception();
}

[Command(nameof(NoExitCodeWithTask))]
public Task NoExitCodeWithTask()
{
return Task.CompletedTask;
}

[Command(nameof(ExitCode))]
public int ExitCode()
{
return 12345;
}

[Command(nameof(ExitCodeException))]
public int ExitCodeException()
{
throw new Exception();
}

[Command(nameof(ExitCodeWithTask))]
public Task<int> ExitCodeWithTask()
{
return Task.FromResult(54321);
}

[Command(nameof(ExitCodeWithTaskException))]
public Task<int> ExitCodeWithTaskException()
{
throw new Exception();
}
}

[Fact]
public async Task NoExitCode()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCode) });
Assert.Equal(0, Environment.ExitCode);
}

[Fact]
public async Task NoExitCodeWithTask()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCodeWithTask) });
Assert.Equal(0, Environment.ExitCode);
}

[Fact]
public async Task NoExitCodeException()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(NoExitCodeException) });
Assert.Equal(1, Environment.ExitCode);
}

[Fact]
public async Task ExitCode()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCode) });
Assert.Equal(12345, Environment.ExitCode);
}

[Fact]
public async Task ExitCodeException()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeException) });
Assert.Equal(1, Environment.ExitCode);
}

[Fact]
public async Task ExitCodeWithTask()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeWithTask) });
Assert.Equal(54321, Environment.ExitCode);
}

[Fact]
public async Task ExitCodeWithTaskException()
{
Environment.ExitCode = 0;
await new HostBuilder().RunBatchEngineAsync<ExitCodeTestBatch>(new[] { nameof(ExitCodeWithTaskException) });
Assert.Equal(1, Environment.ExitCode);
}

}
}

0 comments on commit 8262211

Please sign in to comment.