tst_grpc.qml 7.7 KB

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