在控制台应用程序中,如果从缓存中添加或删除项目,我可以成功地从缓存集群中获得回调。
如何在测试驱动环境中执行相同的测试。当我编写测试用例时,我没有触发 OnCacheChange ..
请帮忙。
谢谢..
以下代码适用于控制台应用程序...
static void Main(string[] args)
{
dtStart = DateTime.Now;
dtEnd = DateTime.MinValue;
string line;
var factory = new DataCacheFactory();
var key = "mycachekey1";
var cache = factory.GetCache("default");
var d = cache.AddCacheLevelCallback(DataCacheOperations.AddItem | DataCacheOperations.RemoveItem | DataCacheOperations.ReplaceItem, OnCacheChange);
Console.WriteLine("Cache Name = {0} : DelegateId = {1}",d.CacheName,d.DelegateId);
var obj = cache[key];
if (obj == null)
{
Console.WriteLine("data was not cached.");
Console.WriteLine("Enter your data to be cached.");
line = Console.ReadLine();
cache.Add(key, line);
}
Console.WriteLine("getting data from cache: {0}", cache[key]);
Console.WriteLine("type yes to remove data for cache key {0} : ", key);
line = Console.ReadLine();
if (line.ToString().Length > 0)
{
cache.Remove(key);
Console.WriteLine("cache removed");
Console.ReadLine();
}
}
static void OnCacheChange(string cacheName, string region, string key, DataCacheItemVersion itemVersion, DataCacheOperations operationId, DataCacheNotificationDescriptor notificationDescriptor)
{
dtEnd = DateTime.Now;
TimeSpan timeSpan = dtEnd - dtStart;
Console.WriteLine("Event to be fired in seconds = {0}", timeSpan.Seconds);
Console.WriteLine("A cache-level notification has been triggered. Following are the details:");
Console.WriteLine("Cache: " + cacheName);
Console.WriteLine("Region: " + region);
Console.WriteLine("Key: " + key);
Console.WriteLine("Operation: " + operationId.ToString());
Console.WriteLine("Notification Description: " + notificationDescriptor.DelegateId);
Console.WriteLine("Finished executing the cache notification callback.");
}
以下 TestMethod 无法触发 OnCacheChange 事件。
[TestMethod]
public void CacheCallBackFunctionTest()
{
_eventFired = false;
_startDateTime = DateTime.Now;
var key = "mycachekey2";
var cache = CachingManager.CacheFactory().GetCache("default");
cache.AddCacheLevelCallback(DataCacheOperations.AddItem | DataCacheOperations.RemoveItem | DataCacheOperations.ReplaceItem, OnCacheChange);
cache.Add(key, "my value 1");
cache.Remove(key);
System.Threading.Thread.Sleep(10000);
Assert.IsTrue(_eventFired);
}