| 1 |
// Copyright (C) 2024 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
import QtQuick |
| 5 |
import QtTest |
| 6 |
import QmlTestUri |
| 7 |
import QtGrpc; |
| 8 |
import qtgrpc.tests |
| 9 |
|
| 10 |
Item { |
| 11 |
id: root |
| 12 |
|
| 13 |
readonly property int expectedNumberOfMessages: 4 |
| 14 |
|
| 15 |
property simpleStringMessage messageArg; |
| 16 |
property string result: "" |
| 17 |
property var streamSender: null |
| 18 |
property bool errorCallbackCalled: false |
| 19 |
property int times: 1 |
| 20 |
|
| 21 |
Timer { |
| 22 |
id: timer |
| 23 |
running: false |
| 24 |
repeat: false |
| 25 |
interval: testMessageLatencyWithThreshold * root.expectedNumberOfMessages |
| 26 |
onTriggered: testCase.when = true; |
| 27 |
} |
| 28 |
|
| 29 |
function readMessage(msg) { |
| 30 |
root.result += msg.testFieldString |
| 31 |
|
| 32 |
if (root.times < expectedNumberOfMessages) { |
| 33 |
++root.times |
| 34 |
root.messageArg.testFieldString = "streamQml" + root.times |
| 35 |
testCase.verify(root.streamSender, |
| 36 |
"readMessage callback is called without active stream sender") |
| 37 |
root.streamSender.writeMessage(root.messageArg) |
| 38 |
|
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
function endTest() { |
| 43 |
testCase.when = true |
| 44 |
} |
| 45 |
|
| 46 |
function errorCallback() { |
| 47 |
root.errorCallbackCalled = true |
| 48 |
endTest() |
| 49 |
} |
| 50 |
|
| 51 |
GrpcHttp2Channel { |
| 52 |
id: httpChannel |
| 53 |
hostUri: "http://localhost:50051" |
| 54 |
options: GrpcChannelOptions { |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
TestServiceClient { |
| 59 |
id: clientQml |
| 60 |
channel: httpChannel.channel |
| 61 |
} |
| 62 |
|
| 63 |
TestCase { |
| 64 |
name: "startBidiStream" |
| 65 |
|
| 66 |
function test_testBidiStream() { |
| 67 |
root.messageArg.testFieldString = "streamQml" + root.times |
| 68 |
root.streamSender = clientQml.testMethodBiStream(root.messageArg, |
| 69 |
root.readMessage, |
| 70 |
root.endTest, |
| 71 |
root.errorCallback) |
| 72 |
timer.start() |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
TestCase { |
| 77 |
id: testCase |
| 78 |
name: "checkBidiStreamResult" |
| 79 |
when: false |
| 80 |
|
| 81 |
function test_testBidiStreamCheck() { |
| 82 |
compare(root.result, "streamQml11streamQml22streamQml33streamQml44") |
| 83 |
compare(root.times, root.expectedNumberOfMessages) |
| 84 |
verify(!root.errorCallbackCalled) |
| 85 |
} |
| 86 |
} |
| 87 |
} |