c# - How to unit test the heartbeat pattern? -
i writing client sends "heartbeat signal" server every second. client calls wcf service in background thread report activity.
how unit test this? need wait couple of seconds , check if appropriate method called several times?
should sort of scenario test instead? maybe shouldn't worried calling service continuously through whole clients lifetime cycle?
i can test single call wcf service doesn't test "heartbeat pattern".
i using tdd approach. (c#, nunit, moq)
any suggestions or examples?
edit:
i think wasn't clear enough.
this simpler version of 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 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 2 questions:
1) why test fails? doing wrong?
2) should test @ all?
the functionality wish test must first isolated. example in case, there 2 aspects tested. 1 hearbeat component sends heartbeat message on designated schedule. other service receives message. if abstract service, can test hearbeat component in isolation service implementation. can done in unit test starting hearbeat component, sleeping, verifying expected number of messages received stub or mock service implementation. test ensures service receiving message integration test , not "pure" unit test. however, since must tested anyway, can have test case well.
Comments
Post a Comment