Skip to content

Commit

Permalink
auto port in CI (#340)
Browse files Browse the repository at this point in the history
auto port in CI
  • Loading branch information
lihuiba authored Jan 29, 2024
1 parent 4bbed75 commit 84f5dc4
Show file tree
Hide file tree
Showing 35 changed files with 364 additions and 316 deletions.
1 change: 1 addition & 0 deletions common/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
*/

#pragma once
#include <stdlib.h>
#include <sys/types.h>
#include <memory>
#include <photon/common/object.h>
Expand Down
2 changes: 1 addition & 1 deletion examples/c++20coro/echo_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ photon::coro::FixedGenerator<photon::net::ISocketStream*> socket_accept(
photon::coro::Coro<void> server(int port) {
auto server = photon::net::new_tcp_socket_server();
DEFER(delete server);
server->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
server->setsockopt<int>(SOL_SOCKET, SO_REUSEPORT, 1);
server->bind(port);
server->listen();
std::ranges::for_each(socket_accept(server), [&](auto sess) {
Expand Down
2 changes: 1 addition & 1 deletion examples/perf/net-perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static int echo_server() {
photon::thread_enable_join(stop_th);

server->set_handler(handler);
server->bind(FLAGS_port, photon::net::IPAddr());
server->bind_localhost4(FLAGS_port);
server->listen();
server->start_loop(true);

Expand Down
2 changes: 1 addition & 1 deletion examples/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int ExampleServer::do_rpc_service(WriteBuffer::Request* req,
}

int ExampleServer::run(int port) {
if (server->bind(port) < 0)
if (server->bind_localhost4(port) < 0)
LOG_ERRNO_RETURN(0, -1, "Failed to bind port `", port)
if (server->listen() < 0) LOG_ERRNO_RETURN(0, -1, "Failed to listen");
server->set_handler({this, &ExampleServer::serve});
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void run_socket_server(photon::net::ISocketServer* server, photon::fs::IFile* fi
};

server->set_handler(handler);
server->bind(9527, photon::net::IPAddr());
server->bind_localhost4(9527);
server->listen();

// Photon's logging system formats the output string at COMPILE time, and has MUCH BETTER performance
Expand Down
4 changes: 2 additions & 2 deletions net/base_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ class ForwardSocketServer : public ISocketServer {
return (recursion == 0) ? m_underlay : m_underlay->get_underlay_object(recursion - 1);
}

int bind(uint16_t port, IPAddr addr) override {
return m_underlay->bind(port, addr);
int bind(const EndPoint& ep) override {
return m_underlay->bind(ep);
}

int bind(const char* path, size_t count) override {
Expand Down
12 changes: 9 additions & 3 deletions net/datagram_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ class UDPSocket : public IDatagramSocket {
public:
using base::recv;
using base::send;
int connect(const EndPoint ep) { return connect((Addr*)&ep, sizeof(ep)); }
int bind(const EndPoint ep) { return bind((Addr*)&ep, sizeof(ep)); }
int connect(const EndPoint& ep) { return connect((Addr*)&ep, sizeof(ep)); }
int bind(const EndPoint& ep) { return bind((Addr*)&ep, sizeof(ep)); }
int bind(uint16_t port = 0) { return bind_any4(0); }
int bind_any4(uint16_t port = 0) { return bind(EndPoint(IPAddr::V4Any(), port)); }
int bind_any6(uint16_t port = 0) { return bind(EndPoint(IPAddr::V6Any(), port)); }
int bind_localhost4(uint16_t port = 0) { return bind(EndPoint(IPAddr::V4Loopback(), port)); }
int bind_localhost6(uint16_t port = 0) { return bind(EndPoint(IPAddr::V6Loopback(), port)); }

template <typename B, typename S>
ssize_t sendto(B* buf, S count, const EndPoint ep, int flags = 0) {
ssize_t sendto(B* buf, S count, const EndPoint& ep, int flags = 0) {
return base::sendto(buf, count, (Addr*)&ep, sizeof(ep), flags);
}
template <typename B, typename S>
Expand Down
2 changes: 1 addition & 1 deletion net/http/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ISocketStream* PooledDialer::dial(std::string_view host, uint16_t port, bool sec
sock = tcpsock->connect(ep);
}
if (sock) {
LOG_DEBUG("Connected ` host : ` ssl: ` `", ep, host, secure, sock);
LOG_DEBUG("Connected ` ", ep, VALUE(host), VALUE(secure));
return sock;
}
LOG_ERROR("connection failed, ssl : ` ep : ` host : `", secure, ep, host);
Expand Down
54 changes: 33 additions & 21 deletions net/http/test/client_function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ limitations under the License.
#include <photon/thread/thread11.h>
#include <photon/common/stream.h>
#include <photon/fs/localfs.h>
#include "to_url.h"

using namespace photon::net;
using namespace photon::net::http;
Expand All @@ -65,8 +66,8 @@ TEST(http_client, get) {
system("mkdir -p /tmp/ease_ut/http_test/");
system("echo \"this is a http_client request body text for socket stream\" > /tmp/ease_ut/http_test/ease-httpclient-gettestfile");
auto tcpserver = new_tcp_socket_server();
tcpserver->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1L);
tcpserver->bind(18731);
tcpserver->setsockopt<int>(IPPROTO_TCP, TCP_NODELAY, 1);
tcpserver->bind_any4();
tcpserver->listen();
DEFER(delete tcpserver);
auto server = new_http_server();
Expand All @@ -78,7 +79,8 @@ TEST(http_client, get) {
server->add_handler(fs_handler);
tcpserver->set_handler(server->get_connection_handler());
tcpserver->start_loop();
static const char target[] = "http://localhost:18731/ease-httpclient-gettestfile";
auto target = to_url(tcpserver, "/ease-httpclient-gettestfile");
// static const char target[] = "http://localhost:18731/ease-httpclient-gettestfile";
auto client = new_http_client();
DEFER(delete client);
auto op2 = client->new_operation(Verb::GET, target);
Expand Down Expand Up @@ -156,8 +158,8 @@ TEST(http_client, post) {
system("echo \"this is a http_client request body text for socket stream\" > /tmp/ease_ut/http_test/ease-httpclient-posttestfile");
auto tcpserver = new_tcp_socket_server();
tcpserver->timeout(1000UL*1000);
tcpserver->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
tcpserver->bind(18731, IPAddr("127.0.0.1"));
tcpserver->bind_localhost4(0);
// tcpserver->bind(18731, IPAddr("127.0.0.1"));
tcpserver->listen();
DEFER(delete tcpserver);
auto server = new_http_server();
Expand All @@ -169,7 +171,8 @@ TEST(http_client, post) {

auto fs = photon::fs::new_localfs_adaptor("/tmp/ease_ut/http_test/");
DEFER(delete fs);
static const char target[] = "http://localhost:18731/ease-httpclient-posttestfile";
auto target = to_url(tcpserver, "/ease-httpclient-posttestfile");
// static const char target[] = "http://localhost:18731/ease-httpclient-posttestfile";
auto client = new_http_client();
DEFER(delete client);

Expand Down Expand Up @@ -267,7 +270,7 @@ int chunked_handler_complict(void*, ISocketStream* sock) {
return 0;
}

EndPoint ep{IPAddr("127.0.0.1"), 19731};
// EndPoint ep{IPAddr("127.0.0.1"), 19731};
std::string std_data;
const size_t std_data_size = 64 * 1024;
static int digtal_num(int n) {
Expand Down Expand Up @@ -317,9 +320,8 @@ int chunked_handler_pt(void*, ISocketStream* sock) {
TEST(http_client, chunked) {
auto server = new_tcp_socket_server();
DEFER({ delete server; });
server->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
server->set_handler({nullptr, &chunked_handler});
auto ret = server->bind(ep.port, ep.addr);
auto ret = server->bind_localhost4();
if (ret < 0) LOG_ERROR(VALUE(errno));
ret |= server->listen(100);
if (ret < 0) LOG_ERROR(VALUE(errno));
Expand All @@ -329,7 +331,8 @@ TEST(http_client, chunked) {
photon::thread_sleep(1);
auto client = new_http_client();
DEFER(delete client);
auto op = client->new_operation(Verb::GET, "http://localhost:19731/");
auto url = to_url(server, "/");
auto op = client->new_operation(Verb::GET, url);
DEFER(delete op);
std::string buf;

Expand All @@ -343,15 +346,18 @@ TEST(http_client, chunked) {
LOG_DEBUG(VALUE(buf));

server->set_handler({nullptr, &chunked_handler_complict});
auto opc = client->new_operation(Verb::GET, "http://localhost:19731/");
auto opc = client->new_operation(Verb::GET, url);
// auto opc = client->new_operation(Verb::GET, "http://localhost:19731/");
DEFER(delete opc);
opc->call();
EXPECT_EQ(200, opc->status_code);
buf.resize(20000);
ret = opc->resp.read((void*)buf.data(), 20000);
EXPECT_EQ(10000 + 4090 + 4086 + 1024, ret);
for (int i = 0; i < 10000 + 4090 + 4086 + 1024; i++)
EXPECT_EQ(buf[i], 'a');
size_t i, cnt;
for (i = cnt = 0; i < 10000 + 4090 + 4086 + 1024; i++)
cnt += (buf[i] == 'a');
EXPECT_EQ(i, cnt);

std_data.resize(std_data_size);
int num = 0;
Expand All @@ -361,7 +367,8 @@ TEST(http_client, chunked) {
srand(time(0));
server->set_handler({nullptr, &chunked_handler_pt});
for (auto tmp = 0; tmp < 20; tmp++) {
auto op_test = client->new_operation(Verb::GET, "http://localhost:19731/");
auto op_test = client->new_operation(Verb::GET, url);
// auto op_test = client->new_operation(Verb::GET, "http://localhost:19731/");
DEFER(delete op_test);
op_test->call();
EXPECT_EQ(200, op_test->status_code);
Expand Down Expand Up @@ -417,9 +424,9 @@ int chunked_handler_debug(void*, ISocketStream* sock) {
TEST(http_client, debug) {
auto server = new_tcp_socket_server();
DEFER({ delete server; });
server->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
server->set_handler({nullptr, &chunked_handler_debug});
auto ret = server->bind(ep.port, ep.addr);
auto ret = server->bind_localhost4();
// auto ret = server->bind(ep.port, ep.addr);
if (ret < 0) LOG_ERROR(ERRNO());
ret |= server->listen(100);
if (ret < 0) LOG_ERROR(ERRNO());
Expand All @@ -436,7 +443,8 @@ TEST(http_client, debug) {

auto client = new_http_client();
DEFER(delete client);
auto op_test = client->new_operation(Verb::GET, "http://localhost:19731/");
auto op_test = client->new_operation(Verb::GET, to_url(server, "/"));
// auto op_test = client->new_operation(Verb::GET, "http://localhost:19731/");
DEFER(delete op_test);
op_test->call();
EXPECT_EQ(200, op_test->status_code);
Expand All @@ -463,13 +471,15 @@ TEST(http_client, server_no_resp) {
auto server = new_tcp_socket_server();
DEFER(delete server);
server->set_handler({nullptr, &sleep_handler});
server->bind(38812, IPAddr());
server->bind_localhost4();
// server->bind(38812, IPAddr());
server->listen();
server->start_loop();

auto client = new_http_client();
DEFER(delete client);
auto op = client->new_operation(Verb::GET, "http://127.0.0.1:38812/wtf");
auto op = client->new_operation(Verb::GET, to_url(server, "/wtf"));
// auto op = client->new_operation(Verb::GET, "http://127.0.0.1:38812/wtf");
DEFER(delete op);
op->req.headers.content_length(0);
client->call(op);
Expand All @@ -483,7 +493,8 @@ TEST(http_client, partial_body) {

auto tcpserver = new_tcp_socket_server();
DEFER(delete tcpserver);
tcpserver->bind(18731);
tcpserver->bind_localhost4();
// tcpserver->bind(18731);
tcpserver->listen();
auto server = new_http_server();
DEFER(delete server);
Expand All @@ -495,7 +506,8 @@ TEST(http_client, partial_body) {
tcpserver->set_handler(server->get_connection_handler());
tcpserver->start_loop();

std::string target_get = "http://localhost:18731/file";
auto target_get = to_url(tcpserver, "/file");
// std::string target_get = "http://localhost:18731/file";
auto client = new_http_client();
DEFER(delete client);
auto op = client->new_operation(Verb::GET, target_get);
Expand Down
11 changes: 5 additions & 6 deletions net/http/test/client_tls_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include <photon/net/http/message.h>
#include <photon/net/http/server.h>
#include <photon/net/http/client.h>
#include "to_url.h"

#include "../../test/cert-key.cpp"

Expand Down Expand Up @@ -54,12 +55,10 @@ TEST(client_tls, basic) {
auto tcpserver = net::new_tls_server(ctx, net::new_tcp_socket_server(), true);
DEFER(delete tcpserver);
tcpserver->timeout(1000UL*1000);
tcpserver->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
auto addr = net::IPAddr::localhost();
int r = tcpserver->bind(19876, addr);
int r = tcpserver->bind_localhost4();
if (r != 0)
LOG_ERRNO_RETURN(0, , "failed to bind to `:", addr, 19876);
LOG_DEBUG("bind to `:", addr, " : 19876");
LOG_ERRNO_RETURN(0, , "failed to bind to localhost");
LOG_DEBUG("bind to :", tcpserver->getsockname());
tcpserver->listen();

auto server = net::http::new_http_server();
Expand All @@ -71,7 +70,7 @@ TEST(client_tls, basic) {

auto client = net::http::new_http_client(nullptr, ctx);
DEFER(delete client);
auto op = client->new_operation(net::http::Verb::GET, "https://localhost:19876/test");
auto op = client->new_operation(net::http::Verb::GET, to_surl(tcpserver, "/test"));
DEFER(delete op);
auto exp_len = 20;
op->req.headers.range(0, exp_len - 1);
Expand Down
4 changes: 2 additions & 2 deletions net/http/test/forward_proxy_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace photon;
using namespace photon::net;
using namespace photon::net::http;

DEFINE_int32(port, 19876, "port");
DEFINE_int32(port, 0, "port");

static bool stop_flag = false;

Expand All @@ -52,8 +52,8 @@ int main(int argc, char** argv) {
photon::sync_signal(SIGTSTP, &stop_handler);

auto tcpserv = new_tcp_socket_server();
tcpserv->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
tcpserv->bind(FLAGS_port);
LOG_DEBUG("bound to ", tcpserv->getsockname());
tcpserv->listen();
DEFER(delete tcpserv);

Expand Down
4 changes: 2 additions & 2 deletions net/http/test/fs_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using namespace photon;
using namespace photon::net;
using namespace photon::net::http;

DEFINE_int32(port, 19876, "port");
DEFINE_int32(port, 0, "port");

static bool stop_flag = false;

Expand All @@ -47,8 +47,8 @@ int main(int argc, char** argv) {
photon::sync_signal(SIGTSTP, &stop_handler);

auto tcpserv = new_tcp_socket_server();
tcpserv->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
tcpserv->bind(FLAGS_port);
LOG_DEBUG("bound to ", tcpserv->getsockname());
tcpserv->listen();
DEFER(delete tcpserv);

Expand Down
Loading

0 comments on commit 84f5dc4

Please sign in to comment.