Skip to content

Commit

Permalink
Set appid on messages (#137)
Browse files Browse the repository at this point in the history
* Set app id

* Add tests for app id
  • Loading branch information
Shillaker authored Sep 8, 2021
1 parent ca9a5c3 commit 0ff6027
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/util/func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ unsigned int setMessageId(faabric::Message& msg)
msg.set_id(messageId);
}

// Set an app ID if not already set
if (msg.appid() == 0) {
msg.set_appid(faabric::util::generateGid());
}

// Set the timestamp if it doesn't have one
if (msg.timestamp() <= 0) {
Clock& clock = faabric::util::getGlobalClock();
Expand Down
12 changes: 10 additions & 2 deletions tests/test/util/test_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,30 @@ TEST_CASE("Test message factory shared", "[util]")
REQUIRE(!msg->resultkey().empty());
}

TEST_CASE("Test adding id to message", "[util]")
TEST_CASE("Test adding ids to message", "[util]")
{
faabric::Message msgA;
faabric::Message msgB;

REQUIRE(msgA.id() == 0);
REQUIRE(msgA.resultkey().empty());
REQUIRE(msgA.statuskey().empty());
REQUIRE(msgA.appid() == 0);

REQUIRE(msgB.id() == 0);
REQUIRE(msgB.resultkey().empty());
REQUIRE(msgB.statuskey().empty());
REQUIRE(msgB.appid() == 0);

faabric::util::setMessageId(msgA);
faabric::util::setMessageId(msgB);

REQUIRE(msgA.id() > 0);
REQUIRE(msgA.appid() > 0);
REQUIRE(msgB.id() > 0);
REQUIRE(msgB.appid() > 0);
REQUIRE(msgB.id() > msgA.id());
REQUIRE(msgB.appid() > msgA.appid());

std::string expectedResultKeyA =
std::string("result_" + std::to_string(msgA.id()));
Expand All @@ -65,21 +70,24 @@ TEST_CASE("Test adding id to message", "[util]")
REQUIRE(msgB.statuskey() == expectedStatusKeyB);
}

TEST_CASE("Test adding ID to message with an existing ID")
TEST_CASE("Test adding ids to message with existing ids")
{
faabric::Message msg;
faabric::util::setMessageId(msg);

int originalId = msg.id();
int originalAppId = msg.appid();
std::string originalStatusKey = msg.statuskey();
std::string originalResultKey = msg.resultkey();

faabric::util::setMessageId(msg);
int afterId = msg.id();
int afterAppId = msg.appid();
std::string afterStatusKey = msg.statuskey();
std::string afterResultKey = msg.resultkey();

REQUIRE(afterId == originalId);
REQUIRE(afterAppId == originalAppId);
REQUIRE(afterStatusKey == originalStatusKey);
REQUIRE(afterResultKey == originalResultKey);
}
Expand Down

0 comments on commit 0ff6027

Please sign in to comment.