Skip to content

Commit b743662

Browse files
committed
Fix HTTP server unit test on Mac
1 parent b7a37f1 commit b743662

File tree

10 files changed

+101
-35
lines changed

10 files changed

+101
-35
lines changed

build-tests.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/sh
2-
./build.sh
2+
cd "${0%/*}"
3+
SKU=${1:-release}
4+
echo Building and running $SKU tests...
5+
./build.sh ${SKU}
36
# Fail on test errors
47
set -e
58
cd out

lib/system/EventProperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace ARIASDK_NS_BEGIN {
9191
return *this;
9292
}
9393

94-
EventProperties::~EventProperties()
94+
EventProperties::~EventProperties() noexcept
9595
{
9696
delete m_storage;
9797
}

tests/common/DebugConsole.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
#ifndef TESTS_COMMON_DEBUGCONSOLE_HPP_
3+
#define TESTS_COMMON_DEBUGCONSOLE_HPP_
4+
5+
#ifndef __FUNCSIG__
6+
#define __FUNCSIG__ __FUNCTION__
7+
#endif
8+
9+
#if defined(HAVE_CONSOLE_LOG) && !defined(LOG_DEBUG)
10+
/* Log to console if there's no standard log facility defined */
11+
# include <cstdio>
12+
# ifndef LOG_DEBUG
13+
# define LOG_DEBUG(fmt_, ...) fprintf(stdout, "%s: " fmt_ "\n", __FUNCSIG__ , ## __VA_ARGS__)
14+
# define LOG_TRACE(fmt_, ...) fprintf(stdout, "%s: " fmt_ "\n", __FUNCSIG__ , ## __VA_ARGS__)
15+
# define LOG_INFO(fmt_, ...) fprintf(stdout, "%s: " fmt_ "\n", __FUNCSIG__ , ## __VA_ARGS__)
16+
# define LOG_WARN(fmt_, ...) fprintf(stderr, "%s: " fmt_ "\n", __FUNCSIG__ , ## __VA_ARGS__)
17+
# define LOG_ERROR(fmt_, ...) fprintf(stderr, "%s: " fmt_ "\n", __FUNCSIG__ , ## __VA_ARGS__)
18+
# endif
19+
#endif
20+
21+
#ifndef LOG_DEBUG
22+
/* Don't log anything if there's no standard log facility defined */
23+
# define LOG_DEBUG(fmt_, ...)
24+
# define LOG_TRACE(fmt_, ...)
25+
# define LOG_INFO(fmt_, ...)
26+
# define LOG_WARN(fmt_, ...)
27+
# define LOG_ERROR(fmt_, ...)
28+
#endif
29+
30+
#endif /* TESTS_COMMON_DEBUGCONSOLE_HPP_ */

tests/common/HttpServer.hpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class HttpServer : private Reactor::Callback
6363
};
6464

6565
protected:
66-
bool allowKeepalive;
66+
std::string m_serverHost;
67+
bool allowKeepalive { true };
6768
Reactor m_reactor;
6869
std::list<Socket> m_listeningSockets;
6970
std::list<std::pair<std::string, Callback*>> m_handlers;
7071
std::map<Socket, Connection> m_connections;
7172
size_t m_maxRequestHeadersSize, m_maxRequestContentSize;
72-
std::string m_serverHost;
7373

7474
// Map of killed token to kill duration of that token
7575
// Ideally we need a map of string --> std::pair<std::string /* suffix */, uint64_t /* duration */
@@ -93,11 +93,11 @@ class HttpServer : private Reactor::Callback
9393
}
9494

9595
HttpServer()
96-
: m_reactor(*this),
96+
: m_serverHost("unnamed"),
97+
allowKeepalive(true),
98+
m_reactor(*this),
9799
m_maxRequestHeadersSize(8192),
98-
m_maxRequestContentSize(1048576),
99-
m_serverHost("unnamed"),
100-
allowKeepalive(true)
100+
m_maxRequestContentSize(2 * 1024 * 1024)
101101
{
102102
}
103103

@@ -156,6 +156,7 @@ class HttpServer : private Reactor::Callback
156156
protected:
157157
virtual void onSocketAcceptable(Socket socket) override
158158
{
159+
LOG_TRACE("HttpServer: accepting socket fd=0x%x", socket.m_sock);
159160
assert(std::find(m_listeningSockets.begin(), m_listeningSockets.end(), socket) != m_listeningSockets.end());
160161

161162
Socket csocket;
@@ -173,6 +174,7 @@ class HttpServer : private Reactor::Callback
173174

174175
virtual void onSocketReadable(Socket socket) override
175176
{
177+
LOG_TRACE("HttpServer: reading socket fd=0x%x", socket.m_sock);
176178
assert(std::find(m_listeningSockets.begin(), m_listeningSockets.end(), socket) == m_listeningSockets.end());
177179

178180
auto connIt = m_connections.find(socket);
@@ -181,7 +183,7 @@ class HttpServer : private Reactor::Callback
181183
}
182184
Connection& conn = connIt->second;
183185

184-
char buffer[2048];
186+
char buffer[2048] = { 0 };
185187
int received = socket.recv(buffer, sizeof(buffer));
186188
LOG_TRACE("HttpServer: [%s] received %d", conn.request.client.c_str(), received);
187189
if (received <= 0) {
@@ -195,6 +197,7 @@ class HttpServer : private Reactor::Callback
195197

196198
virtual void onSocketWritable(Socket socket) override
197199
{
200+
LOG_TRACE("HttpServer: writing socket fd=0x%x", socket.m_sock);
198201
assert(std::find(m_listeningSockets.begin(), m_listeningSockets.end(), socket) == m_listeningSockets.end());
199202

200203
auto connIt = m_connections.find(socket);
@@ -210,6 +213,7 @@ class HttpServer : private Reactor::Callback
210213

211214
virtual void onSocketClosed(Socket socket) override
212215
{
216+
LOG_TRACE("HttpServer: closing socket fd=0x%x", socket.m_sock);
213217
assert(std::find(m_listeningSockets.begin(), m_listeningSockets.end(), socket) == m_listeningSockets.end());
214218

215219
auto connIt = m_connections.find(socket);

tests/common/Reactor.cpp

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ namespace SocketTools {
3030
#ifdef TARGET_OS_MAC
3131
struct kevent event;
3232
bzero(&event, sizeof(event));
33-
event.ident = socket;
34-
EV_SET(&event, socket, EVFILT_READ, EV_ADD, 0, 0, NULL);
35-
assert(-1 != kevent(kq, &event, 1, NULL, 0, NULL));
33+
event.ident = socket.m_sock;
34+
EV_SET(&event, event.ident, EVFILT_READ, EV_ADD, 0, 0, NULL);
35+
kevent(kq, &event, 1, NULL, 0, NULL);
36+
EV_SET(&event, event.ident, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
37+
kevent(kq, &event, 1, NULL, 0, NULL);
3638
#endif
3739
m_sockets.push_back(SocketData());
3840
m_sockets.back().socket = socket;
@@ -117,7 +119,13 @@ namespace SocketTools {
117119
if (-1 == kevent(kq, &event, 1, NULL, 0, NULL))
118120
{
119121
//// Already removed?
120-
// LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
122+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
123+
}
124+
EV_SET(&event, socket, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
125+
if (-1 == kevent(kq, &event, 1, NULL, 0, NULL))
126+
{
127+
//// Already removed?
128+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
121129
}
122130
#endif
123131
m_sockets.erase(it);
@@ -153,7 +161,13 @@ namespace SocketTools {
153161
if (-1 == kevent(kq, &event, 1, NULL, 0, NULL))
154162
{
155163
//// Already removed?
156-
// LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
164+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
165+
}
166+
EV_SET(&event, sd.socket, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
167+
if (-1 == kevent(kq, &event, 1, NULL, 0, NULL))
168+
{
169+
//// Already removed?
170+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
157171
}
158172
#endif
159173
}
@@ -255,21 +269,8 @@ namespace SocketTools {
255269
Socket socket = it->socket;
256270
int flags = it->flags;
257271

258-
// LOG_TRACE("Reactor: Handling socket %d active flags 0x%x (armed 0x%x)", static_cast<int>(socket), event.flags, event.fflags);
259-
260-
if ((event.flags & EV_EOF)||(event.flags & EV_ERROR))
261-
{
262-
m_callback.onSocketClosed(socket);
263-
it->flags=Closed;
264-
struct kevent kevt;
265-
EV_SET(&kevt, event.ident, EVFILT_READ, EV_DELETE, 0, 0, NULL);
266-
if (-1 == kevent(kq, &kevt, 1, NULL, 0, NULL))
267-
{
268-
//// Already removed?
269-
// LOG_ERROR("cannot delete fd=%d from kqueue!", event.ident);
270-
}
271-
continue;
272-
}
272+
LOG_TRACE("Handling socket %d active flags 0x%08x (armed 0x%08x)",
273+
static_cast<int>(socket), event.flags, event.fflags);
273274

274275
if (event.filter==EVFILT_READ)
275276
{
@@ -292,6 +293,25 @@ namespace SocketTools {
292293
}
293294
continue;
294295
}
296+
297+
if ((event.flags & EV_EOF)||(event.flags & EV_ERROR))
298+
{
299+
LOG_TRACE("event.filter=%s", "EVFILT_WRITE");
300+
m_callback.onSocketClosed(socket);
301+
it->flags=Closed;
302+
struct kevent kevt;
303+
EV_SET(&kevt, event.ident, EVFILT_READ, EV_DELETE, 0, 0, NULL);
304+
if (-1 == kevent(kq, &kevt, 1, NULL, 0, NULL))
305+
{
306+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
307+
}
308+
EV_SET(&kevt, event.ident, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
309+
if (-1 == kevent(kq, &kevt, 1, NULL, 0, NULL))
310+
{
311+
LOG_TRACE("cannot delete fd=%d from kqueue!", event.ident);
312+
}
313+
continue;
314+
}
295315
LOG_ERROR("Reactor: unhandled kevent!");
296316
}
297317
#endif

tests/common/Reactor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Reactor : protected Thread
5151
#ifdef TARGET_OS_MAC
5252
/* use kqueue on Mac */
5353
#define KQUEUE_SIZE 32
54-
int kq;
54+
int kq { 0 };
5555
struct kevent m_events[KQUEUE_SIZE];
5656
#endif
5757

tests/common/SocketTools.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ class Socket
213213
static Type const Invalid = -1;
214214
#endif
215215

216-
protected:
217216
Type m_sock;
218217

219218
public:
@@ -310,7 +309,8 @@ class Socket
310309
int recv(_Out_cap_(size) void* buffer, unsigned size)
311310
{
312311
assert(m_sock != Invalid);
313-
return ::recv(m_sock, reinterpret_cast<char*>(buffer), size, 0);
312+
int flags = 0;
313+
return ::recv(m_sock, reinterpret_cast<char*>(buffer), size, flags);
314314
}
315315

316316
int send(void const* buffer, unsigned size)
@@ -399,7 +399,7 @@ class Thread
399399
{
400400
private:
401401
std::thread m_thread;
402-
volatile bool m_terminate;
402+
volatile bool m_terminate { false };
403403

404404
protected:
405405
Thread()

tests/functests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.externalToolBuilders/

tests/unittests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.externalToolBuilders/

tests/unittests/HttpServerTests.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
#ifndef WIN32_LEAN_AND_MEAN
33
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
44
#endif
5+
6+
#include <chrono>
7+
58
#include "common/Common.hpp"
69
#include "common/HttpServer.hpp"
710

8-
using namespace testing;
11+
#ifdef HAVE_CONSOLE_LOG
12+
#undef LOG_DEBUG
13+
#include "common/DebugConsole.hpp"
14+
#endif
915

16+
using namespace testing;
1017

1118
class HttpServerTestsBase : public ::testing::Test
1219
{
1320
protected:
1421
HttpServer server;
15-
int port;
22+
int port { 0 };
1623
Socket clientSocket;
1724

1825
public:

0 commit comments

Comments
 (0)