Loading...
Searching...
No Matches
service_network_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 <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include <future>
26#include <thread>
27
30
31namespace resdb {
32
33using ::testing::Invoke;
34using ::testing::Return;
35
37 std::vector<ReplicaInfo> replicas;
38 ReplicaInfo self_info;
39 self_info.set_ip("127.0.0.1");
40 self_info.set_port(1234);
41 ReplicaInfo dest_info;
42 dest_info.set_ip("127.0.0.1");
43 dest_info.set_port(1235);
44 replicas.push_back(dest_info);
45
46 return ResDBConfig(replicas, self_info, KeyInfo(), CertificateInfo());
47}
48
49void SendData(const std::string& data) {
50 TcpSocket client_socket;
51 int ret = client_socket.Connect("127.0.0.1", 1234);
52 ASSERT_EQ(ret, 0);
53 ret = client_socket.Send(data);
54 ASSERT_EQ(ret, 0);
55}
56
57TEST(ServiceNetworkTest, RecvData) {
58 std::promise<bool> init;
59 std::future<bool> init_done = init.get_future();
60
61 std::promise<bool> recv;
62 std::future<bool> recv_done = recv.get_future();
63
64 std::unique_ptr<MockServiceInterface> service =
65 std::make_unique<MockServiceInterface>();
66 bool finished = false;
67 EXPECT_CALL(*service, IsRunning).WillRepeatedly(Invoke([&]() {
68 return !finished;
69 }));
70
71 EXPECT_CALL(*service, Process)
72 .WillOnce(Invoke([&](std::unique_ptr<Context>,
73 std::unique_ptr<DataInfo> request_info) {
74 EXPECT_EQ(
75 std::string((char*)request_info->buff, request_info->data_len),
76 "test");
77 recv.set_value(true);
78 return 0;
79 }));
80
81 std::thread svr_thead2 = std::thread([&]() {
82 ServiceNetwork server(GenerateDBConfig(), std::move(service));
83 init.set_value(true);
84 server.Run();
85 });
86 init_done.get();
87 SendData("test");
88 recv_done.get();
89 finished = true;
90 svr_thead2.join();
91}
92
93TEST(ServiceNetworkTest, RunningDone) {
94 std::unique_ptr<MockServiceInterface> service =
95 std::make_unique<MockServiceInterface>();
96 bool finished = false;
97 EXPECT_CALL(*service, IsRunning).WillRepeatedly(Invoke([&]() {
98 return !finished;
99 }));
100
101 EXPECT_CALL(*service, Process).Times(0);
102
103 std::promise<bool> init;
104 std::future<bool> init_done = init.get_future();
105
106 std::thread svr_thead2 = std::thread([&]() {
107 ServiceNetwork server(GenerateDBConfig(), std::move(service));
108 init.set_value(true);
109 server.Run();
110 });
111 init_done.get();
112 finished = true;
113 svr_thead2.join();
114}
115
116} // namespace resdb
std::string data
int Connect(const std::string &ip, int port) override
int Send(const std::string &data) override
ResDBConfig GenerateDBConfig()
void SendData(const std::string &data)
TEST(NetworkUtilsTest, GetDNSName)