tst_grpc.qml 7.7 KB

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