public async Task<IHttpActionResult> Phoenix()
{
var all = Global.Cache.PhoenixFireCage.ToList();
var items = new List<CacheItemStatus>();
foreach (var p in all)
{
if (p.Key == null || p.Value == null)
{
continue;
}
var cacheItem = p.Value._info;
object unknownCacheObject = null;
var asyncStore = _cacheStoreProvider.GetAsyncCacheStore(p.Value._info.StoreId);
if (asyncStore != null)
{
unknownCacheObject = await asyncStore.GetAsync(p.Key);
if (unknownCacheObject is CacheItem)
{
cacheItem = unknownCacheObject as CacheItem;
}
}
var status = cacheItem != null ? new CacheItemStatus(cacheItem) : new CacheItemStatus(unknownCacheObject);
status.Key = p.Key;
status.PhoenixStatus = p.Value.GetCurrentState().GetState();
status.Type = p.Value.GetType().Name;
if (cacheItem == p.Value._info)
{
status = status.CacheItemNotFound();
}
items.Add(status);
}
return Json(items,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented
});
}
public async Task Phoenix_action_should_display_all_phoenix_in_cache() { Global.Cache.PhoenixFireCage.Add("item1", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem { Data = "data", Key = "item1" })); Global.Cache.PhoenixFireCage.Add("item2", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem { Data = new MediaTypeHeaderValue("text/json"), Key = "item2" })); Global.Cache.PhoenixFireCage.Add("item5", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem())); Global.Cache.PhoenixFireCage.Add("item6", Substitute.For<Phoenix>(Substitute.For<_IInvocation>(), new CacheItem())); var syncStore = Substitute.For<ICacheStore>(); syncStore.Get(Arg.Any<string>()).Returns(c => new CacheItem { Key = c.Arg<string>(), Data = "data" }); var asyncStore = Substitute.For<IAsyncCacheStore>(); asyncStore.GetAsync(Arg.Any<string>()).Returns(c => { object obj = new WebApiCacheItem { Key = c.Arg<string>(), Content = new byte[0] }; return Task.FromResult(obj); }); var provider = Substitute.For<ICacheStoreProvider>(); provider.GetCacheStore(Arg.Any<int>()).Returns(syncStore); provider.GetAsyncCacheStore(Arg.Any<int>()).Returns(asyncStore); var controller = new FlatwhiteStatusController(provider); // Action var result = await controller.Phoenix(); var jsonResult = (JsonResult<List<FlatwhiteStatusController.CacheItemStatus>>)result; // Assert Assert.AreEqual(4, jsonResult.Content.Count); }