fix(simmy): return null from FaultGenerator when no fault is generated

FaultGenerator's implicit conversion dereferenced the nullable Outcome
returned by the underlying generator via `!.Value`, throwing
InvalidOperationException ("Nullable object must have a value.") when the
generator produced no outcome - an empty generator, or registered weights
summing to zero. ChaosFaultStrategy treats a null fault as "do not inject"
and only catches OperationCanceledException, so the exception escaped and
failed the execution the strategy was meant to leave untouched.

Propagate the nullable outcome with `?.Exception`, mirroring
OutcomeGenerator<TResult>, so no fault is injected when none is generated.
This commit is contained in:
dualfroz committed 2026-09-02 11:43:55 +02:00
1 parent 7121d2323f
commit fa219d839e
2 files changed
+19 -1

No files matched your search

+1 -1
View File
@@ -78,6 +78,6 @@ public sealed class FaultGenerator
var generatorDelegate = generator._helper.CreateGenerator();
return args => new ValueTask<Exception?>(generatorDelegate(args.Context)!.Value.Exception);
return args => new ValueTask<Exception?>(generatorDelegate(args.Context)?.Exception);
}
}
@@ -39,6 +39,24 @@ public class FaultGeneratorTests
Generate(generator).ShouldBeOfType<InvalidOperationException>();
}
[Fact]
public void NoExceptionRegistered_ShouldReturnNull()
{
var generator = new FaultGenerator();
Generate(generator).ShouldBeNull();
}
[Fact]
public void AllWeightsZero_ShouldReturnNull()
{
var generator = new FaultGenerator();
generator.AddException<InvalidOperationException>(weight: 0);
Generate(generator).ShouldBeNull();
}
private static Exception? Generate(FaultGenerator generator)
{
Func<FaultGeneratorArguments, ValueTask<Exception?>> func = generator;