2020-03-23 03:36:29 +01:00
|
|
|
/*
|
|
|
|
* IXCobraToStatsdTest.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2020 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXTest.h"
|
|
|
|
#include "catch.hpp"
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
#include <ixbots/IXCobraToStatsdBot.h>
|
|
|
|
#include <ixcobra/IXCobraConnection.h>
|
|
|
|
#include <ixcobra/IXCobraMetricsPublisher.h>
|
|
|
|
#include <ixcrypto/IXUuid.h>
|
2020-06-11 07:30:55 +02:00
|
|
|
#include <ixredis/IXRedisServer.h>
|
2020-07-08 19:39:46 +02:00
|
|
|
#include <ixsentry/IXSentryClient.h>
|
2020-03-23 03:36:29 +01:00
|
|
|
#include <ixsnake/IXSnakeServer.h>
|
|
|
|
#include <ixwebsocket/IXHttpServer.h>
|
|
|
|
#include <ixwebsocket/IXUserAgent.h>
|
|
|
|
|
|
|
|
using namespace ix;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void runPublisher(const ix::CobraConfig& config, const std::string& channel)
|
|
|
|
{
|
|
|
|
ix::CobraMetricsPublisher cobraMetricsPublisher;
|
|
|
|
cobraMetricsPublisher.configure(config, channel);
|
|
|
|
cobraMetricsPublisher.setSession(uuid4());
|
|
|
|
cobraMetricsPublisher.enable(true);
|
|
|
|
|
|
|
|
Json::Value msg;
|
|
|
|
msg["fps"] = 60;
|
|
|
|
|
|
|
|
cobraMetricsPublisher.setGenericAttributes("game", "ody");
|
|
|
|
|
|
|
|
// Wait a bit
|
|
|
|
ix::msleep(500);
|
|
|
|
|
|
|
|
// publish some messages
|
|
|
|
cobraMetricsPublisher.push("sms_metric_A_id", msg); // (msg #1)
|
|
|
|
cobraMetricsPublisher.push("sms_metric_B_id", msg); // (msg #2)
|
|
|
|
ix::msleep(500);
|
|
|
|
|
|
|
|
cobraMetricsPublisher.push("sms_metric_A_id", msg); // (msg #3)
|
|
|
|
cobraMetricsPublisher.push("sms_metric_D_id", msg); // (msg #4)
|
|
|
|
ix::msleep(500);
|
|
|
|
|
|
|
|
cobraMetricsPublisher.push("sms_metric_A_id", msg); // (msg #4)
|
|
|
|
cobraMetricsPublisher.push("sms_metric_F_id", msg); // (msg #5)
|
|
|
|
ix::msleep(500);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_CASE("Cobra_to_statsd_bot", "[cobra_bots]")
|
|
|
|
{
|
|
|
|
SECTION("Exchange and count sent/received messages.")
|
|
|
|
{
|
|
|
|
int port = getFreePort();
|
|
|
|
snake::AppConfig appConfig = makeSnakeServerConfig(port, true);
|
|
|
|
|
|
|
|
// Start a redis server
|
|
|
|
ix::RedisServer redisServer(appConfig.redisPort);
|
|
|
|
auto res = redisServer.listen();
|
|
|
|
REQUIRE(res.first);
|
|
|
|
redisServer.start();
|
|
|
|
|
|
|
|
// Start a snake server
|
|
|
|
snake::SnakeServer snakeServer(appConfig);
|
|
|
|
snakeServer.run();
|
|
|
|
|
|
|
|
// Start a fake statsd server (ultimately)
|
|
|
|
|
|
|
|
// Run the bot for a small amount of time
|
|
|
|
std::string channel = ix::generateSessionId();
|
|
|
|
std::string appkey("FC2F10139A2BAc53BB72D9db967b024f");
|
|
|
|
std::string role = "_sub";
|
|
|
|
std::string secret = "66B1dA3ED5fA074EB5AE84Dd8CE3b5ba";
|
|
|
|
std::string endpoint = makeCobraEndpoint(port, true);
|
|
|
|
|
|
|
|
ix::CobraConfig config;
|
|
|
|
config.endpoint = endpoint;
|
|
|
|
config.appkey = appkey;
|
|
|
|
config.rolename = role;
|
|
|
|
config.rolesecret = secret;
|
|
|
|
config.socketTLSOptions = makeClientTLSOptions();
|
|
|
|
|
|
|
|
std::thread publisherThread(runPublisher, config, channel);
|
|
|
|
|
2020-05-13 04:08:16 +02:00
|
|
|
ix::CobraBotConfig cobraBotConfig;
|
|
|
|
cobraBotConfig.cobraConfig = config;
|
|
|
|
cobraBotConfig.channel = channel;
|
|
|
|
cobraBotConfig.runtime = 3; // Only run the bot for 3 seconds
|
|
|
|
cobraBotConfig.enableHeartbeat = false;
|
2020-03-23 03:36:29 +01:00
|
|
|
|
|
|
|
std::string hostname("127.0.0.1");
|
|
|
|
// std::string hostname("www.google.com");
|
|
|
|
int statsdPort = 8125;
|
|
|
|
std::string prefix("ix.test");
|
|
|
|
StatsdClient statsdClient(hostname, statsdPort, prefix);
|
|
|
|
|
|
|
|
std::string errMsg;
|
|
|
|
bool initialized = statsdClient.init(errMsg);
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
spdlog::error(errMsg);
|
|
|
|
}
|
|
|
|
REQUIRE(initialized);
|
|
|
|
|
|
|
|
std::string fields("device.game\ndevice.os_name");
|
2020-03-30 04:31:49 +02:00
|
|
|
std::string gauge;
|
2020-03-31 01:08:47 +02:00
|
|
|
std::string timer;
|
2020-05-13 04:08:16 +02:00
|
|
|
bool verbose = true;
|
2020-03-23 03:36:29 +01:00
|
|
|
|
2020-05-13 06:40:17 +02:00
|
|
|
int64_t sentCount =
|
|
|
|
ix::cobra_to_statsd_bot(cobraBotConfig, statsdClient, fields, gauge, timer, verbose);
|
2020-03-23 03:36:29 +01:00
|
|
|
//
|
|
|
|
// We want at least 2 messages to be sent
|
|
|
|
//
|
|
|
|
REQUIRE(sentCount >= 2);
|
|
|
|
|
|
|
|
// Give us 1s for all messages to be received
|
|
|
|
ix::msleep(1000);
|
|
|
|
|
|
|
|
spdlog::info("Stopping snake server...");
|
|
|
|
snakeServer.stop();
|
|
|
|
|
|
|
|
spdlog::info("Stopping redis server...");
|
|
|
|
redisServer.stop();
|
|
|
|
|
|
|
|
publisherThread.join();
|
|
|
|
}
|
|
|
|
}
|