From d46ce7eb6362c66efde0b895479f4546be3004ac Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 23 Sep 2019 21:51:55 -0700 Subject: [PATCH] fix tsan errors on macOS when running the unittest --- ixsnake/ixsnake/IXSnakeProtocol.cpp | 24 +++++++--------------- ixsnake/ixsnake/IXSnakeServer.cpp | 30 +++++++++++++++------------- test/IXCobraChatTest.cpp | 13 ++++++------ test/IXCobraMetricsPublisherTest.cpp | 10 ++++------ third_party/jsoncpp/jsoncpp.cpp | 5 +++-- 5 files changed, 36 insertions(+), 46 deletions(-) diff --git a/ixsnake/ixsnake/IXSnakeProtocol.cpp b/ixsnake/ixsnake/IXSnakeProtocol.cpp index bebfc297..b2434d9b 100644 --- a/ixsnake/ixsnake/IXSnakeProtocol.cpp +++ b/ixsnake/ixsnake/IXSnakeProtocol.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace snake @@ -47,7 +48,6 @@ namespace snake }}}; auto serializedResponse = response.dump(); - std::cout << "response = " << serializedResponse << std::endl; ws->sendText(serializedResponse); } @@ -58,7 +58,6 @@ namespace snake const nlohmann::json& pdu) { auto secret = getRoleSecret(appConfig, state->appkey(), state->role()); - std::cout << "secret = " << secret << std::endl; if (secret.empty()) { @@ -74,12 +73,6 @@ namespace snake auto serverHash = ix::hmac(nonce, secret); std::string clientHash = pdu["body"]["credentials"]["hash"]; - if (appConfig.verbose) - { - std::cout << serverHash << std::endl; - std::cout << clientHash << std::endl; - } - if (serverHash != clientHash) { nlohmann::json response = { @@ -174,8 +167,6 @@ namespace snake return; } - std::cout << "Connected to redis host " << hostname << ":" << port << std::endl; - // Now authenticate, if needed if (!appConfig.redisPassword.empty()) { @@ -187,7 +178,6 @@ namespace snake handleError("rtm/subscribe", ws, pdu, ss.str()); return; } - std::cout << "Auth response: " << authResponse << ":" << port << std::endl; } int id = 0; @@ -205,8 +195,6 @@ namespace snake }; auto responseCallback = [ws, pdu, &subscriptionId](const std::string& redisResponse) { - std::cout << "Redis subscribe response: " << redisResponse << std::endl; - // Success nlohmann::json response = {{"action", "rtm/subscribe/ok"}, {"id", pdu.value("id", 1)}, @@ -214,7 +202,12 @@ namespace snake 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)) { std::stringstream ss; @@ -255,10 +248,7 @@ namespace snake const std::string& str) { auto pdu = nlohmann::json::parse(str); - std::cout << "Got " << str << std::endl; - auto action = pdu["action"]; - std::cout << "action = " << action << std::endl; if (action == "auth/handshake") { diff --git a/ixsnake/ixsnake/IXSnakeServer.cpp b/ixsnake/ixsnake/IXSnakeServer.cpp index 5052d38f..0b9db17b 100644 --- a/ixsnake/ixsnake/IXSnakeServer.cpp +++ b/ixsnake/ixsnake/IXSnakeServer.cpp @@ -11,6 +11,8 @@ #include "IXSnakeProtocol.h" #include #include +#include + namespace snake { @@ -43,8 +45,6 @@ namespace snake bool SnakeServer::run() { - std::cout << "Listening on " << _appConfig.hostname << ":" << _appConfig.port << std::endl; - auto factory = []() -> std::shared_ptr { return std::make_shared(); }; @@ -57,15 +57,16 @@ namespace snake webSocket->setOnMessageCallback( [this, webSocket, state](const ix::WebSocketMessagePtr& msg) { + std::stringstream ss; if (msg->type == ix::WebSocketMessageType::Open) { - std::cerr << "New connection" << std::endl; - std::cerr << "id: " << state->getId() << std::endl; - std::cerr << "Uri: " << msg->openInfo.uri << std::endl; - std::cerr << "Headers:" << std::endl; + ss << "New connection" << std::endl; + ss << "id: " << state->getId() << std::endl; + ss << "Uri: " << msg->openInfo.uri << std::endl; + ss << "Headers:" << std::endl; 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); @@ -75,14 +76,14 @@ namespace snake if (!state->redisClient().connect(_appConfig.redisHosts[0], _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) { - std::cerr << "Closed connection" - << " code " << msg->closeInfo.code << " reason " - << msg->closeInfo.reason << std::endl; + ss << "Closed connection" + << " code " << msg->closeInfo.code << " reason " + << msg->closeInfo.reason << std::endl; } else if (msg->type == ix::WebSocketMessageType::Error) { @@ -91,17 +92,18 @@ namespace snake ss << "#retries: " << msg->errorInfo.retries << std::endl; ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl; ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl; - std::cerr << ss.str(); } 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) { - std::cerr << "Received " << msg->wireSize << " bytes" << std::endl; + ss << "Received " << msg->wireSize << " bytes" << std::endl; processCobraMessage(state, webSocket, _appConfig, msg->str); } + + ix::IXCoreLogger::Log(ss.str().c_str()); }); }); diff --git a/test/IXCobraChatTest.cpp b/test/IXCobraChatTest.cpp index 92e160ef..77603063 100644 --- a/test/IXCobraChatTest.cpp +++ b/test/IXCobraChatTest.cpp @@ -125,7 +125,8 @@ namespace { std::string filter; _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.isMember("user")) return; if (!msg.isMember("text")) return; @@ -335,15 +336,13 @@ TEST_CASE("Cobra_chat", "[cobra_chat]") REQUIRE(chatA.getReceivedMessagesCount() == 2); REQUIRE(chatB.getReceivedMessagesCount() == 3); - std::cout << "Incoming bytes: " << incomingBytes << std::endl; - std::cout << "Outgoing bytes: " << outgoingBytes << std::endl; + spdlog::info("Incoming bytes {}", incomingBytes); + spdlog::info("Outgoing bytes {}", outgoingBytes); - std::cerr << "Stopping snake server... "; + spdlog::info("Stopping snake server..."); snakeServer.stop(); - std::cerr << "OK" << std::endl; - std::cerr << "Stopping redis server... "; + spdlog::info("Stopping redis server..."); redisServer.stop(); - std::cerr << "OK" << std::endl; } } diff --git a/test/IXCobraMetricsPublisherTest.cpp b/test/IXCobraMetricsPublisherTest.cpp index 74b55a72..d4b64d2d 100644 --- a/test/IXCobraMetricsPublisherTest.cpp +++ b/test/IXCobraMetricsPublisherTest.cpp @@ -267,14 +267,12 @@ TEST_CASE("Cobra_Metrics_Publisher", "[cobra]") CHECK(gIds.count("sms_set_rate_control_id") == 1); CHECK(gIds.count("sms_set_blacklist_id") == 1); - std::cout << "Incoming bytes: " << incomingBytes << std::endl; - std::cout << "Outgoing bytes: " << outgoingBytes << std::endl; + spdlog::info("Incoming bytes {}", incomingBytes); + spdlog::info("Outgoing bytes {}", outgoingBytes); - std::cerr << "Stopping snake server... "; + spdlog::info("Stopping snake server..."); snakeServer.stop(); - std::cerr << "OK" << std::endl; - std::cerr << "Stopping redis server... "; + spdlog::info("Stopping redis server..."); redisServer.stop(); - std::cerr << "OK" << std::endl; } diff --git a/third_party/jsoncpp/jsoncpp.cpp b/third_party/jsoncpp/jsoncpp.cpp index 297d13c1..516d57d7 100644 --- a/third_party/jsoncpp/jsoncpp.cpp +++ b/third_party/jsoncpp/jsoncpp.cpp @@ -205,6 +205,7 @@ static inline void fixNumericLocale(char* begin, char* end) { #include #include #include +#include #if defined(__BORLANDC__) #include #endif @@ -227,8 +228,8 @@ static inline void fixNumericLocale(char* begin, char* end) { #pragma warning(disable : 4996) #endif -static int const stackLimit_g = 1000; -static int stackDepth_g = 0; // see readValue() +static std::atomic const stackLimit_g(1000); +static std::atomic stackDepth_g(0); // see readValue() namespace Json {