tst_grpc.qml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. QtObject {
  37. id: returnMsg
  38. property string ret: serverStreamSubscription.returnValue.testFieldString
  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) && returnMsg.ret == ("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. function test_stringEchoTest() {
  92. var called = false;
  93. var errorCalled = false;
  94. TestServiceClient.testMethod(stringMsg, function(result) {
  95. called = result.testFieldString === stringMsg.testFieldString;
  96. }, function(status) {
  97. errorCalled = true
  98. })
  99. wait(300)
  100. compare(called && !errorCalled, true, "testMethod was not called proper way")
  101. }
  102. function test_statusTest() {
  103. var called = false;
  104. var errorCalled = false;
  105. TestServiceClient.testMethodStatusMessage(stringMsg, function(result) {
  106. called = true;
  107. }, function(status) {
  108. errorCalled = status.code === GrpcStatus.Unimplemented && status.message === stringMsg.testFieldString;
  109. })
  110. wait(300)
  111. compare(!called && errorCalled, true, "testMethodStatusMessage was not called proper way")
  112. }
  113. function test_stringEchoAsyncTest() {
  114. var called = false;
  115. var errorCalled = false;
  116. stringMsg.testFieldString = "sleep";
  117. TestServiceClient.testMethod(stringMsg, function(result) {
  118. called = result.testFieldString === stringMsg.testFieldString;
  119. }, function(status) {
  120. errorCalled = true
  121. })
  122. wait(300)
  123. compare(!called && !errorCalled, true, "testMethod was not called proper way")
  124. wait(1000)
  125. compare(called && !errorCalled, true, "testMethod was not called proper way")
  126. stringMsg.testFieldString = "Test string";
  127. }
  128. function test_serverStreamSubscription() {
  129. serverStreamSubscription.enabled = true;
  130. wait(20000);
  131. compare(serverStreamSubscription.ok, true, "Subscription data failed")
  132. compare(serverStreamSubscription.updateCount, 4, "Subscription failed, update was not called right amount times")
  133. }
  134. function test_serverStreamCancelSubscription() {
  135. serverStreamCancelSubscription.enabled = true;
  136. wait(20000);
  137. compare(serverStreamCancelSubscription.ok, true, "Subscription data failed")
  138. compare(serverStreamCancelSubscription.updateCount, 3, "Subscription failed, update was not called right amount times")
  139. }
  140. function test_serverStreamInvalidSubscription() {
  141. serverStreamInvalidSubscription.enabled = true;
  142. wait(500);
  143. compare(serverStreamInvalidSubscription.ok, true, "Subscription data failed")
  144. compare(serverStreamInvalidSubscription.enabled, false, "Subscription data failed")
  145. }
  146. }