|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <test_utils/ChannelType.h> |
| 4 | +#include <test_utils/TestUtils.h> |
| 5 | + |
| 6 | +#include "FSChannel.h" |
| 7 | + |
| 8 | +#include <fstream> |
| 9 | + |
| 10 | +#include "modules/SharedResource/SharedResource.h" |
| 11 | + |
| 12 | +using namespace quisp_test; |
| 13 | +using namespace quisp_test::utils; |
| 14 | +using namespace quisp::messages; |
| 15 | +using namespace quisp::modules::SharedResource; |
| 16 | +using namespace omnetpp; |
| 17 | +using namespace quisp_test::utils; |
| 18 | +using OriginalFSChannel = channels::FSChannel; |
| 19 | +using quisp_test::channel_type::TestChannelType; |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +class MockNode : public quisp_test::TestQNode { |
| 24 | + public: |
| 25 | + MockNode(int addr, int mass, bool is_initiator, bool i_am_qnode) : TestQNode(addr, mass, is_initiator), is_qnode(i_am_qnode) {} |
| 26 | + MockNode(int addr, int mass, bool is_initiator) : TestQNode(addr, mass, is_initiator), is_qnode(true) {} |
| 27 | + bool is_qnode; |
| 28 | +}; |
| 29 | + |
| 30 | +class FSChannel : public OriginalFSChannel { |
| 31 | + public: |
| 32 | + FSChannel() : OriginalFSChannel::FSChannel() { |
| 33 | + setParDouble(this, "distance", 0); |
| 34 | + setParDouble(this, "delay", 0); |
| 35 | + setParDouble(this, "datarate", 0); |
| 36 | + setParDouble(this, "ber", 0); |
| 37 | + setParDouble(this, "per", 0); |
| 38 | + setParDouble(this, "orbital_period", 86400); |
| 39 | + setParDouble(this, "speed_of_light_in_FS", 299792458); |
| 40 | + setParBool(this, "disabled", false); |
| 41 | + setParBool(this, "CSV_varies_delay", true); |
| 42 | + |
| 43 | + setComponentType(new TestChannelType("test channel")); |
| 44 | + } |
| 45 | + cChannel::Result public_processMessage(cMessage* msg) { return processMessage(msg, SendOptions(), simTime()); }; |
| 46 | + void addResultRecorders() override{}; |
| 47 | +}; |
| 48 | + |
| 49 | +class TestSimpleModule : public cSimpleModule { |
| 50 | + public: |
| 51 | + virtual void scheduleAfter(simtime_t delay, cMessage* msg) override { |
| 52 | + take(msg); |
| 53 | + cSimpleModule::scheduleAfter(delay, msg); |
| 54 | + } |
| 55 | + virtual void addResultRecorders() override{}; |
| 56 | + |
| 57 | + virtual void handleMessage(cMessage* msg) override{}; |
| 58 | + MockNode* parent; |
| 59 | + cModule* getParentModule() const override { return parent; }; |
| 60 | + |
| 61 | + explicit TestSimpleModule(MockNode* parent_qnode) : cSimpleModule() { |
| 62 | + this->setComponentType(new TestModuleType("test_simple_module")); |
| 63 | + parent = parent_qnode; |
| 64 | + auto* sim = getTestSimulation(); |
| 65 | + sim->registerComponent(this); |
| 66 | + port$o = new TestGate(this, "port$o"); |
| 67 | + port$i = new TestGate(this, "port$i"); |
| 68 | + this->addGate("port", cGate::INOUT); |
| 69 | + } |
| 70 | + |
| 71 | + TestGate* port$i; |
| 72 | + TestGate* port$o; |
| 73 | + |
| 74 | + std::map<const char*, cGate*> ports{}; |
| 75 | + TestGate* gate(const char* gatename, int index = -1) override { |
| 76 | + if (strcmp(gatename, "port$i") == 0) return port$i; |
| 77 | + if (strcmp(gatename, "port$o") == 0) return port$o; |
| 78 | + error("port: %s not found", gatename); |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +class FSChannelTest : public ::testing::Test { |
| 84 | + protected: |
| 85 | + void SetUp() { |
| 86 | + generateTestCSV("channels/test_dist_csv.csv"); |
| 87 | + |
| 88 | + sim = prepareSimulation(); |
| 89 | + |
| 90 | + sat_node = new MockNode(1, 0, false); |
| 91 | + ground_node = new MockNode(2, 0, false); |
| 92 | + |
| 93 | + sat_simplemodule = new TestSimpleModule(sat_node); |
| 94 | + |
| 95 | + sat_gate = sat_node->addGate("sat_gate", cGate::INOUT); |
| 96 | + ground_gate = ground_node->addGate("ground_gate", cGate::INOUT); |
| 97 | + |
| 98 | + downlink_chl = new FSChannel(); |
| 99 | + |
| 100 | + sim->registerComponent(downlink_chl); |
| 101 | + |
| 102 | + utils::setParStr(downlink_chl, "distance_CSV", "channels/test_dist_csv.csv"); |
| 103 | + |
| 104 | + sat_node->gate("sat_gate$o")->connectTo(ground_node->gate("ground_gate$i"), downlink_chl, true); |
| 105 | + |
| 106 | + sat_simplemodule->callInitialize(); |
| 107 | + |
| 108 | + downlink_chl->finalizeParameters(); // THIS METHOD MAY ONLY BE CALLED WHEN THE CHANNEL IS CONNECTED |
| 109 | + downlink_chl->callInitialize(); |
| 110 | + } |
| 111 | + void TearDown() { std::remove("channels/test_dist_csv.csv"); } |
| 112 | + |
| 113 | + /** |
| 114 | + * This function mimics the behavior of Omnet++ internals |
| 115 | + * that sets up the message arrival to PointingSystem module. |
| 116 | + * Call this function before PointingSystem->handleMessages |
| 117 | + * when you want to retrieve the info of the arrival gate. |
| 118 | + */ |
| 119 | + |
| 120 | + void generateTestCSV(const char* name) { |
| 121 | + std::ofstream csv_to_generate; |
| 122 | + csv_to_generate.open(name); |
| 123 | + csv_to_generate << "200,100000\n"; |
| 124 | + csv_to_generate << "300,300000\n"; |
| 125 | + csv_to_generate << "400,200000\n"; |
| 126 | + csv_to_generate.close(); |
| 127 | + } |
| 128 | + |
| 129 | + TestSimulation* sim; |
| 130 | + TestSimpleModule* sat_simplemodule; |
| 131 | + MockNode* sat_node; |
| 132 | + MockNode* ground_node; |
| 133 | + FSChannel* downlink_chl; |
| 134 | + cGate* sat_gate; |
| 135 | + cGate* ground_gate; |
| 136 | +}; |
| 137 | + |
| 138 | +TEST_F(FSChannelTest, messageWhenNonVisible) { |
| 139 | + cMessage* msg = new cMessage(); |
| 140 | + cChannel::Result res = downlink_chl->public_processMessage(msg); |
| 141 | + sim->run(); |
| 142 | + ASSERT_EQ(res.discard, true); |
| 143 | +} |
| 144 | + |
| 145 | +TEST_F(FSChannelTest, messageWhenVisible) { |
| 146 | + auto timeout = new cMessage; |
| 147 | + sat_simplemodule->scheduleAfter(200, timeout); |
| 148 | + sim->executeNextEvent(); |
| 149 | + auto msg = new cMessage; |
| 150 | + cChannel::Result res = downlink_chl->public_processMessage(msg); |
| 151 | + sim->run(); |
| 152 | + ASSERT_EQ(res.discard, false); |
| 153 | + simtime_t delay = downlink_chl->par("distance").doubleValue() / downlink_chl->par("speed_of_light_in_FS").doubleValue(); |
| 154 | + ASSERT_EQ(res.delay.raw(), delay.raw()); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F(FSChannelTest, messageAfterVisible) { |
| 158 | + auto timeout = new cMessage; |
| 159 | + sat_simplemodule->scheduleAfter(600, timeout); |
| 160 | + sim->executeNextEvent(); |
| 161 | + auto msg = new cMessage; |
| 162 | + cChannel::Result res = downlink_chl->public_processMessage(msg); |
| 163 | + sim->run(); |
| 164 | + ASSERT_EQ(res.discard, true); |
| 165 | +} |
| 166 | + |
| 167 | +TEST_F(FSChannelTest, messageFollowingDayVisible) { |
| 168 | + auto timeout = new cMessage; |
| 169 | + sat_simplemodule->scheduleAfter(86700, timeout); |
| 170 | + sim->executeNextEvent(); |
| 171 | + auto msg = new cMessage; |
| 172 | + cChannel::Result res = downlink_chl->public_processMessage(msg); |
| 173 | + sim->run(); |
| 174 | + ASSERT_EQ(res.discard, false); |
| 175 | + simtime_t delay = downlink_chl->par("distance").doubleValue() / downlink_chl->par("speed_of_light_in_FS").doubleValue(); |
| 176 | + ASSERT_EQ(res.delay.raw(), delay.raw()); // Comparing the raw int64_t values doesn't lose precision, unlike .dbl(). |
| 177 | +} |
| 178 | +TEST_F(FSChannelTest, messageFollowingDayNonVisible) { |
| 179 | + auto timeout = new cMessage; |
| 180 | + sat_simplemodule->scheduleAfter(86801, timeout); |
| 181 | + sim->executeNextEvent(); |
| 182 | + auto msg = new cMessage; |
| 183 | + cChannel::Result res = downlink_chl->public_processMessage(msg); |
| 184 | + sim->run(); |
| 185 | + ASSERT_EQ(res.discard, true); |
| 186 | +} |
| 187 | +} // namespace |
0 commit comments