Skip to content

Commit 054ddcd

Browse files
author
Paolo Fittipaldi
committed
Finished tests for FreeSpace Channels
1 parent 0cdf459 commit 054ddcd

File tree

6 files changed

+204
-22
lines changed

6 files changed

+204
-22
lines changed

quisp/channels/FSChannel_test.cc

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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

quisp/test_utils/ChannelType.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ class TestChannelType : public cChannelType {
2121
props = new cProperties;
2222
}
2323

24-
cChannel* createChannelObject() override { return nullptr; };
24+
cChannel *createChannelObject() override { return nullptr; };
2525
void addParametersTo(cChannel *channel) override{};
26-
void applyPatternAssignments(cComponent *component) override{};
26+
void applyPatternAssignments(cComponent *component) override{};
2727
// void setupGateVectors(cModule *module) override{};
2828
// void buildInside(cModule *module) override{};
2929

3030
// methods redefined from cComponentType
31-
cProperties *getProperties() const override { return props; };
32-
cProperties *getParamProperties(const char *paramName) const override { return nullptr; };
33-
cProperties *getGateProperties(const char *gateName) const override { return nullptr; };
34-
cProperties *getSubmoduleProperties(const char *submoduleName, const char *submoduleType) const override { return nullptr; };
35-
cProperties *getConnectionProperties(int connectionId, const char *channelType) const override { return nullptr; };
36-
std::string getPackageProperty(const char *name) const override { return std::string("mock package property"); };
37-
const char *getImplementationClassName() const override { return "TestModuleType"; };
38-
std::string getCxxNamespace() const override { return std::string("mock namespace"); };
39-
#if OMNETPP_VERSION >= 0x600 && OMNETPP_BUILDNUM > 1531
40-
std::string getCxxNamespaceForType(const char *type) const override { return "mock cxx namespace"; };
41-
#endif
31+
cProperties *getProperties() const override { return props; };
32+
cProperties *getParamProperties(const char *paramName) const override { return nullptr; };
33+
cProperties *getGateProperties(const char *gateName) const override { return nullptr; };
34+
cProperties *getSubmoduleProperties(const char *submoduleName, const char *submoduleType) const override { return nullptr; };
35+
cProperties *getConnectionProperties(int connectionId, const char *channelType) const override { return nullptr; };
36+
std::string getPackageProperty(const char *name) const override { return std::string("mock package property"); };
37+
const char *getImplementationClassName() const override { return "TestModuleType"; };
38+
std::string getCxxNamespace() const override { return std::string("mock namespace"); };
39+
#if OMNETPP_VERSION >= 0x600 && OMNETPP_BUILDNUM > 1531
40+
std::string getCxxNamespaceForType(const char *type) const override { return "mock cxx namespace"; };
41+
#endif
4242

4343
const char *getSourceFileName() const override { return "no source"; };
4444
bool isInnerType() const override { return false; };

quisp/test_utils/Gate.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ using omnetpp::cSimulation;
1010
TempGate::TempGate() {}
1111
bool TempGate::deliver(cMessage *msg, const omnetpp::SendOptions &options, simtime_t at) { return true; }
1212

13-
1413
TestGate::TestGate(cModule *mod, const char *name) {
1514
desc = new omnetpp::cGate::Desc;
1615
// only for output gate
@@ -30,11 +29,7 @@ bool TestGate::deliver(cMessage *msg, const omnetpp::SendOptions &options, simti
3029
return true;
3130
}
3231

33-
void TestGate::quietConnectTo(cGate* target) {
34-
nextGate = target;
35-
}
36-
37-
32+
void TestGate::quietConnectTo(cGate *target) { nextGate = target; }
3833

3934
} // namespace gate
4035
} // namespace quisp_test

quisp/test_utils/Gate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TestGate : public omnetpp::cGate {
3131
public:
3232
explicit TestGate(cModule *mod, const char *name);
3333
std::vector<cMessage *> messages;
34-
void quietConnectTo(cGate* target); //To connect to a gate skipping all the listeners checks, which create problems if a full-fledged sim environment is not up.
34+
void quietConnectTo(cGate *target); // To connect to a gate skipping all the listeners checks, which create problems if a full-fledged sim environment is not up.
3535

3636
protected:
3737
TempGate temp_gate;

quisp/test_utils/TestUtilFunctions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
namespace quisp_test {
88
namespace utils {
99

10-
using omnetpp::cModule;
1110
using omnetpp::cChannel;
11+
using omnetpp::cModule;
1212
using quisp_test::simulation::TestSimulation;
1313

1414
void setParInt(cModule *module, const char *name, const int val);

quisp/test_utils/TestUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#pragma once
22

33
#include "AccessPrivate.h"
4+
#include "ChannelType.h"
45
#include "Gate.h"
56
#include "ModuleType.h"
67
#include "QNode.h"
7-
#include "ChannelType.h"
88
#include "Simulation.h"
99
#include "StaticEnv.h"
1010
#include "TestComponentProviderStrategy.h"

0 commit comments

Comments
 (0)