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 4003002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:05:57+00:00 2026-05-20T08:05:57+00:00

I am writing a client which sends a heartbeat signal to the server every

  • 0

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

How to unit test this? Do I need to wait for a couple of seconds and check if appropriate method was called several times?

Should it be some sort of scenario test instead? Maybe I shouldn’t be worried about calling the service continuously through the whole clients lifetime cycle?

I can test a single call to WCF service but it doesn’t test the “heartbeat pattern”.

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

Any suggestions or examples?

EDIT:

I think it wasn’t 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));
}

I have a two questions:
1) Why my test always fails? What I am doing wrong?
2) Should I test it at all?

  • 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-05-20T08:05:58+00:00Added an answer on May 20, 2026 at 8:05 am

    The functionality that you wish to test must be first isolated. For example in this case, there are a two aspects that could be tested. One is that the hearbeat component actually sends a heartbeat message on the designated schedule. The other is that the service receives the message. If you abstract the service, then you can test the hearbeat component in isolation from the service implementation. This can be done in a unit test by starting the hearbeat component, then sleeping, then verifying that the expected number of messages were received by the stub or mock service implementation. The test that ensures the service is receiving the message is an integration test and so is a not a “pure” unit test. However, since it must be tested anyway, you can have a test case for this as well.

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

Sidebar

Related Questions

I've been writing a background service which enforces my client's security policies on company
I'm writing a client-server app, in which the client has a determined memory address
Writing a client application that sends images to a server via a webservice. As
I am writing a client - server system in which I used NON-BLOCKING sockets.
I am writing a pyQt client-server application which restarts/shutdowns PCs remotely. The receivers are
I'm writing a client/server application in which, client is written in C++ and the
I'm writing a PHP client to a C#/WCF web service. The parameters for some
I am writing a web service. I am also writing a web client which
I'm writing my first java client/server program which just establishes a connection with the
I am writing an enterprise client/server application. The client is an iOS app which

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.