Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:14:33+00:00 2026-06-14T23:14:33+00:00

In Console application, I can successfully get a call back from cache cluster in

  • 0

In Console application, I can successfully get a call back from cache cluster in case an item is added or removed from the cache.

How can I execute the same test in Test Driven environment. When I write the test case, I do not get the OnCacheChange to fire..

Please help.

Thanks..

Following code works in Console application…

    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.");
    }

Following TestMethod is not able to fire the OnCacheChange event.

    [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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:14:35+00:00Added an answer on June 14, 2026 at 11:14 pm

    It’s not clear from the scope of your question that the test callback is actually touching _eventFired. This should work, however:

    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);
    

    Above I wrote the callback inline as a lambda, so that it’s clear the _eventFired gets updated on the callback. I also used a ManualResetEvent, so that the test thread proceeds when the callback is fired, rather than waiting a hard-coded 10 seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know with .NET we can host wcf service from a console application without
I have a console application from which I create a window. I can render
In C++, a console application can have a message handler in its WinMain procedure,
I have a console application which can also open winform under certain conditions.Processing(its a
I've written a small console application that can perform certain tasks. The user interface
I'm programming a console application with Java that can be used to monitor a
How can I pipe between two separate console application running in different console windows?
I want to make an console application of c which can run other applications
If I have a console application, is there any way I can handle the
I have written a little console application that uses s#arp. I can create an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.