Skip to content

Commit

Permalink
Integration tests to show match voting works
Browse files Browse the repository at this point in the history
  • Loading branch information
albert-wang committed Nov 30, 2024
1 parent 5ba6588 commit 418f078
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
1 change: 0 additions & 1 deletion schema/matches.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ namespace gamelink
struct MUXY_GAMELINK_API MatchPollUpdate : ReceiveEnvelope<MatchPollUpdateInformation>
{};


struct MatchPollUpdateInformationInternal
{
string matchId;
Expand Down
52 changes: 49 additions & 3 deletions test/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ TEST_CASE_METHOD(IntegrationTestFixture, "Transactions Support", "[.][integratio
REQUIRE(calls == 6);
}


TEST_CASE_METHOD(IntegrationTestFixture, "Transactions Support through gateway", "[.][integration][t]")
{
Connect();
Expand All @@ -384,14 +383,14 @@ TEST_CASE_METHOD(IntegrationTestFixture, "Transactions Support through gateway",
// This should get 1 call, to purchase 50 coins.
REQUIRE(used.SKU == "muxy-bits-50");
REQUIRE(used.Bits == 50);

bitsCalls++;
});

size_t coinCalls = 0;
gateway.OnActionUsed([&](const gateway::ActionUsed& used)
{
// This should get 5 calls, which are all coins
// This should get 5 calls, which are all coins
REQUIRE(used.ActionID == "costs-ten");
REQUIRE(used.Cost == 10);

Expand Down Expand Up @@ -519,6 +518,53 @@ TEST_CASE_METHOD(IntegrationTestFixture, "Datastream operations", "[.][integrati
REQUIRE(events == 6);
}

TEST_CASE_METHOD(IntegrationTestFixture, "Matches and polls", "[.][integration]")
{
Connect();

sdk.CreateMatch("my-cool-match");

sdk.AddChannelsToMatch("my-cool-match", {
"26052853", "89319907", "89368629", "89368745", "124708734"
});

size_t updateCalls = 0;
auto update = [&updateCalls](const gamelink::schema::MatchPollUpdate& update)
{
updateCalls++;
};

size_t finishCalls = 0;
auto finish = [&finishCalls](const gamelink::schema::MatchPollUpdate& finish)
{
finishCalls++;
};

gamelink::PollConfiguration config;
config.startsIn = 0;
config.endsIn = 5;

gamelink::RequestId waiter = sdk.RunMatchPoll("my-cool-match", "what-to-eat",
"How many pizzas should I buy?",
config,
{ "one", "two", "twenty" },
update,
finish
);

Sleep(1);

nlohmann::json voteValue;
voteValue["value"] = 1;
nlohmann::json unused;

Request("POST", "vote?id=what-to-eat", &voteValue, &unused);
Sleep(6);

REQUIRE(updateCalls > 0);
REQUIRE(finishCalls == 1);
}

TEST_CASE_METHOD(IntegrationTestFixture, "Client IP access", "[.][integration]")
{
int code;
Expand Down
46 changes: 22 additions & 24 deletions test/integration_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void IntegrationTestFixture::Connect()
std::string token = resp["token"];

gamelink::RequestId req = sdk.AuthenticateWithGameIDAndPIN(
gamelink::string(client.c_str()),
gamelink::string(client.c_str()),
gamelink::string("Mystery Fun House"),
gamelink::string(token.c_str())
);
Expand All @@ -68,7 +68,7 @@ void IntegrationTestFixture::Connect()
else
{
gamelink::RequestId req = sdk.AuthenticateWithGameIDAndRefreshToken(
gamelink::string(client.c_str()),
gamelink::string(client.c_str()),
gamelink::string("Mystery Fun House"),
gamelink::string(refreshToken.c_str())
);
Expand Down Expand Up @@ -251,28 +251,28 @@ void IntegrationTestFixture::LoadEnvironment()
}

target = "sandbox";
targetDomain = "sandbox.muxy.io";
apiUrl = "https://sandbox.muxy.io";
websocketUrl = "gamelink.sandbox.muxy.io";

const char * targetEnv = std::getenv("MUXY_INTEGRATION_TARGET");
if (targetEnv)
{
if (std::string(targetEnv) != "production")
target = std::string(targetEnv);
if (target == "localhost")
{
target = std::string(targetEnv);
targetDomain = target + ".muxy.io";
} else
apiUrl = "http://localhost:5050";
websocketUrl = "localhost:5051";
}
else if (std::string(targetEnv) != "production")
{
target = "production";
targetDomain = "muxy.io";
apiUrl = "https://" + target + ".muxy.io";
websocketUrl = "gamelink." + target + ".muxy.io";
}
else
{
apiUrl = "https://api.muxy.io";
websocketUrl = "gamelink.muxy.io";
}
}
REQUIRE(targetDomain.size());

targetPrefix = "";
const char * prefixEnv = std::getenv("MUXY_INTEGRATION_OLD_URLS");
if (prefixEnv && std::string(prefixEnv) == "true")
{
targetPrefix = target + ".";
targetDomain = "muxy.io";
}

signature = "";
Expand All @@ -288,9 +288,8 @@ void IntegrationTestFixture::Reconnect()
ForceDisconnect();

char buffer[256];
int output = snprintf(buffer, 256, "%sgamelink.%s/%d.%d.%d/%s",
targetPrefix.c_str(),
targetDomain.c_str(),
int output = snprintf(buffer, 256, "%s/%d.%d.%d/%s",
websocketUrl.c_str(),
MUXY_GAMELINK_VERSION_MAJOR, MUXY_GAMELINK_VERSION_MINOR, MUXY_GAMELINK_VERSION_PATCH,
client.c_str());

Expand Down Expand Up @@ -342,9 +341,8 @@ int IntegrationTestFixture::Request(const char* method, const char* endpoint, co
}

char buffer[256];
int writtenBytes = snprintf(buffer, 256, "https://%sapi.%s/v1/e/%s",
targetPrefix.c_str(),
targetDomain.c_str(),
int writtenBytes = snprintf(buffer, 256, "%s/v1/e/%s",
apiUrl.c_str(),
endpoint);

REQUIRE(writtenBytes > 0);
Expand Down
5 changes: 2 additions & 3 deletions test/integration_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ class IntegrationTestFixture
void LoadEnvironment();

std::string target;

std::string targetDomain;
std::string targetPrefix;
std::string apiUrl;
std::string websocketUrl;

std::string authenticationHeader;
std::unique_ptr<WebsocketConnection> connection;
Expand Down
19 changes: 12 additions & 7 deletions websocket_network/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ size_t writeResponse(char* ptr, size_t size, size_t nmemb, void* data)

char* start = impl->messageFragment.data() + oldSize;
memcpy(start, ptr, size * nmemb);

curl_ws_frame* frame = curl_ws_meta(impl->connection);
if (frame->bytesleft > 0)
{
Expand Down Expand Up @@ -94,6 +94,11 @@ WebsocketConnection::WebsocketConnection(const std::string& url, uint16_t port)
CURL* curl = curl_easy_init();

std::string protocolURL = "wss://" + url;
if (url.rfind("localhost", 0) == 0)
{
protocolURL = "ws://" + url;
}

curl_easy_setopt(curl, CURLOPT_URL, protocolURL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, impl);
Expand Down Expand Up @@ -185,11 +190,11 @@ int WebsocketConnection::run()

size_t sent = 0;
CURLcode result = curl_ws_send(
impl->connection,
msg->data.data(),
msg->data.size(),
&sent,
0,
impl->connection,
msg->data.data(),
msg->data.size(),
&sent,
0,
flags
);

Expand Down Expand Up @@ -218,7 +223,7 @@ void WebsocketConnection::send(const char* bytes, uint32_t length)
std::unique_ptr<Message> msg = std::unique_ptr<Message>(new Message());
msg->binary = false;
msg->data = std::vector<char>(bytes, bytes + length);

std::lock_guard<std::mutex> lock(impl->lock);
impl->messages.push_back(std::move(msg));
}
Expand Down

0 comments on commit 418f078

Please sign in to comment.