111 lines
2.9 KiB
C++
111 lines
2.9 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;
|
|
|
|
// construct a producer with fake file and callback
|
|
Producer producer{"fake_sysfs_input", [&outputs](int value)
|
|
{ outputs.push_back(value); }, []() { return 42; }};
|
|
// Act: initialize producer and stop it.
|
|
producer.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // ← 1ms window
|
|
producer.stop();
|
|
|
|
// Assert: we expect one output being 42
|
|
ASSERT_EQ(outputs.size(), 1u);
|
|
EXPECT_EQ(outputs[0], 42);
|
|
}
|
|
|
|
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;
|
|
|
|
Producer producer{"fake_sysfs_input",
|
|
[&outputs](int value) { outputs.push_back(value); },
|
|
[]() { return 42; }, [](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());
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenEmpty)
|
|
{
|
|
// Arrange create the file and write "0\n" into it.
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << " ";
|
|
}
|
|
|
|
std::vector<int> outputs;
|
|
|
|
Producer producer{"fake_sysfs_input",
|
|
[&outputs](int value) { outputs.push_back(value); },
|
|
[]() { return 42; }, [](std::chrono::milliseconds) {}};
|
|
|
|
producer.start();
|
|
producer.stop();
|
|
|
|
// Assert: we expect no output
|
|
EXPECT_TRUE(outputs.empty());
|
|
}
|
|
|
|
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; }, [](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());
|
|
}
|
|
|
|
TEST(ProducerTest, ProducerDoesNotCallWhenTempTooHigh)
|
|
{
|
|
// create a file that contains error
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "error: temp too high";
|
|
}
|
|
std::vector<int> outputs;
|
|
|
|
Producer producer{"fake_sysfs_input",
|
|
[&outputs](int value) { outputs.push_back(value); },
|
|
[]() { return 42; }, [](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());
|
|
}
|