Skip to content

Commit

Permalink
test: implemented test case for setting nice value
Browse files Browse the repository at this point in the history
Signed-off-by: Janosch Machowinski <[email protected]>
  • Loading branch information
Janosch Machowinski authored and Janosch Machowinski committed Sep 14, 2023
1 parent 5227edf commit 35c8cdb
Showing 1 changed file with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@

#include "mock_compression_factory.hpp"

#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/resource.h>
#endif

using namespace testing; // NOLINT

static constexpr const char * DefaultTestCompressor = "fake_comp";
Expand Down Expand Up @@ -394,6 +401,100 @@ TEST_P(SequentialCompressionWriterTest, writer_writes_with_compression_queue_siz
EXPECT_EQ(fake_storage_size_, kNumMessagesToWrite);
}


TEST_P(SequentialCompressionWriterTest, writer_sets_nice_value)
{
const std::string test_topic_name = "test_topic";
const std::string test_topic_type = "test_msgs/BasicTypes";
const uint64_t kCompressionQueueSize = GetParam();
const int wanted_nice_value = 10;

class TestCompressor : public rosbag2_compression::BaseCompressorInterface
{
int & detected_nice_value;

public:
TestCompressor(int & detected_nice_value)
: detected_nice_value(detected_nice_value) {}
virtual std::string compress_uri(const std::string & uri)
{
return uri;
}

virtual void compress_serialized_bag_message(
const rosbag2_storage::SerializedBagMessage * bag_message,
rosbag2_storage::SerializedBagMessage * compressed_message)
{
#ifdef _WIN32
int cur_nice_value = getpriority(PRIO_PROCESS, 0);
if (cur_nice_value != -1 && errno == 0) {

detected_nice_value = cur_nice_value;
}
#else
//FIXME implement windows version
#endif

*compressed_message = *bag_message;
}

/**
* Get the identifier of the compression algorithm.
* This is appended to the extension of the compressed file.
*/
virtual std::string get_compression_identifier() const
{
return "niceTest";
}
};

class FakeFactory : public rosbag2_compression::CompressionFactory
{
int & detected_nice_value;

public:
FakeFactory(int & detected_nice_value)
: detected_nice_value(detected_nice_value) {}

virtual std::shared_ptr<rosbag2_compression::BaseCompressorInterface>
create_compressor(const std::string & /*compression_format*/)
{
return std::make_shared<TestCompressor>(detected_nice_value);
}
};

// queue size should be 0 or at least the number of remaining messages to prevent message loss
rosbag2_compression::CompressionOptions compression_options {
DefaultTestCompressor,
rosbag2_compression::CompressionMode::MESSAGE,
kCompressionQueueSize,
kDefaultCompressionQueueThreads,
wanted_nice_value
};

// nice values are in the range from -20 to +19, so this value will never be read
int detected_nice_value = 100;

initializeFakeFileStorage();
initializeWriter(compression_options, std::make_unique<FakeFactory>(detected_nice_value));

writer_->open(tmp_dir_storage_options_);
writer_->create_topic({test_topic_name, test_topic_type, "", "", ""});

auto message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
message->topic_name = test_topic_name;

const size_t kNumMessagesToWrite = 5;
for (size_t i = 0; i < kNumMessagesToWrite; i++) {
writer_->write(message);
}
writer_.reset(); // reset will call writer destructor

EXPECT_EQ(detected_nice_value, *compression_options.thread_nice_value);

EXPECT_EQ(fake_storage_size_, kNumMessagesToWrite);
}

INSTANTIATE_TEST_SUITE_P(
SequentialCompressionWriterTestQueueSizes,
SequentialCompressionWriterTest,
Expand Down

0 comments on commit 35c8cdb

Please sign in to comment.