Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9897D200C5C for ; Thu, 20 Apr 2017 22:25:10 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 973B5160B90; Thu, 20 Apr 2017 20:25:10 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 89681160BB1 for ; Thu, 20 Apr 2017 22:25:09 +0200 (CEST) Received: (qmail 46337 invoked by uid 500); 20 Apr 2017 20:25:08 -0000 Mailing-List: contact dev-help@quickstep.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@quickstep.incubator.apache.org Delivered-To: mailing list dev@quickstep.incubator.apache.org Received: (qmail 46220 invoked by uid 99); 20 Apr 2017 20:25:08 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Apr 2017 20:25:08 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id B4BC0C18AA for ; Thu, 20 Apr 2017 20:25:07 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.021 X-Spam-Level: X-Spam-Status: No, score=-4.021 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id Z9r_cuc6aK7I for ; Thu, 20 Apr 2017 20:25:06 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id ECC325FBB0 for ; Thu, 20 Apr 2017 20:25:05 +0000 (UTC) Received: (qmail 45805 invoked by uid 99); 20 Apr 2017 20:25:05 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 20 Apr 2017 20:25:05 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 27A6AF4A27; Thu, 20 Apr 2017 20:25:05 +0000 (UTC) From: zuyu To: dev@quickstep.incubator.apache.org Reply-To: dev@quickstep.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-quickstep pull request #232: QUICKSTEP-87 Adds network cli interfa... Content-Type: text/plain Message-Id: <20170420202505.27A6AF4A27@git1-us-west.apache.org> Date: Thu, 20 Apr 2017 20:25:05 +0000 (UTC) archived-at: Thu, 20 Apr 2017 20:25:10 -0000 Github user zuyu commented on a diff in the pull request: https://github.com/apache/incubator-quickstep/pull/232#discussion_r112550617 --- Diff: cli/tests/NetworkIO_unittest.cpp --- @@ -0,0 +1,182 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + **/ + +#include +#include +#include +#include +#include + +#include "cli/Flags.hpp" +#include "cli/LineReaderBuffered.hpp" +#include "cli/NetworkCliClient.hpp" +#include "cli/NetworkIO.hpp" +#include "threading/ConditionVariable.hpp" +#include "threading/Mutex.hpp" +#include "threading/Thread.hpp" + +#include "glog/logging.h" +#include "gtest/gtest.h" + +using std::unique_ptr; + +namespace quickstep { + +static std::string const kQueryRequest = "O Captain! My Captain!"; +static std::string const kQueryResponse = "Our fearful trip is done,"; + +/** + * Contains a server instance for testing. + */ +class TestNetworkIO { + public: + TestNetworkIO() + : service_(), + server_address_("localhost:" + std::to_string(FLAGS_port)), + server_(nullptr) { + grpc::ServerBuilder builder; + builder.AddListeningPort(server_address_, grpc::InsecureServerCredentials()); + builder.RegisterService(&service_); + server_ = builder.BuildAndStart(); + CHECK(server_) << "Failed to start server"; + LOG(INFO) << "TestSingleNodeServer listening on " << server_address_; + } + + // Gets a message from the Service worker. + std::string getSentMessage() { + networkio_internal::RequestState& requestState = service_.getRequestState(); + std::unique_lock lock(requestState.mutex_); + while (!requestState.request_ready_) + requestState.condition_.wait(lock); + + EXPECT_EQ(requestState.request_ready_, true); + EXPECT_EQ(requestState.response_ready_, false); + + return requestState.request_buffer_; + } + + // Sets the response message of the Service worker. Alerts it that the request is ready. + void setResponse(std::string response) { + networkio_internal::RequestState& requestState = service_.getRequestState(); + requestState.response_message_.set_query_result(response); + + EXPECT_EQ(requestState.response_ready_, false); + + requestState.responseReady(); + } + + NetworkCliServiceImpl& getService() { + return service_; + } + + ~TestNetworkIO() { + service_.kill(); + server_->Shutdown(); + server_->Wait(); + } + + private: + NetworkCliServiceImpl service_; + std::string server_address_; + std::unique_ptr server_; +}; + +/** + * Tests that killing the service will cancel requests. + */ +TEST(NetworkIOTest, TestShutdown) { + TestNetworkIO server; + + server.getService().kill(); + + NetworkCliClient client( + grpc::CreateChannel("localhost:" + std::to_string(FLAGS_port), + grpc::InsecureChannelCredentials())); + QueryRequest request; + request.set_query(kQueryRequest); + QueryResponse response; + Status status = client.SendQuery(request, &response); + ASSERT_EQ(status.error_code(), grpc::CANCELLED); +} + +/** + * Tests a simple call and response to the Service. + */ +TEST(NetworkIOTest, TestNetworkIOCommandInteraction) { + NetworkIO networkIO; + std::string const query_stmt = kQueryRequest + ";" + kQueryRequest; + + // This thread will handle the response from the client in a similar way as the quickstep cli will. + std::thread server_handler([&networkIO, &query_stmt]() { + std::string command = networkIO.getNextCommand(); + EXPECT_EQ(command, query_stmt); + + // Set some output for the main test thread, return. + fprintf(networkIO.out(), "%s", kQueryResponse.c_str()); + networkIO.notifyCommandComplete(); + }); + + NetworkCliClient client( + grpc::CreateChannel("localhost:" + std::to_string(FLAGS_port), + grpc::InsecureChannelCredentials())); + QueryRequest request; + request.set_query(query_stmt); + QueryResponse response; + Status status = client.SendQuery(request, &response); + ASSERT_TRUE(status.ok()); + EXPECT_EQ(response.query_result(), kQueryResponse); + EXPECT_EQ(response.error_result(), std::string("")); + + server_handler.join(); +} + +TEST(NetworkIOTest, TestLineReaderBuffered) { + // The buffered line reader is used exclusively by the NetworkIO's client. + LineReaderBuffered linereader; + EXPECT_TRUE(linereader.bufferEmpty()); + + std::string stmt = "select * from foo;"; + std::string stmts = stmt + "select 1; select 2; quit;"; --- End diff -- Suggest to change to `single_statement` and `multiple_statements`. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---