How to unit test heart rate pattern?

I am writing a client that sends a “server signal” to the server every second. The client calls the WCF service in the background thread to report on its activity.

How unit test is this? I need to wait a couple of seconds and check if the corresponding method is called several times?

Could this be some kind of scenario? Maybe I should not worry about the continuous call of the service throughout the life cycle of clients?

I can test one call to the WCF service, but it does not test the "heart rate pattern".

I am using the TDD approach. (C #, NUnit, Moq)

Any suggestions or examples?

EDIT:

I think this was not clear enough.

This is a much simpler version of what I have:

public class FeedService
{
   private Timer t;

   public FeedService()
   {
      t.Interval = 1000;
      t.Elapsed += TimerElapsed;
      t.Start();
   }

   private void TimerElapsed(object sender, ElapsedEventArgs e)
   {
      t.Stop();
      SendHeartbeat();
      t.Start();
   }
}

... and this is my test:

[Test]
public void Heartbeat_called_twice_after_2_seconds()
{
  var mockFeedService = new Mock<FeedService>();

  Thread.Sleep(2000);
  mockFeedService.Verify(x => x.SendHeartBeat(), Times.AtLeast(2));
}

:
1) ? ?
2) ?

+3
3

, , . , . -, listenbeat . - . , listenbeat . unit test, listenbeat, , , . , , , "" unit test. , , .

+5

"". , , .

, Morten

+2

The scenario you want to test is , strictly speaking, an integration test.

The approach that I applied to similar test building scenarios is to use this convenient function:

/// <summary>
/// Wait no longer than @waitNoLongerThanMillis for @thatWhatWeAreWaitingFor to return true.
/// Tests every second for the 
/// </summary>
/// <param name="thatWhatWeAreWaitingFor">Function that when evaluated returns true if the state we are waiting for has been reached.</param>
/// <param name="waitNoLongerThanMillis">Max time to wait in milliseconds</param>
/// <param name="checkEveryMillis">How often to check for @thatWhatWeAreWaitingFor</param>
/// <returns></returns>
private bool WaitFor(Func<bool> thatWhatWeAreWaitingFor, int checkEveryMillis, int waitNoLongerThanMillis)
{
    var waitedFor = 0;
    while (waitedFor < waitNoLongerThanMillis)
    {
        if (thatWhatWeAreWaitingFor()) return true;

        Console.WriteLine("Waiting another {0}ms for a situation to occur.  Giving up in {1}ms ...", checkEveryMillis, (waitNoLongerThanMillis - waitedFor));
        Thread.Sleep(checkEveryMillis);
        waitedFor += checkEveryMillis;
    }
    return false;
}

Using:

// WaitFor (transaction to become failed, checkEverySoOften, waitNoLongerThan)
int wait = (Settings.EventHandlerCoordinatorNoActivitySleepTime + 5) * 1000;
var failedEventExists = WaitFor(() => EventQueueManager.GetFailedEvents(0, 10).TotalRecords > 0, checkEveryMillis: 1000, waitNoLongerThanMillis: wait);

if (!failedEventExists)
    Assert.Fail("Waited longer than " + wait + " without any evidence of the event having been handled.  Expected to failed event on the queue.");

It uses C # features that are only available with .Net4, but this probably suits most .Net developers these days.

0
source

Source: https://habr.com/ru/post/1795276/


All Articles