tst_grpc.qml 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. GrpcSubscription {
  37. id: serverStreamSubscription
  38. property bool ok: true
  39. property int updateCount: 0
  40. enabled: false
  41. client: TestServiceClient
  42. method: "testMethodServerStream"
  43. argument: stringMsg
  44. onUpdated: {
  45. ++updateCount;
  46. ok = ok && value.testFieldString === "Test string" + updateCount
  47. }
  48. onError: {
  49. console.log("Subscription error: " + status.code + " " + status.message)
  50. ok = false;
  51. }
  52. }
  53. GrpcSubscription {
  54. id: serverStreamCancelSubscription
  55. property bool ok: true
  56. property int updateCount: 0
  57. enabled: false
  58. client: TestServiceClient
  59. method: "testMethodServerStream"
  60. argument: stringMsg
  61. onUpdated: {
  62. ++updateCount;
  63. ok = ok && value.testFieldString === "Test string" + updateCount
  64. if (updateCount === 3) {
  65. serverStreamCancelSubscription.enabled = false;
  66. }
  67. }
  68. onError: {
  69. console.log("Subscription error: " + status.code + " " + status.message)
  70. ok = false;
  71. }
  72. }
  73. GrpcSubscription {
  74. id: serverStreamInvalidSubscription
  75. property bool ok: false
  76. enabled: false
  77. client: TestServiceClient
  78. method: "testMethodServerStreamNotExist"
  79. argument: stringMsg
  80. onUpdated: {
  81. ok = false;
  82. }
  83. onError: {
  84. ok = status.code === GrpcStatus.Unimplemented;
  85. }
  86. }
  87. function test_stringEchoTest() {
  88. var called = false;
  89. var errorCalled = false;
  90. TestServiceClient.testMethod(stringMsg, function(result) {
  91. called = result.testFieldString === stringMsg.testFieldString;
  92. }, function(status) {
  93. errorCalled = true
  94. })
  95. wait(300)
  96. compare(called && !errorCalled, true, "testMethod was not called proper way")
  97. }
  98. function test_statusTest() {
  99. var called = false;
  100. var errorCalled = false;
  101. TestServiceClient.testMethodStatusMessage(stringMsg, function(result) {
  102. called = true;
  103. }, function(status) {
  104. errorCalled = status.code === GrpcStatus.Unimplemented && status.message === stringMsg.testFieldString;
  105. })
  106. wait(300)
  107. compare(!called && errorCalled, true, "testMethodStatusMessage was not called proper way")
  108. }
  109. function test_stringEchoAsyncTest() {
  110. var called = false;
  111. var errorCalled = false;
  112. stringMsg.testFieldString = "sleep";
  113. TestServiceClient.testMethod(stringMsg, function(result) {
  114. called = result.testFieldString === stringMsg.testFieldString;
  115. }, function(status) {
  116. errorCalled = true
  117. })
  118. wait(300)
  119. compare(!called && !errorCalled, true, "testMethod was not called proper way")
  120. wait(1000)
  121. compare(called && !errorCalled, true, "testMethod was not called proper way")
  122. stringMsg.testFieldString = "Test string";
  123. }
  124. function test_serverStreamSubscription() {
  125. serverStreamSubscription.enabled = true;
  126. wait(20000);
  127. compare(serverStreamSubscription.ok, true, "Subscription data failed")
  128. compare(serverStreamSubscription.updateCount, 4, "Subscription failed, update was not called right amount times")
  129. }
  130. function test_serverStreamCancelSubscription() {
  131. serverStreamCancelSubscription.enabled = true;
  132. wait(20000);
  133. compare(serverStreamCancelSubscription.ok, true, "Subscription data failed")
  134. compare(serverStreamCancelSubscription.updateCount, 3, "Subscription failed, update was not called right amount times")
  135. }
  136. function test_serverStreamInvalidSubscription() {
  137. serverStreamInvalidSubscription.enabled = true;
  138. wait(500);
  139. compare(serverStreamInvalidSubscription.ok, true, "Subscription data failed")
  140. compare(serverStreamInvalidSubscription.enabled, false, "Subscription data failed")
  141. }
  142. }