tst_grpc.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 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. import QtQuick 2.12
  26. import QtTest 1.0
  27. import QtProtobuf 0.4
  28. import QtGrpc 0.4
  29. import qtprotobufnamespace.tests 1.0
  30. TestCase {
  31. name: "gRPC client qml test"
  32. SimpleStringMessage {
  33. id: stringMsg
  34. testFieldString: "Test string"
  35. }
  36. SimpleIntMessage {
  37. id: intMsg
  38. testField: 1024
  39. }
  40. GrpcSubscription {
  41. id: serverStreamSubscription
  42. property bool ok: true
  43. property int updateCount: 0
  44. enabled: false
  45. client: TestServiceClient
  46. method: "testMethodServerStream"
  47. argument: stringMsg
  48. onUpdated: {
  49. ++updateCount;
  50. ok = ok && value.testFieldString === "Test string" + updateCount
  51. }
  52. onError: {
  53. console.log("Subscription error: " + status.code + " " + status.message)
  54. ok = false;
  55. }
  56. }
  57. GrpcSubscription {
  58. id: serverStreamCancelSubscription
  59. property bool ok: true
  60. property int updateCount: 0
  61. enabled: false
  62. client: TestServiceClient
  63. method: "testMethodServerStream"
  64. argument: stringMsg
  65. onUpdated: {
  66. ++updateCount;
  67. ok = ok && value.testFieldString === "Test string" + updateCount
  68. if (updateCount === 3) {
  69. serverStreamCancelSubscription.enabled = false;
  70. }
  71. }
  72. onError: {
  73. console.log("Subscription error: " + status.code + " " + status.message)
  74. ok = false;
  75. }
  76. }
  77. GrpcSubscription {
  78. id: serverStreamInvalidSubscription
  79. property bool ok: false
  80. enabled: false
  81. client: TestServiceClient
  82. method: "testMethodServerStreamNotExist"
  83. argument: stringMsg
  84. onUpdated: {
  85. ok = false;
  86. }
  87. onError: {
  88. ok = status.code === GrpcStatus.Unimplemented;
  89. }
  90. }
  91. Loader {
  92. id: subscriptionLoader
  93. active: false
  94. sourceComponent: Component {
  95. GrpcSubscription {
  96. property bool ok: true
  97. property int updateCount: 0
  98. enabled: true
  99. client: TestServiceClient
  100. method: "testMethodServerStream"
  101. argument: stringMsg
  102. onUpdated: {
  103. ++updateCount;
  104. ok = ok && value.testFieldString === ("Test string" + updateCount)
  105. }
  106. onError: {
  107. console.log("Subscription error: " + status.code + " " + status.message)
  108. ok = false;
  109. }
  110. }
  111. }
  112. }
  113. function test_stringEchoTest() {
  114. var called = false;
  115. var errorCalled = false;
  116. TestServiceClient.testMethod(stringMsg, function(result) {
  117. called = result.testFieldString === stringMsg.testFieldString;
  118. }, function(status) {
  119. errorCalled = true
  120. })
  121. wait(300)
  122. compare(called && !errorCalled, true, "testMethod was not called proper way")
  123. }
  124. function test_statusTest() {
  125. var called = false;
  126. var errorCalled = false;
  127. TestServiceClient.testMethodStatusMessage(stringMsg, function(result) {
  128. called = true;
  129. }, function(status) {
  130. errorCalled = status.code === GrpcStatus.Unimplemented && status.message === stringMsg.testFieldString;
  131. })
  132. wait(300)
  133. compare(!called && errorCalled, true, "testMethodStatusMessage was not called proper way")
  134. }
  135. function test_stringEchoAsyncTest() {
  136. var called = false;
  137. var errorCalled = false;
  138. stringMsg.testFieldString = "sleep";
  139. TestServiceClient.testMethod(stringMsg, function(result) {
  140. called = result.testFieldString === stringMsg.testFieldString;
  141. }, function(status) {
  142. errorCalled = true
  143. })
  144. wait(300)
  145. compare(!called && !errorCalled, true, "testMethod was not called proper way")
  146. wait(1000)
  147. compare(called && !errorCalled, true, "testMethod was not called proper way")
  148. stringMsg.testFieldString = "Test string";
  149. }
  150. function test_serverStreamSubscription() {
  151. serverStreamSubscription.enabled = true;
  152. wait(20000);
  153. compare(serverStreamSubscription.ok, true, "Subscription data failed")
  154. compare(serverStreamSubscription.updateCount, 4, "Subscription failed, update was not called right amount times")
  155. }
  156. function test_serverStreamCancelSubscription() {
  157. serverStreamCancelSubscription.enabled = true;
  158. wait(20000);
  159. compare(serverStreamCancelSubscription.ok, true, "Subscription data failed")
  160. compare(serverStreamCancelSubscription.updateCount, 3, "Subscription failed, update was not called right amount times")
  161. }
  162. function test_serverStreamInvalidSubscription() {
  163. serverStreamInvalidSubscription.enabled = true;
  164. wait(500);
  165. compare(serverStreamInvalidSubscription.ok, true, "Subscription data failed")
  166. compare(serverStreamInvalidSubscription.enabled, false, "Subscription data failed")
  167. }
  168. function test_nonCompatibleArgRet() {
  169. var called = false;
  170. var errorCalled = false;
  171. TestServiceClient.testMethodNonCompatibleArgRet(intMsg, function(result) {
  172. called = result.testFieldString === "1024";
  173. }, function(status) {
  174. errorCalled = true
  175. })
  176. wait(300)
  177. compare(called && !errorCalled, true, "testMethodNonCompatibleArgRet was not called proper way")
  178. }
  179. function test_1loader() {//This test has to be called first to fail other tests in case if it fails
  180. subscriptionLoader.active = true;
  181. wait(1500);
  182. compare(subscriptionLoader.item.ok, true, "Subscription data failed")
  183. compare(subscriptionLoader.item.updateCount, 1, "Subscription failed, update was not called right amount times")
  184. subscriptionLoader.active = false;
  185. }
  186. }