clienttest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "testserviceclient.h"
  26. #include "http2channel.h"
  27. #include "qtprotobuf.h"
  28. #include <QTimer>
  29. #include <QCoreApplication>
  30. #include <gtest/gtest.h>
  31. using namespace qtprotobufnamespace::tests;
  32. using namespace qtprotobuf;
  33. class ClientTest : public ::testing::Test
  34. {
  35. protected:
  36. static void SetUpTestCase() {
  37. QtProtobuf::init();
  38. SimpleStringMessage::registerTypes();
  39. }
  40. };
  41. TEST_F(ClientTest, CheckMethodsGeneration)
  42. {
  43. //Dummy compile time check of functions generation and interface compatibility
  44. TestServiceClient testClient;
  45. SimpleStringMessage result;
  46. SimpleStringMessage request;
  47. testClient.testMethod(result, request);
  48. testClient.testMethod(result);
  49. testClient.testMethod(result, &testClient, [](AsyncReply*){});
  50. }
  51. TEST_F(ClientTest, StringEchoTest)
  52. {
  53. int argc = 0;
  54. QCoreApplication app(argc, nullptr);
  55. TestServiceClient testClient;
  56. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051));
  57. SimpleStringMessage result;
  58. SimpleStringMessage request;
  59. request.setTestFieldString("Hello beach!");
  60. ASSERT_TRUE(testClient.testMethod(request, result));
  61. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  62. }
  63. TEST_F(ClientTest, StringEchoAsyncTest)
  64. {
  65. int argc = 0;
  66. QCoreApplication app(argc, nullptr);
  67. TestServiceClient testClient;
  68. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051));
  69. SimpleStringMessage result;
  70. SimpleStringMessage request;
  71. request.setTestFieldString("Hello beach!");
  72. QEventLoop waiter;
  73. AsyncReply* reply = testClient.testMethod(request);
  74. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  75. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  76. result = reply->read<SimpleStringMessage>();
  77. }
  78. waiter.quit();
  79. });
  80. waiter.exec();
  81. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  82. }
  83. TEST_F(ClientTest, StringEchoAsync2Test)
  84. {
  85. int argc = 0;
  86. QCoreApplication app(argc, nullptr);
  87. TestServiceClient testClient;
  88. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051));
  89. SimpleStringMessage result;
  90. SimpleStringMessage request;
  91. request.setTestFieldString("Hello beach!");
  92. QEventLoop waiter;
  93. testClient.testMethod(request, &app, [&result, &waiter, &testClient](AsyncReply *reply) {
  94. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  95. result = reply->read<SimpleStringMessage>();
  96. }
  97. waiter.quit();
  98. });
  99. waiter.exec();
  100. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  101. }
  102. TEST_F(ClientTest, StringEchoAsyncAbortTest)
  103. {
  104. int argc = 0;
  105. QCoreApplication app(argc, nullptr);
  106. TestServiceClient testClient;
  107. testClient.attachChannel(std::make_shared<Http2Channel>("localhost", 50051));
  108. SimpleStringMessage result;
  109. SimpleStringMessage request;
  110. request.setTestFieldString("sleep");
  111. QEventLoop waiter;
  112. bool errorCalled = false;
  113. AsyncReply* reply = testClient.testMethod(request);
  114. result.setTestFieldString("Result not changed by echo");
  115. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  116. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  117. result = reply->read<SimpleStringMessage>();
  118. }
  119. waiter.quit();
  120. });
  121. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  122. errorCalled = true;
  123. });
  124. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  125. reply->abort();
  126. waiter.exec();
  127. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  128. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  129. ASSERT_TRUE(errorCalled);
  130. errorCalled = false;
  131. reply = testClient.testMethod(request);
  132. QObject::connect(reply, &AsyncReply::finished, &app, [reply, &result, &waiter, &testClient]() {
  133. if (testClient.lastError() == AbstractChannel::StatusCodes::Ok) {
  134. result = reply->read<SimpleStringMessage>();
  135. }
  136. waiter.quit();
  137. });
  138. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCodes){
  139. errorCalled = true;
  140. });
  141. QTimer::singleShot(2000, reply, &AsyncReply::abort);
  142. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  143. waiter.exec();
  144. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  145. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCodes::Aborted);
  146. ASSERT_TRUE(errorCalled);
  147. }