title: Testing with MemoryCache UpdateCallback
body: <p>The following test fails intermittently. It caches an item in MemoryCache with an absolute expiration time, and an update callback that should be called before the item is removed. However sometimes the callback is invoked before the test finishes, and sometimes not at all.</p>
  
  <p>With a large enough buffer time it will always be invoked at least once. But that does not serve my purposes, since I require that the cache always attempts to update the data before it expired.</p>
  
  <p>Now in my real world scenario I will not have 10 second expiration time and granularity, but it still bothers me that this test fails intermittently.</p>
  
  <p>Anyone have thoughts on why this is happening?</p>
  
  <p>Note: Also intermittently fails with 60 second expiry and 5 second buffer.</p>
  
  <pre><code>using System;
  using System.Runtime.Caching;
  using System.Threading.Tasks;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  
  [TestClass]
  public class MemoryCacheTest
  {
      private const double ExpiryInSeconds = 10;
      private const double ExpiryBufferInSeconds = 5;
  
      private readonly object updateItemCounterLock = new object();
      private int updateItemCounter = 0;
  
      [TestMethod]
      public async Task MemoryCacheUpdateTest()
      {
          // Set item in cache with absolute expiration defined above
          MemoryCache cache = MemoryCache.Default;
          CacheItem cacheItem = new CacheItem("key", "value");
          CacheItemPolicy cacheItemPolicy = new CacheItemPolicy
          {
              AbsoluteExpiration = DateTimeOffset.Now + TimeSpan.FromSeconds(ExpiryInSeconds),
              UpdateCallback = new CacheEntryUpdateCallback(this.UpdateItem)
          };
          cache.Set(cacheItem, cacheItemPolicy);
  
          // Delay for absolute expiration time + buffer
          await Task.Delay(TimeSpan.FromSeconds(ExpiryInSeconds) + TimeSpan.FromSeconds(ExpiryBufferInSeconds));
  
          // Test that the update callback was invoked once
          Assert.AreEqual(1, updateItemCounter);
      }
  
      // Incrememnts the updateItemCounter
      private void UpdateItem(CacheEntryUpdateArguments args)
      {
          lock (updateItemCounterLock)
          {
              updateItemCounter++;
          }
      }
  }
  </code></pre>