tst_grpc.qml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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. function test_stringEchoTest() {
  96. var called = false;
  97. var errorCalled = false;
  98. TestServiceClient.testMethod(stringMsg, function(result) {
  99. called = result.testFieldString === stringMsg.testFieldString;
  100. }, function(status) {
  101. errorCalled = true
  102. })
  103. wait(300)
  104. compare(called && !errorCalled, true, "testMethod was not called proper way")
  105. }
  106. function test_statusTest() {
  107. var called = false;
  108. var errorCalled = false;
  109. TestServiceClient.testMethodStatusMessage(stringMsg, function(result) {
  110. called = true;
  111. }, function(status) {
  112. errorCalled = status.code === GrpcStatus.Unimplemented && status.message === stringMsg.testFieldString;
  113. })
  114. wait(300)
  115. compare(!called && errorCalled, true, "testMethodStatusMessage was not called proper way")
  116. }
  117. function test_stringEchoAsyncTest() {
  118. var called = false;
  119. var errorCalled = false;
  120. stringMsg.testFieldString = "sleep";
  121. TestServiceClient.testMethod(stringMsg, function(result) {
  122. called = result.testFieldString === stringMsg.testFieldString;
  123. }, function(status) {
  124. errorCalled = true
  125. })
  126. wait(300)
  127. compare(!called && !errorCalled, true, "testMethod was not called proper way")
  128. wait(1000)
  129. compare(called && !errorCalled, true, "testMethod was not called proper way")
  130. stringMsg.testFieldString = "Test string";
  131. }
  132. function test_serverStreamSubscription() {
  133. serverStreamSubscription.enabled = true;
  134. wait(20000);
  135. compare(serverStreamSubscription.ok, true, "Subscription data failed")
  136. compare(serverStreamSubscription.updateCount, 4, "Subscription failed, update was not called right amount times")
  137. }
  138. function test_serverStreamCancelSubscription() {
  139. serverStreamCancelSubscription.enabled = true;
  140. wait(20000);
  141. compare(serverStreamCancelSubscription.ok, true, "Subscription data failed")
  142. compare(serverStreamCancelSubscription.updateCount, 3, "Subscription failed, update was not called right amount times")
  143. }
  144. function test_serverStreamInvalidSubscription() {
  145. serverStreamInvalidSubscription.enabled = true;
  146. wait(500);
  147. compare(serverStreamInvalidSubscription.ok, true, "Subscription data failed")
  148. compare(serverStreamInvalidSubscription.enabled, false, "Subscription data failed")
  149. }
  150. function test_nonCompatibleArgRet() {
  151. var called = false;
  152. var errorCalled = false;
  153. TestServiceClient.testMethodNonCompatibleArgRet(intMsg, function(result) {
  154. called = result.testFieldString === "1024";
  155. }, function(status) {
  156. errorCalled = true
  157. })
  158. wait(300)
  159. compare(called && !errorCalled, true, "testMethodNonCompatibleArgRet was not called proper way")
  160. }
  161. }