Loading...
Searching...
No Matches
async_acceptor_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
28
29namespace resdb {
30namespace {
31
32TEST(AsyncAcceptorTest, RecvMessage) {
33 std::promise<bool> bc;
34 std::future<bool> bc_done = bc.get_future();
35 AsyncAcceptor acceptor(
36 "127.0.0.1", 1234, 1,
37 [&](const char* buff, size_t data_len) { bc.set_value(true); });
38
39 acceptor.StartAccept();
40
41 TcpSocket client_socket;
42 int ret = client_socket.Connect("127.0.0.1", 1234);
43 ASSERT_EQ(ret, 0);
44 ret = client_socket.Send("test");
45 ASSERT_EQ(ret, 0);
46 bc_done.get();
47}
48
49TEST(AsyncAcceptorTest, RecvMessageAndClose) {
50 std::promise<bool> bc;
51 std::future<bool> bc_done = bc.get_future();
52 AsyncAcceptor acceptor(
53 "127.0.0.1", 1234, 1,
54 [&](const char* buff, size_t data_len) { bc.set_value(true); });
55
56 acceptor.StartAccept();
57
58 TcpSocket client_socket;
59 int ret = client_socket.Connect("127.0.0.1", 1234);
60 ASSERT_EQ(ret, 0);
61 ret = client_socket.Send("test");
62 client_socket.Close();
63 ASSERT_EQ(ret, 0);
64 bc_done.get();
65}
66
67TEST(AsyncAcceptorTest, MultiAcceptor) {
68 std::promise<bool> bc;
69 std::future<bool> bc_done = bc.get_future();
70 int a = 0;
71 AsyncAcceptor acceptor("127.0.0.1", 1234, 2,
72 [&](const char* buff, size_t data_len) {
73 a++;
74 if (a == 3) bc.set_value(true);
75 });
76
77 acceptor.StartAccept();
78
79 for (int i = 0; i < 3; ++i) {
80 TcpSocket client_socket;
81 int ret = client_socket.Connect("127.0.0.1", 1234);
82 ASSERT_EQ(ret, 0);
83 ret = client_socket.Send("test");
84 client_socket.Close();
85 ASSERT_EQ(ret, 0);
86 }
87 bc_done.get();
88}
89
90TEST(AsyncAcceptorTest, MultiAcceptorError) {
91 std::promise<bool> bc;
92 std::future<bool> bc_done = bc.get_future();
93 AsyncAcceptor acceptor("127.0.0.1", 1234, 2,
94 [&](const char* buff, size_t data_len) {});
95
96 acceptor.StartAccept();
97
98 for (int i = 0; i < 3; ++i) {
99 TcpSocket client_socket;
100 int ret = client_socket.Connect("127.0.0.1", 1234);
101 ret = client_socket.Send("test");
102 ASSERT_EQ(ret, 0);
103 client_socket.Close();
104 }
105}
106
107TEST(AsyncAcceptorTest, RecvClose) {
108 std::promise<bool> bc;
109 std::future<bool> bc_done = bc.get_future();
110 AsyncAcceptor acceptor(
111 "127.0.0.1", 1234, 1,
112 [&](const char* buff, size_t data_len) { bc.set_value(true); });
113
114 acceptor.StartAccept();
115
116 TcpSocket client_socket;
117 int ret = client_socket.Connect("127.0.0.1", 1234);
118 ASSERT_EQ(ret, 0);
119 client_socket.Close();
120
121 {
122 TcpSocket client_socket;
123 int ret = client_socket.Connect("127.0.0.1", 1234);
124 ASSERT_EQ(ret, 0);
125 ret = client_socket.Send("test");
126 ASSERT_EQ(ret, 0);
127 client_socket.Close();
128 }
129
130 bc_done.get();
131}
132
133} // namespace
134
135} // namespace resdb
TEST(NetworkUtilsTest, GetDNSName)