Kaynağa Gözat

Remove deprecated ÁbstractClient::lastError-functionality (#108)

- Private AbstractClient::call returns status instead of bool
- Public call method left unchanged as it requires changes in generated code
Viktor Kopp 5 yıl önce
ebeveyn
işleme
18e4078dc4

+ 7 - 25
src/grpc/abstractclient.cpp

@@ -30,13 +30,10 @@
 namespace qtprotobuf {
 class AbstractClientPrivate final {
 public:
-    AbstractClientPrivate(const QString &service) : service(service)
-    , lastError(AbstractChannel::StatusCode::Ok) {}
+    AbstractClientPrivate(const QString &service) : service(service) {}
 
     std::shared_ptr<AbstractChannel> channel;
     const QString service;
-    AbstractChannel::StatusCode lastError;
-    QString lastErrorString;
 };
 }
 
@@ -45,7 +42,6 @@ using namespace qtprotobuf;
 AbstractClient::AbstractClient(const QString &service, QObject *parent) : QObject(parent)
   , d(new AbstractClientPrivate(service))
 {
-
 }
 
 AbstractClient::~AbstractClient()
@@ -58,7 +54,7 @@ void AbstractClient::attachChannel(const std::shared_ptr<AbstractChannel> &chann
     d->channel = channel;
 }
 
-bool AbstractClient::call(const QString &method, const QByteArray &arg, QByteArray &ret)
+AbstractChannel::StatusCode AbstractClient::call(const QString &method, const QByteArray &arg, QByteArray &ret)
 {
     AbstractChannel::StatusCode callStatus = AbstractChannel::Unknown;
     if (d->channel) {
@@ -67,7 +63,7 @@ bool AbstractClient::call(const QString &method, const QByteArray &arg, QByteArr
         emit error(callStatus, QLatin1String("No channel(s) attached."));
     }
 
-    return AbstractChannel::Ok == callStatus;
+    return callStatus;
 }
 
 AsyncReply *AbstractClient::call(const QString &method, const QByteArray &arg)
@@ -77,7 +73,6 @@ AsyncReply *AbstractClient::call(const QString &method, const QByteArray &arg)
         reply = new AsyncReply(d->channel, this);
 
         connect(reply, &AsyncReply::error, this, [this, reply](AbstractChannel::StatusCode statusCode) {
-            d->lastError = statusCode;
             emit error(statusCode, QLatin1String("Connection has been aborted."));
             reply->deleteLater();
         });
@@ -96,22 +91,9 @@ AsyncReply *AbstractClient::call(const QString &method, const QByteArray &arg)
 
 void AbstractClient::subscribe_p(const QString &method, const QByteArray &arg, const std::function<void(const QByteArray&)> &handler)
 {
-    d->lastError = AbstractChannel::Ok;
-    if (!d->channel) {
-        d->lastError = AbstractChannel::Unknown;
-        d->lastErrorString = "No channel attached";
-        return;
+    if (d->channel) {
+        d->channel->subscribe(method, d->service, arg, this, handler);
+    } else {
+        emit error(AbstractChannel::Unknown, QLatin1String("No channel(s) attached."));
     }
-
-    d->channel->subscribe(method, d->service, arg, this, handler);
-}
-
-AbstractChannel::StatusCode AbstractClient::lastError() const
-{
-    return d->lastError;
-}
-
-QString AbstractClient::lastErrorString() const
-{
-    return d->lastErrorString;
 }

+ 3 - 10
src/grpc/abstractclient.h

@@ -49,15 +49,8 @@ class QTGRPCSHARED_EXPORT AbstractClient : public QObject
 public:
     void attachChannel(const std::shared_ptr<AbstractChannel> &channel);
 
-    [[deprecated]]
-    AbstractChannel::StatusCode lastError() const;
-
-    [[deprecated]]
-    QString lastErrorString() const;
-
 signals:
-    // TODO: emit this signal instead of setting lastError
-    void error(AbstractChannel::StatusCode code, QString errorText);
+    void error(AbstractChannel::StatusCode code, const QString &errorText);
 
 protected:
     AbstractClient(const QString &service, QObject *parent = nullptr);
@@ -78,7 +71,7 @@ protected:
         }
 
         QByteArray retData;
-        if (call(method, arg.serialize(), retData)) {
+        if (AbstractChannel::StatusCode::Ok == call(method, arg.serialize(), retData)) {
             try {
                 ret->deserialize(retData);
             } catch (std::invalid_argument &) {
@@ -128,7 +121,7 @@ protected:
     }
 
 private:
-    bool call(const QString &method, const QByteArray &arg, QByteArray &ret);
+    AbstractChannel::StatusCode call(const QString &method, const QByteArray &arg, QByteArray &ret);
     AsyncReply *call(const QString &method, const QByteArray &arg);
     void subscribe_p(const QString &method, const QByteArray &arg, const std::function<void(const QByteArray &)> &handler);
 

+ 5 - 13
tests/test_grpc/clienttest.cpp

@@ -88,10 +88,8 @@ TEST_F(ClientTest, StringEchoAsyncTest)
     QEventLoop waiter;
 
     AsyncReply *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>();
-        }
+    QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter]() {
+        result = reply->read<SimpleStringMessage>();
         waiter.quit();
     });
 
@@ -107,10 +105,8 @@ TEST_F(ClientTest, StringEchoAsync2Test)
     SimpleStringMessage request;
     request.setTestFieldString("Hello beach!");
     QEventLoop waiter;
-    testClient.testMethod(request, &m_app, [&result, &waiter, &testClient](AsyncReply *reply) {
-        if (testClient.lastError() == AbstractChannel::StatusCode::Ok) {
-            result = reply->read<SimpleStringMessage>();
-        }
+    testClient.testMethod(request, &m_app, [&result, &waiter](AsyncReply *reply) {
+        result = reply->read<SimpleStringMessage>();
         waiter.quit();
     });
 
@@ -167,7 +163,7 @@ TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
     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]() {
+    QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter]() {
         result = reply->read<SimpleStringMessage>();
         waiter.quit();
     });
@@ -181,7 +177,6 @@ TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
     waiter.exec();
 
     ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
-    ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Aborted);
     ASSERT_TRUE(errorCalled);
 }
 
@@ -213,7 +208,6 @@ TEST_F(ClientTest, StringEchoStreamTest)
 
     ASSERT_EQ(i, 4);
     ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
-    ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
 }
 
 TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
@@ -239,7 +233,6 @@ TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
 
     ASSERT_EQ(i, 4);
     ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
-    ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
 }
 
 
@@ -268,5 +261,4 @@ TEST_F(ClientTest, HugeBlobEchoStreamTest)
 
     QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
     ASSERT_TRUE(returnDataHash == dataHash);
-    ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
 }