【问题标题】:Test case to simulate cache call back for AppFabric为 AppFabric 模拟缓存回调的测试用例
【发布时间】:2012-11-13 17:54:42
【问题描述】:

在控制台应用程序中,我可以成功从缓存集群中获得回调,以防在缓存中添加或删除项目。

如何在测试驱动环境中执行相同的测试。 当我编写测试用例时,我没有触发 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);
    }

【问题讨论】:

  • 您遇到了什么错误/问题?是编译时错误,还是其他原因?
  • 除了在测试用例中永远不会触发 OnCacheChange 之外,我没有收到任何错误。但是,在控制台应用程序中,当我让控制台应用程序运行时,OnCacheChange 会在 10 秒后被触发左右...
  • 我假设您已经尝试将超时时间增加到 20 秒,确定吗?
  • 更新最后几行代码如下: Assert.AreEqual("my value 1", cache.Get(key1)); if (!reset.WaitOne(TimeSpan.FromSeconds(100))) { throw new Exception("10 秒后没有收到缓存回调"); } System.Threading.Thread.Sleep(60000); Assert.IsTrue(eventFired);
  • 我正在运行 4 分钟 240 秒的测试,看看是否可行。可能是我的集群服务器响应缓慢。

标签: c# testdriven.net appfabric-cache


【解决方案1】:

从您的问题范围来看,测试回调实际上是否触及_eventFired 尚不清楚。但是,这应该可行:

bool eventFired = false;
var reset = new System.Threading.ManualResetEvent(true);
cache.AddCacheLevelCallback(DataCacheOperations.AddItem | DataCacheOperations.RemoveItem,
    (cacheName, region, key, itemVersion, operationId, notificationDescriptor) => {
        eventFired = true;
        reset.Set();
    });

cache.Add(key, "my value 1");
if (!reset.WaitOne(TimeSpan.FromSeconds(10)))
{
    throw new Exception("Didn't receive cache callback after 10 seconds");
}
Assert.IsTrue(eventFired);

上面我将回调内联编写为 lambda,因此很明显 _eventFired 在回调中得到更新。我还使用了ManualResetEvent,以便在触发回调时继续测试线程,而不是等待硬编码的 10 秒。

【讨论】:

  • dbaseman 当我从缓存中添加/删除项目时,回调仍然没有被触发。
猜你喜欢
  • 2021-04-02
  • 2011-02-08
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多