Skip to content

Commit 1675db0

Browse files
committed
Making Server tests' port to run on configurable
This addresses some concerns with the unit tests for servers that don't run mainly because the port they're all trying to run on (port 8000) is being used across all the executables. Even if the socket address is reused, that will only make sense for the same executable on UNIX/Linux. Now the tests run on different ports, with the help of changes in the tests and the CMake configuration.
1 parent ab45ced commit 1675db0

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

libs/network/test/http/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ if (Boost_FOUND)
2525
client_get_test
2626
client_get_different_port_test
2727
client_get_timeout_test
28+
client_localhost_normal_test
2829
)
30+
if (OPENSSL_FOUND)
31+
set ( TESTS ${TESTS} client_localhost_ssl_test )
32+
endif (OPENSSL_FOUND)
2933
foreach ( test ${TESTS} )
3034
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
3135
set_source_files_properties(${test}.cpp
@@ -63,7 +67,9 @@ if (Boost_FOUND)
6367
set ( SERVER_TESTS
6468
server_hello_world
6569
server_async
70+
server_async_less_copy
6671
)
72+
set ( PORT 8000 )
6773
foreach ( test ${SERVER_TESTS} )
6874
if (${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
6975
set_source_files_properties(${test}.cpp
@@ -79,7 +85,9 @@ if (Boost_FOUND)
7985
python
8086
${CPP-NETLIB_SOURCE_DIR}/libs/network/test/httplib_acceptance.py
8187
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}
88+
${PORT}
8289
${CPP-NETLIB_BINARY_DIR}/tests/cpp-netlib-http-${test}.passed)
90+
math (EXPR PORT "${PORT} + 1")
8391
endforeach (test)
8492

8593
set ( INLINED_TESTS

libs/network/test/http/server_async.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ struct async_hello_world {
5959
int main(int argc, char * argv[]) {
6060
utils::thread_pool thread_pool(2);
6161
async_hello_world handler;
62-
server instance("127.0.0.1", "8000", handler, thread_pool, http::_reuse_address=true);
62+
std::string port = "8000";
63+
if (argc > 1) port = argv[1];
64+
server instance("127.0.0.1", port, handler, thread_pool, http::_reuse_address=true);
6365
instance.run();
6466
return 0;
6567
}

libs/network/test/http/server_async_less_copy.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ struct async_hello_world {
6767
int main(int argc, char * argv[]) {
6868
utils::thread_pool thread_pool(2);
6969
async_hello_world handler;
70-
server instance("127.0.0.1", "8000", handler, thread_pool, http::_reuse_address=true);
70+
std::string port = "8000";
71+
if (argc > 1) port = argv[1];
72+
server instance("127.0.0.1", port, handler, thread_pool, http::_reuse_address=true);
7173
instance.run();
7274
return 0;
7375
}

libs/network/test/http/server_hello_world.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ struct hello_world {
4343

4444
int main(int argc, char * argv[]) {
4545
hello_world handler;
46-
server server_("127.0.0.1", "8000", handler, http::_reuse_address=true);
46+
std::string port = "8000";
47+
if (argc > 1) port = argv[1];
48+
server server_("127.0.0.1", port, handler, http::_reuse_address=true);
4749
server_.run();
4850
return EXIT_SUCCESS;
4951
}

libs/network/test/httplib_acceptance.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import httplib2 as httplib
1111
from subprocess import Popen,PIPE
1212

13-
if len(argv) < 3:
14-
print('I need the executable to run.')
13+
if len(argv) < 4:
14+
print('I need the executable to run, the port to run it on, and the final touched file indicator.')
1515
exit(1)
1616

17-
print('Running {0}...'.format(argv[1]))
17+
print('Running {0} on port {1}...'.format(argv[1], argv[2]))
1818

1919
pipe = None
2020
try:
21-
pipe = Popen(argv[1], executable=argv[1], stdin=PIPE, stdout=PIPE, close_fds=True)
21+
pipe = Popen(args=[argv[1], argv[2]], executable=argv[1], stdin=PIPE, stdout=PIPE, close_fds=True)
2222
print('Done with spawning {0}.'.format(argv[1]))
2323
print('Sleeping to give the server a chance to run...')
2424
sleep(1)
@@ -58,7 +58,7 @@ def test_status(url, method, expected, headers={}, body=''):
5858
print('Caught Exception: {0}'.format(e))
5959
status = 1
6060

61-
url = 'http://localhost:8000/'
61+
url = 'http://localhost:{0}/'.format(argv[2])
6262
test(url, 'GET', expected)
6363
test(url, 'DELETE', expected)
6464
# Good request case, there's a content-length header for POST
@@ -72,10 +72,10 @@ def test_status(url, method, expected, headers={}, body=''):
7272

7373
if status != 0:
7474
print('Failures encountered.')
75-
pipe.kill()
75+
pipe.terminate()
7676
exit(status)
7777

7878
open(argv[len(argv) - 1], 'w').close()
7979
print('All tests pass.')
80-
pipe.kill()
80+
pipe.terminate()
8181

0 commit comments

Comments
 (0)