예제 #1
0
 public void Invoke()
 {
     if (_random.Intn(100) < _value)
     {
         throw new Exception("Nemesis exception ヽ(`Д´)ノ");
     }
 }
예제 #2
0
        /// <summary>
        /// Calculate returns a random duration of time in the
        /// range [0, 2^(backoffLevel-1) * <see cref="IBackoffConfig.BackoffMultiplier"/>).
        /// </summary>
        /// <param name="backoffConfig">Read only configuration values related to backoff.</param>
        /// <param name="backoffLevel">
        ///     The backoff level (>= 1) used to calculate backoff duration.
        ///     <paramref name="backoffLevel"/> increases/decreases with successive failures/successes.
        /// </param>
        /// <returns>A <see cref="BackoffCalculation"/> object with the backoff duration and whether to increase
        ///          the backoff level.</returns>
        public BackoffCalculation Calculate(IBackoffConfig backoffConfig, int backoffLevel)
        {
            rngOnce.Do(() =>
            {
                // lazily initialize the RNG
                if (rng != null)
                {
                    return;
                }
                rng = new RNGCryptoServiceProvider();
            }
                       );

            var backoffDuration = new TimeSpan(backoffConfig.BackoffMultiplier.Ticks *
                                               (long)Math.Pow(2, backoffLevel - 1));

            int maxBackoffMilliseconds = (int)backoffDuration.TotalMilliseconds;
            int backoffMilliseconds    = maxBackoffMilliseconds == 0 ? 0 : rng.Intn(maxBackoffMilliseconds);

            return(new BackoffCalculation
            {
                Duration = TimeSpan.FromMilliseconds(backoffMilliseconds),
                IncreaseBackoffLevel = backoffDuration < backoffConfig.MaxBackoffDuration
            });
        }
예제 #3
0
 public void IntnRange()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => _rng.Intn(0));
     Assert.Throws <ArgumentOutOfRangeException>(() => _rng.Intn(-1));
 }