Ver código fonte

Async reply emits either error (failure) or finished (success) signals

- Abort test split into two: immediate and deferred abort
Viktor Kopp 5 anos atrás
pai
commit
a400a94f56
2 arquivos alterados com 29 adições e 14 exclusões
  1. 4 6
      src/grpc/http2channel.cpp
  2. 25 8
      tests/test_grpc/clienttest.cpp

+ 4 - 6
src/grpc/http2channel.cpp

@@ -246,14 +246,13 @@ void Http2Channel::call(const QString &method, const QString &service, const QBy
         QByteArray data = Http2ChannelPrivate::processReply(networkReply, grpcStatus);
 
         qProtoDebug() << "RECV: " << data;
-        if (grpcStatus != StatusCode::Ok) {
+        if (StatusCode::Ok == grpcStatus ) {
+            reply->setData(data);
+            reply->finished();
+        } else {
             reply->setData({});
             reply->error(grpcStatus);
-            reply->finished();
-            return;
         }
-        reply->setData(data);
-        reply->finished();
     });
 
     QObject::connect(reply, &AsyncReply::error, networkReply, [networkReply, connection](AbstractChannel::StatusCode code) {
@@ -318,5 +317,4 @@ void Http2Channel::abort(AsyncReply *reply)
     assert(reply != nullptr);
     reply->setData({});
     reply->error(StatusCode::Aborted);
-    reply->finished();
 }

+ 25 - 8
tests/test_grpc/clienttest.cpp

@@ -118,7 +118,7 @@ TEST_F(ClientTest, StringEchoAsync2Test)
     ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
 }
 
-TEST_F(ClientTest, StringEchoAsyncAbortTest)
+TEST_F(ClientTest, StringEchoImmediateAsyncAbortTest)
 {
     TestServiceClient testClient;
     testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
@@ -129,7 +129,8 @@ TEST_F(ClientTest, StringEchoAsyncAbortTest)
     AsyncReply *reply = testClient.testMethod(request);
 
     result.setTestFieldString("Result not changed by echo");
-    QObject::connect(reply, &AsyncReply::finished, &m_app, [&waiter]() {
+    QObject::connect(reply, &AsyncReply::finished, &m_app, [&waiter, &result, reply]() {
+        result = reply->read<SimpleStringMessage>();
         waiter.quit();
     });
 
@@ -137,21 +138,37 @@ TEST_F(ClientTest, StringEchoAsyncAbortTest)
     QObject::connect(reply, &AsyncReply::error, reply, [&asyncStatus](AbstractChannel::StatusCode code){
         asyncStatus = code;
     });
+
+    AbstractChannel::StatusCode clientStatus = AbstractChannel::StatusCode::Ok;
+    QObject::connect(&testClient, &TestServiceClient::error, reply, [&clientStatus](AbstractChannel::StatusCode code, QString errMsg){
+        clientStatus = code;
+        std::cerr << code << ":" << errMsg.toStdString();
+    });
+
     QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
     reply->abort();
-
     waiter.exec();
-    ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Aborted);
+
+    ASSERT_EQ(clientStatus, AbstractChannel::StatusCode::Aborted);
     ASSERT_EQ(asyncStatus, AbstractChannel::StatusCode::Aborted);
     ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
+}
+
+TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
+{
+    TestServiceClient testClient;
+    testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
+    SimpleStringMessage result;
+    SimpleStringMessage request;
+    request.setTestFieldString("sleep");
+    QEventLoop waiter;
+    AsyncReply *reply = testClient.testMethod(request);
 
-    // What we check here?
+    result.setTestFieldString("Result not changed by echo");
     bool errorCalled = false;
     reply = testClient.testMethod(request);
     QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
-        if (testClient.lastError() == AbstractChannel::StatusCode::Ok) {
-            result = reply->read<SimpleStringMessage>();
-        }
+        result = reply->read<SimpleStringMessage>();
         waiter.quit();
     });
     QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCode){