sync_client_ut.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <library/cpp/messagebus/test/helper/example.h>
  2. #include <library/cpp/messagebus/test/helper/object_count_check.h>
  3. namespace NBus {
  4. namespace NTest {
  5. using namespace std;
  6. ////////////////////////////////////////////////////////////////////
  7. /// \brief Client for sending synchronous message to local server
  8. struct TSyncClient {
  9. TNetAddr ServerAddr;
  10. TExampleProtocol Proto;
  11. TBusMessageQueuePtr Bus;
  12. TBusSyncClientSessionPtr Session;
  13. int NumReplies;
  14. int NumMessages;
  15. /// constructor creates instances of queue, protocol and session
  16. TSyncClient(const TNetAddr& serverAddr)
  17. : ServerAddr(serverAddr)
  18. {
  19. /// create or get instance of message queue, need one per application
  20. Bus = CreateMessageQueue();
  21. NumReplies = 0;
  22. NumMessages = 10;
  23. /// register source/client session
  24. TBusClientSessionConfig sessionConfig;
  25. Session = Bus->CreateSyncSource(&Proto, sessionConfig);
  26. Session->RegisterService("localhost");
  27. }
  28. ~TSyncClient() {
  29. Session->Shutdown();
  30. }
  31. /// dispatch of requests is done here
  32. void Work() {
  33. for (int i = 0; i < NumMessages; i++) {
  34. THolder<TExampleRequest> mess(new TExampleRequest(&Proto.RequestCount));
  35. EMessageStatus status;
  36. THolder<TBusMessage> reply(Session->SendSyncMessage(mess.Get(), status, &ServerAddr));
  37. if (!!reply) {
  38. NumReplies++;
  39. }
  40. }
  41. }
  42. };
  43. Y_UNIT_TEST_SUITE(SyncClientTest) {
  44. Y_UNIT_TEST(TestSync) {
  45. TObjectCountCheck objectCountCheck;
  46. TExampleServer server;
  47. TSyncClient client(server.GetActualListenAddr());
  48. client.Work();
  49. // assert correct number of replies
  50. UNIT_ASSERT_EQUAL(client.NumReplies, client.NumMessages);
  51. // assert that there is no message left in flight
  52. UNIT_ASSERT_EQUAL(server.Session->GetInFlight(), 0);
  53. UNIT_ASSERT_EQUAL(client.Session->GetInFlight(), 0);
  54. }
  55. }
  56. }
  57. }