Skip to content

Commit

Permalink
Add test showing exception when nullable parameter is called with nul…
Browse files Browse the repository at this point in the history
…l argument
  • Loading branch information
khellang committed May 2, 2024
1 parent 444abf5 commit f97f7e9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public int Increment()
return countingService.Increment();
}

public int Increment(int? number)
{
return countingService.Increment(number);
}

public int GetCurrentValue()
{
return countingService.GetCurrentValue();
Expand Down
21 changes: 20 additions & 1 deletion source/Halibut.TestUtils.Contracts/CountingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ public class CountingService : ICountingService
int count;
public int Increment()
{
return Interlocked.Increment(ref count);
return Increment(1);
}

public int Increment(int? number)
{
var increment = number ?? 1;
var counter = 0;

for (var i = 0; i < increment; i++)
{
counter = Interlocked.Increment(ref count);
}

return counter;
}

public int GetCurrentValue()
Expand All @@ -26,6 +39,12 @@ public async Task<int> IncrementAsync(CancellationToken cancellationToken)
return service.Increment();
}

public async Task<int> IncrementAsync(int? number, CancellationToken cancellationToken)
{
await Task.CompletedTask;
return service.Increment(number);
}

public async Task<int> GetCurrentValueAsync(CancellationToken cancellationToken)
{
await Task.CompletedTask;
Expand Down
2 changes: 2 additions & 0 deletions source/Halibut.TestUtils.Contracts/ICountingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace Halibut.TestUtils.Contracts
public interface ICountingService
{
public int Increment();
public int Increment(int? number);
public int GetCurrentValue();
}

public interface IAsyncCountingService
{
public Task<int> IncrementAsync(CancellationToken cancellationToken);
public Task<int> IncrementAsync(int? number, CancellationToken cancellationToken);
public Task<int> GetCurrentValueAsync(CancellationToken cancellationToken);
}
}
20 changes: 20 additions & 0 deletions source/Halibut.Tests/ServiceModel/ServiceInvokerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ public async Task AsyncInvokeWithNoParamsOnAsyncService()
response.Result.Should().Be(1);
}

[Test]
public async Task AsyncInvokeWithNullableParamsOnAsyncService()
{
var serviceFactory = new ServiceFactoryBuilder()
.WithConventionVerificationDisabled()
.WithService<ICountingService, IAsyncCountingService>(() => new AsyncCountingService())
.Build();

var sut = new ServiceInvoker(serviceFactory);
var request = new RequestMessage()
{
ServiceName = nameof(ICountingService),
MethodName = nameof(ICountingService.Increment),
Params = new object[] { null! },
};

var response = await sut.InvokeAsync(request);
response.Result.Should().Be(1);
}

[Test]
public async Task AsyncInvokeWithNoParams_AsyncServiceMissingSuffix()
{
Expand Down

0 comments on commit f97f7e9

Please sign in to comment.