randomFaults function
A FaultPolicy that drops, delays and crashes at random.
drop, delay and crash are probabilities per delivery and must sum to
at most 1. A delayed message waits a random time up to maxDelay.
where limits the faults to some deliveries.
Implementation
FaultPolicy randomFaults({
double drop = 0,
double delay = 0,
double crash = 0,
Duration maxDelay = const Duration(milliseconds: 100),
bool Function(Delivery delivery)? where,
}) {
if (drop < 0 || delay < 0 || crash < 0 || drop + delay + crash > 1) {
throw ArgumentError('Probabilities must be in 0..1 and sum to at most 1');
}
return (delivery) {
if (where != null && !where(delivery)) return Fault.none;
final roll = delivery.random.nextDouble();
if (roll < drop) return Fault.drop;
if (roll < drop + crash) return Fault.crash;
if (roll < drop + crash + delay) {
return Fault.delay(Duration(
microseconds:
1 + delivery.random.nextInt(max(1, maxDelay.inMicroseconds))));
}
return Fault.none;
};
}