Loading...
Searching...
No Matches
tcp_socket_test.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23#include <gtest/gtest.h>
24
25#include <future>
26#include <thread>
27
28namespace resdb {
29
30TEST(TcpSocket, SendAndRecv) {
31 std::promise<bool> svr_done, cli_done;
32 std::future<bool> svr_done_future = svr_done.get_future(),
33 cli_done_future = cli_done.get_future();
34
35 std::thread svr_thead = std::thread([&]() {
36 TcpSocket svr_socket;
37 svr_socket.Listen("127.0.0.1", 1234);
38 svr_done.set_value(true);
39 auto client_socket = svr_socket.Accept();
40 char *buf = nullptr;
41 size_t len = 0;
42 int ret = client_socket->Recv((void **)&buf, &len);
43 EXPECT_EQ(ret, 4);
44 EXPECT_EQ(std::string(buf, len), "test");
45 free(buf);
46
47 ret = client_socket->Send("recv_suc");
48 EXPECT_EQ(ret, 0);
49
50 cli_done_future.get();
51 });
52
53 svr_done_future.get();
54 // Sever is ready, start the client.
55
56 TcpSocket client_socket;
57 int ret = client_socket.Connect("127.0.0.1", 1234);
58 ASSERT_EQ(ret, 0);
59 ret = client_socket.Send("test");
60 ASSERT_EQ(ret, 0);
61
62 char *buf = nullptr;
63 size_t len = 0;
64 ret = client_socket.Recv((void **)&buf, &len);
65 EXPECT_EQ(ret, 8);
66 EXPECT_EQ(std::string(buf, len), "recv_suc");
67 free(buf);
68
69 cli_done.set_value(true);
70 svr_thead.join();
71}
72
73TEST(TcpSocket, SendAfterClose) {
74 std::promise<bool> svr_done, cli_done;
75 std::future<bool> svr_done_future = svr_done.get_future(),
76 cli_done_future = cli_done.get_future();
77
78 std::thread svr_thead = std::thread([&]() {
79 TcpSocket svr_socket;
80 svr_socket.Listen("127.0.0.1", 1234);
81 svr_done.set_value(true);
82 cli_done_future.get();
83 });
84
85 svr_done_future.get();
86 TcpSocket client_socket;
87 client_socket.Connect("127.0.0.1", 1234);
88 client_socket.Close();
89 EXPECT_EQ(client_socket.Send("test"), -2);
90 cli_done.set_value(true);
91 svr_thead.join();
92}
93
94} // namespace resdb
void Close() override
int Connect(const std::string &ip, int port) override
int Send(const std::string &data) override
int Recv(void **buf, size_t *len) override
std::unique_ptr< Socket > Accept() override
int Listen(const std::string &ip, int port) override
TEST(NetworkUtilsTest, GetDNSName)