public void ExceptionsShouldBubbleAllTheWay_NoArgVersion2() { Func <string> exceptionInvocationFunc = delegate { throw new ArgumentException("Aiii!"); }; IMemoizer <string> memoizer = exceptionInvocationFunc.CacheFor(1).Seconds.GetMemoizer(); memoizer.Invoke(); }
public void NoArgumentsMemoizer() { Func <string> expensiveNoArgsInvocationFunc = delegate { Console.WriteLine("TypicalReferenceDataStaticInvocation invoked..."); Thread.Sleep(1345); Console.WriteLine("TypicalReferenceDataStaticInvocation returns..."); return("Some no-arguments-one-time-only-invocation data stuff"); }; IMemoizer <string> memoizer = expensiveNoArgsInvocationFunc.CacheFor(1).Seconds.GetMemoizer(); // Cached-for-1-second memoizer long startTime = DateTime.Now.Ticks; string retVal = memoizer.Invoke(); long durationInTicks = DateTime.Now.Ticks - startTime; long durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(1345)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); startTime = DateTime.Now.Ticks; retVal = memoizer.Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); // Cached-for-1-second-on-the-fly function - should be cached already startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CacheFor(1).Seconds.GetMemoizer().Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); // Function only - not yet cached startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CachedInvoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(1345)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CachedInvoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); expensiveNoArgsInvocationFunc.RemoveFromCache(); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CachedInvoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(1345)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CachedInvoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); expensiveNoArgsInvocationFunc.UnMemoize(); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CacheFor(1).Seconds.GetMemoizer().Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(1345)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CacheFor(1).Seconds.GetMemoizer().Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); Thread.Sleep(1000); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CacheFor(55).Seconds.GetMemoizer().Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.GreaterThanOrEqualTo(1345)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); startTime = DateTime.Now.Ticks; retVal = expensiveNoArgsInvocationFunc.CacheFor(55).Seconds.GetMemoizer().Invoke(); durationInTicks = DateTime.Now.Ticks - startTime; durationInMilliseconds = durationInTicks / TimeSpan.TicksPerMillisecond; Assert.That(retVal, Is.EqualTo("Some no-arguments-one-time-only-invocation data stuff")); Assert.That(durationInMilliseconds, Is.LessThan(5)); // ms Console.WriteLine("One-time-invocation took " + durationInMilliseconds + " ms | " + durationInTicks + " ticks"); }