fix tsan errors on macOS when running the unittest

This commit is contained in:
Benjamin Sergeant 2019-09-23 21:51:55 -07:00
parent 169e225ccd
commit d46ce7eb63
5 changed files with 36 additions and 46 deletions

View File

@ -12,6 +12,7 @@
#include <iostream> #include <iostream>
#include <ixcrypto/IXHMac.h> #include <ixcrypto/IXHMac.h>
#include <ixwebsocket/IXWebSocket.h> #include <ixwebsocket/IXWebSocket.h>
#include <ixcore/utils/IXCoreLogger.h>
#include <sstream> #include <sstream>
namespace snake namespace snake
@ -47,7 +48,6 @@ namespace snake
}}}; }}};
auto serializedResponse = response.dump(); auto serializedResponse = response.dump();
std::cout << "response = " << serializedResponse << std::endl;
ws->sendText(serializedResponse); ws->sendText(serializedResponse);
} }
@ -58,7 +58,6 @@ namespace snake
const nlohmann::json& pdu) const nlohmann::json& pdu)
{ {
auto secret = getRoleSecret(appConfig, state->appkey(), state->role()); auto secret = getRoleSecret(appConfig, state->appkey(), state->role());
std::cout << "secret = " << secret << std::endl;
if (secret.empty()) if (secret.empty())
{ {
@ -74,12 +73,6 @@ namespace snake
auto serverHash = ix::hmac(nonce, secret); auto serverHash = ix::hmac(nonce, secret);
std::string clientHash = pdu["body"]["credentials"]["hash"]; std::string clientHash = pdu["body"]["credentials"]["hash"];
if (appConfig.verbose)
{
std::cout << serverHash << std::endl;
std::cout << clientHash << std::endl;
}
if (serverHash != clientHash) if (serverHash != clientHash)
{ {
nlohmann::json response = { nlohmann::json response = {
@ -174,8 +167,6 @@ namespace snake
return; return;
} }
std::cout << "Connected to redis host " << hostname << ":" << port << std::endl;
// Now authenticate, if needed // Now authenticate, if needed
if (!appConfig.redisPassword.empty()) if (!appConfig.redisPassword.empty())
{ {
@ -187,7 +178,6 @@ namespace snake
handleError("rtm/subscribe", ws, pdu, ss.str()); handleError("rtm/subscribe", ws, pdu, ss.str());
return; return;
} }
std::cout << "Auth response: " << authResponse << ":" << port << std::endl;
} }
int id = 0; int id = 0;
@ -205,8 +195,6 @@ namespace snake
}; };
auto responseCallback = [ws, pdu, &subscriptionId](const std::string& redisResponse) { auto responseCallback = [ws, pdu, &subscriptionId](const std::string& redisResponse) {
std::cout << "Redis subscribe response: " << redisResponse << std::endl;
// Success // Success
nlohmann::json response = {{"action", "rtm/subscribe/ok"}, nlohmann::json response = {{"action", "rtm/subscribe/ok"},
{"id", pdu.value("id", 1)}, {"id", pdu.value("id", 1)},
@ -214,7 +202,12 @@ namespace snake
ws->sendText(response.dump()); ws->sendText(response.dump());
}; };
std::cerr << "Subscribing to " << appChannel << "..." << std::endl; {
std::stringstream ss;
ss << "Subscribing to " << appChannel << "...";
ix::IXCoreLogger::Log(ss.str().c_str());
}
if (!redisClient.subscribe(appChannel, responseCallback, callback)) if (!redisClient.subscribe(appChannel, responseCallback, callback))
{ {
std::stringstream ss; std::stringstream ss;
@ -255,10 +248,7 @@ namespace snake
const std::string& str) const std::string& str)
{ {
auto pdu = nlohmann::json::parse(str); auto pdu = nlohmann::json::parse(str);
std::cout << "Got " << str << std::endl;
auto action = pdu["action"]; auto action = pdu["action"];
std::cout << "action = " << action << std::endl;
if (action == "auth/handshake") if (action == "auth/handshake")
{ {

View File

@ -11,6 +11,8 @@
#include "IXSnakeProtocol.h" #include "IXSnakeProtocol.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <ixcore/utils/IXCoreLogger.h>
namespace snake namespace snake
{ {
@ -43,8 +45,6 @@ namespace snake
bool SnakeServer::run() bool SnakeServer::run()
{ {
std::cout << "Listening on " << _appConfig.hostname << ":" << _appConfig.port << std::endl;
auto factory = []() -> std::shared_ptr<ix::ConnectionState> { auto factory = []() -> std::shared_ptr<ix::ConnectionState> {
return std::make_shared<SnakeConnectionState>(); return std::make_shared<SnakeConnectionState>();
}; };
@ -57,15 +57,16 @@ namespace snake
webSocket->setOnMessageCallback( webSocket->setOnMessageCallback(
[this, webSocket, state](const ix::WebSocketMessagePtr& msg) { [this, webSocket, state](const ix::WebSocketMessagePtr& msg) {
std::stringstream ss;
if (msg->type == ix::WebSocketMessageType::Open) if (msg->type == ix::WebSocketMessageType::Open)
{ {
std::cerr << "New connection" << std::endl; ss << "New connection" << std::endl;
std::cerr << "id: " << state->getId() << std::endl; ss << "id: " << state->getId() << std::endl;
std::cerr << "Uri: " << msg->openInfo.uri << std::endl; ss << "Uri: " << msg->openInfo.uri << std::endl;
std::cerr << "Headers:" << std::endl; ss << "Headers:" << std::endl;
for (auto it : msg->openInfo.headers) for (auto it : msg->openInfo.headers)
{ {
std::cerr << it.first << ": " << it.second << std::endl; ss << it.first << ": " << it.second << std::endl;
} }
std::string appkey = parseAppKey(msg->openInfo.uri); std::string appkey = parseAppKey(msg->openInfo.uri);
@ -75,12 +76,12 @@ namespace snake
if (!state->redisClient().connect(_appConfig.redisHosts[0], if (!state->redisClient().connect(_appConfig.redisHosts[0],
_appConfig.redisPort)) _appConfig.redisPort))
{ {
std::cerr << "Cannot connect to redis host" << std::endl; ss << "Cannot connect to redis host" << std::endl;
} }
} }
else if (msg->type == ix::WebSocketMessageType::Close) else if (msg->type == ix::WebSocketMessageType::Close)
{ {
std::cerr << "Closed connection" ss << "Closed connection"
<< " code " << msg->closeInfo.code << " reason " << " code " << msg->closeInfo.code << " reason "
<< msg->closeInfo.reason << std::endl; << msg->closeInfo.reason << std::endl;
} }
@ -91,17 +92,18 @@ namespace snake
ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "#retries: " << msg->errorInfo.retries << std::endl;
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
std::cerr << ss.str();
} }
else if (msg->type == ix::WebSocketMessageType::Fragment) else if (msg->type == ix::WebSocketMessageType::Fragment)
{ {
std::cerr << "Received message fragment" << std::endl; ss << "Received message fragment" << std::endl;
} }
else if (msg->type == ix::WebSocketMessageType::Message) else if (msg->type == ix::WebSocketMessageType::Message)
{ {
std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; ss << "Received " << msg->wireSize << " bytes" << std::endl;
processCobraMessage(state, webSocket, _appConfig, msg->str); processCobraMessage(state, webSocket, _appConfig, msg->str);
} }
ix::IXCoreLogger::Log(ss.str().c_str());
}); });
}); });

View File

@ -125,7 +125,8 @@ namespace
{ {
std::string filter; std::string filter;
_conn.subscribe(channel, filter, [this](const Json::Value& msg) { _conn.subscribe(channel, filter, [this](const Json::Value& msg) {
std::cout << msg.toStyledString() << std::endl; spdlog::info("receive {}", msg.toStyledString());
if (!msg.isObject()) return; if (!msg.isObject()) return;
if (!msg.isMember("user")) return; if (!msg.isMember("user")) return;
if (!msg.isMember("text")) return; if (!msg.isMember("text")) return;
@ -335,15 +336,13 @@ TEST_CASE("Cobra_chat", "[cobra_chat]")
REQUIRE(chatA.getReceivedMessagesCount() == 2); REQUIRE(chatA.getReceivedMessagesCount() == 2);
REQUIRE(chatB.getReceivedMessagesCount() == 3); REQUIRE(chatB.getReceivedMessagesCount() == 3);
std::cout << "Incoming bytes: " << incomingBytes << std::endl; spdlog::info("Incoming bytes {}", incomingBytes);
std::cout << "Outgoing bytes: " << outgoingBytes << std::endl; spdlog::info("Outgoing bytes {}", outgoingBytes);
std::cerr << "Stopping snake server... "; spdlog::info("Stopping snake server...");
snakeServer.stop(); snakeServer.stop();
std::cerr << "OK" << std::endl;
std::cerr << "Stopping redis server... "; spdlog::info("Stopping redis server...");
redisServer.stop(); redisServer.stop();
std::cerr << "OK" << std::endl;
} }
} }

View File

@ -267,14 +267,12 @@ TEST_CASE("Cobra_Metrics_Publisher", "[cobra]")
CHECK(gIds.count("sms_set_rate_control_id") == 1); CHECK(gIds.count("sms_set_rate_control_id") == 1);
CHECK(gIds.count("sms_set_blacklist_id") == 1); CHECK(gIds.count("sms_set_blacklist_id") == 1);
std::cout << "Incoming bytes: " << incomingBytes << std::endl; spdlog::info("Incoming bytes {}", incomingBytes);
std::cout << "Outgoing bytes: " << outgoingBytes << std::endl; spdlog::info("Outgoing bytes {}", outgoingBytes);
std::cerr << "Stopping snake server... "; spdlog::info("Stopping snake server...");
snakeServer.stop(); snakeServer.stop();
std::cerr << "OK" << std::endl;
std::cerr << "Stopping redis server... "; spdlog::info("Stopping redis server...");
redisServer.stop(); redisServer.stop();
std::cerr << "OK" << std::endl;
} }

View File

@ -205,6 +205,7 @@ static inline void fixNumericLocale(char* begin, char* end) {
#include <memory> #include <memory>
#include <set> #include <set>
#include <limits> #include <limits>
#include <atomic>
#if defined(__BORLANDC__) #if defined(__BORLANDC__)
#include <stdio.h> #include <stdio.h>
#endif #endif
@ -227,8 +228,8 @@ static inline void fixNumericLocale(char* begin, char* end) {
#pragma warning(disable : 4996) #pragma warning(disable : 4996)
#endif #endif
static int const stackLimit_g = 1000; static std::atomic<int> const stackLimit_g(1000);
static int stackDepth_g = 0; // see readValue() static std::atomic<int> stackDepth_g(0); // see readValue()
namespace Json { namespace Json {