125 lines
3.7 KiB
C++
125 lines
3.7 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <chrono>
|
|
#include <fstream>
|
|
|
|
#include "Producer.hpp"
|
|
|
|
TEST(ProducerTest, ProducerCallsBackWhenEnabled)
|
|
{
|
|
// Arrange create the file and write "1\n" into it.
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "1\n";
|
|
}
|
|
// create a fake callback function
|
|
std::vector<int> outputs;
|
|
std::vector<std::string> logs;
|
|
|
|
// construct a producer with fake file and callback
|
|
Producer producer{"fake_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; },
|
|
[&logs](const std::string& msg) { logs.push_back(msg); }};
|
|
// Act: initialize producer and stop it.
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
producer.stop();
|
|
|
|
// Assert: we expect one output being 42
|
|
EXPECT_EQ(outputs[0], 42);
|
|
EXPECT_NE(logs[0].find("Enabled"), std::string::npos);
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenUnexpectedValue)
|
|
{
|
|
// Arrange create the file and write "0\n" into it.
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "0\n";
|
|
}
|
|
|
|
std::vector<int> outputs;
|
|
std::vector<std::string> logs;
|
|
|
|
Producer producer{"fake_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; },
|
|
[&logs](const std::string& msg) { logs.push_back(msg); },
|
|
[](std::chrono::milliseconds) {}};
|
|
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // ← 1ms window
|
|
producer.stop();
|
|
|
|
// Assert: we expect no output
|
|
EXPECT_TRUE(outputs.empty());
|
|
EXPECT_NE(logs[0].find("UnexpectedValue"), std::string::npos);
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenEmpty)
|
|
{
|
|
// Arrange create the file and write "0\n" into it.
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << " ";
|
|
}
|
|
|
|
std::vector<int> outputs;
|
|
std::vector<std::string> logs;
|
|
|
|
Producer producer{"fake_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; },
|
|
[&logs](const std::string& msg) { logs.push_back(msg); },
|
|
[](std::chrono::milliseconds) {}};
|
|
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // ← 1ms window
|
|
producer.stop();
|
|
|
|
// Assert: we expect no output
|
|
EXPECT_TRUE(outputs.empty());
|
|
EXPECT_NE(logs[0].find("Empty"), std::string::npos);
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenUnreachable)
|
|
{
|
|
std::vector<int> outputs;
|
|
std::vector<std::string> logs;
|
|
|
|
Producer producer{"nonexistant_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; },
|
|
[&logs](const std::string& msg) { logs.push_back(msg); },
|
|
[](std::chrono::milliseconds) {}};
|
|
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // ← 1ms window
|
|
producer.stop();
|
|
|
|
// Assert: we expect no output
|
|
EXPECT_TRUE(outputs.empty());
|
|
EXPECT_NE(logs[0].find("Unreachable"), std::string::npos);
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenTempTooHigh)
|
|
{
|
|
// create a file that contains error
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "error: temp too high";
|
|
}
|
|
std::vector<int> outputs;
|
|
std::vector<std::string> logs;
|
|
|
|
Producer producer{"fake_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; },
|
|
[&logs](const std::string& msg) { logs.push_back(msg); },
|
|
[](std::chrono::milliseconds) {}};
|
|
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // ← 1ms window
|
|
producer.stop();
|
|
|
|
// Assert: we expect no output
|
|
EXPECT_TRUE(outputs.empty());
|
|
EXPECT_NE(logs[0].find("Error"), std::string::npos);
|
|
}
|