##// END OF EJS Templates
Merge branch 'feature/FuzzingTests2' into develop
Alexandre Leroux -
r1221:5e5e6b9907f3 merge
parent child
Show More
@@ -0,0 +1,38
1 #ifndef SCIQLOP_SIGNALWAITER_H
2 #define SCIQLOP_SIGNALWAITER_H
3
4 #include "CoreGlobal.h"
5
6 #include <QEventLoop>
7
8 /**
9 * Class for synchronously waiting for the reception of a signal. The signal to wait is passed to
10 * the construction of the object. When starting the wait, a timeout can be set to exit if the
11 * signal has not been sent
12 */
13 class SCIQLOP_CORE_EXPORT SignalWaiter : public QObject {
14 Q_OBJECT
15 public:
16 /**
17 * Ctor
18 * @param object the sender of the signal
19 * @param signal the signal to listen
20 */
21 explicit SignalWaiter(QObject &sender, const char *signal);
22
23 /**
24 * Starts the signal and leaves after the signal has been received, or after the timeout
25 * @param timeout the timeout set (if 0, uses a default timeout)
26 * @return true if the signal was sent, false if the timeout occured
27 */
28 bool wait(int timeout);
29
30 private:
31 bool m_Timeout;
32 QEventLoop m_EventLoop;
33
34 private slots:
35 void timeout();
36 };
37
38 #endif // SCIQLOP_SIGNALWAITER_H
@@ -0,0 +1,36
1 #include "Common/SignalWaiter.h"
2
3 #include <QTimer>
4
5 namespace {
6
7 const auto DEFAULT_TIMEOUT = 30000;
8
9 } // namespace
10
11 SignalWaiter::SignalWaiter(QObject &sender, const char *signal) : m_Timeout{false}
12 {
13 connect(&sender, signal, &m_EventLoop, SLOT(quit()));
14 }
15
16 bool SignalWaiter::wait(int timeout)
17 {
18 if (timeout == 0) {
19 timeout = DEFAULT_TIMEOUT;
20 }
21
22 QTimer timer{};
23 timer.setInterval(timeout);
24 timer.start();
25 connect(&timer, &QTimer::timeout, this, &SignalWaiter::timeout);
26
27 m_EventLoop.exec();
28
29 return !m_Timeout;
30 }
31
32 void SignalWaiter::timeout()
33 {
34 m_Timeout = true;
35 m_EventLoop.quit();
36 }
@@ -0,0 +1,228
1 #include "FuzzingValidators.h"
2 #include "FuzzingDefs.h"
3
4 #include <Data/DataSeries.h>
5 #include <Variable/Variable.h>
6
7 #include <QTest>
8
9 #include <functional>
10
11 Q_LOGGING_CATEGORY(LOG_FuzzingValidators, "FuzzingValidators")
12
13 namespace {
14
15 // ////////////// //
16 // DATA VALIDATOR //
17 // ////////////// //
18
19 /// Singleton used to validate data of a variable
20 class DataValidatorHelper {
21 public:
22 /// @return the single instance of the helper
23 static DataValidatorHelper &instance();
24 virtual ~DataValidatorHelper() noexcept = default;
25
26 virtual void validate(const VariableState &variableState) const = 0;
27 };
28
29 /**
30 * Default implementation of @sa DataValidatorHelper
31 */
32 class DefaultDataValidatorHelper : public DataValidatorHelper {
33 public:
34 void validate(const VariableState &variableState) const override
35 {
36 Q_UNUSED(variableState);
37 qCWarning(LOG_FuzzingValidators()).noquote() << "Checking variable's data... WARN: no data "
38 "verification is available for this server";
39 }
40 };
41
42 /// Data resolution in local server's files
43 const auto LOCALHOST_SERVER_RESOLUTION = 4;
44 /// Reference value used to generate the data on the local server (a value is the number of seconds
45 /// between the data date and this reference date)
46 const auto LOCALHOST_REFERENCE_VALUE
47 = DateUtils::secondsSinceEpoch(QDateTime{QDate{2000, 1, 1}, QTime{}, Qt::UTC});
48
49 /**
50 * Implementation of @sa DataValidatorHelper for the local AMDA server
51 */
52 class LocalhostServerDataValidatorHelper : public DataValidatorHelper {
53 public:
54 void validate(const VariableState &variableState) const override
55 {
56 // Don't check data for null variable
57 if (!variableState.m_Variable || variableState.m_Range == INVALID_RANGE) {
58 return;
59 }
60
61 auto message = "Checking variable's data...";
62 auto toDateString = [](double value) { return DateUtils::dateTime(value).toString(); };
63
64 // Checks that data are defined
65 auto variableDataSeries = variableState.m_Variable->dataSeries();
66 if (variableDataSeries == nullptr && variableState.m_Range != INVALID_RANGE) {
67 qCInfo(LOG_FuzzingValidators()).noquote()
68 << message << "FAIL: the variable has no data while a range is defined";
69 QFAIL("");
70 }
71
72 auto dataIts = variableDataSeries->xAxisRange(variableState.m_Range.m_TStart,
73 variableState.m_Range.m_TEnd);
74
75 // Checks that the data are well defined in the range:
76 // - there is at least one data
77 // - the data are consistent (no data holes)
78 if (std::distance(dataIts.first, dataIts.second) == 0) {
79 qCInfo(LOG_FuzzingValidators()).noquote()
80 << message << "FAIL: the variable has no data";
81 QFAIL("");
82 }
83
84 auto firstXAxisData = dataIts.first->x();
85 auto lastXAxisData = (dataIts.second - 1)->x();
86
87 if (std::abs(firstXAxisData - variableState.m_Range.m_TStart) > LOCALHOST_SERVER_RESOLUTION
88 || std::abs(lastXAxisData - variableState.m_Range.m_TEnd)
89 > LOCALHOST_SERVER_RESOLUTION) {
90 qCInfo(LOG_FuzzingValidators()).noquote()
91 << message << "FAIL: the data in the defined range are inconsistent (data hole "
92 "found at the beginning or the end)";
93 QFAIL("");
94 }
95
96 auto dataHoleIt = std::adjacent_find(
97 dataIts.first, dataIts.second, [](const auto &it1, const auto &it2) {
98 /// @todo: validate resolution
99 return std::abs(it1.x() - it2.x()) > 2 * (LOCALHOST_SERVER_RESOLUTION - 1);
100 });
101
102 if (dataHoleIt != dataIts.second) {
103 qCInfo(LOG_FuzzingValidators()).noquote()
104 << message << "FAIL: the data in the defined range are inconsistent (data hole "
105 "found between times "
106 << toDateString(dataHoleIt->x()) << "and " << toDateString((dataHoleIt + 1)->x())
107 << ")";
108 QFAIL("");
109 }
110
111 // Checks values
112 auto dataIndex = 0;
113 for (auto dataIt = dataIts.first; dataIt != dataIts.second; ++dataIt, ++dataIndex) {
114 auto xAxisData = dataIt->x();
115 auto valuesData = dataIt->values();
116 for (auto valueIndex = 0, valueEnd = valuesData.size(); valueIndex < valueEnd;
117 ++valueIndex) {
118 auto value = valuesData.at(valueIndex);
119 auto expectedValue = xAxisData + valueIndex * LOCALHOST_SERVER_RESOLUTION
120 - LOCALHOST_REFERENCE_VALUE;
121
122 if (value != expectedValue) {
123 qCInfo(LOG_FuzzingValidators()).noquote()
124 << message << "FAIL: incorrect value data at time"
125 << toDateString(xAxisData) << ", index" << valueIndex << "(found:" << value
126 << ", expected:" << expectedValue << ")";
127 QFAIL("");
128 }
129 }
130 }
131
132 // At this step validation is OK
133 qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK";
134 }
135 };
136
137 /// Creates the @sa DataValidatorHelper according to the server passed in parameter
138 std::unique_ptr<DataValidatorHelper> createDataValidatorInstance(const QString &server)
139 {
140 if (server == QString{"localhost"}) {
141 return std::make_unique<LocalhostServerDataValidatorHelper>();
142 }
143 else {
144 return std::make_unique<DefaultDataValidatorHelper>();
145 }
146 }
147
148 DataValidatorHelper &DataValidatorHelper::instance()
149 {
150 // Creates instance depending on the SCIQLOP_AMDA_SERVER value at compile time
151 static auto instance = createDataValidatorInstance(SCIQLOP_AMDA_SERVER);
152 return *instance;
153 }
154
155 // /////////////// //
156 // RANGE VALIDATOR //
157 // /////////////// //
158
159 /**
160 * Checks that a range of a variable matches the expected range passed as a parameter
161 * @param variable the variable for which to check the range
162 * @param expectedRange the expected range
163 * @param getVariableRangeFun the function to retrieve the range from the variable
164 * @remarks if the variable is null, checks that the expected range is the invalid range
165 */
166 void validateRange(std::shared_ptr<Variable> variable, const SqpRange &expectedRange,
167 std::function<SqpRange(const Variable &)> getVariableRangeFun)
168 {
169 auto compare = [](const auto &range, const auto &expectedRange, const auto &message) {
170 if (range == expectedRange) {
171 qCInfo(LOG_FuzzingValidators()).noquote() << message << "OK";
172 }
173 else {
174 qCInfo(LOG_FuzzingValidators()).noquote()
175 << message << "FAIL (current range:" << range
176 << ", expected range:" << expectedRange << ")";
177 QFAIL("");
178 }
179 };
180
181 if (variable) {
182 compare(getVariableRangeFun(*variable), expectedRange, "Checking variable's range...");
183 }
184 else {
185 compare(INVALID_RANGE, expectedRange, "Checking that there is no range set...");
186 }
187 }
188
189 /**
190 * Default implementation of @sa IFuzzingValidator. This validator takes as parameter of its
191 * construction a function of validation which is called in the validate() method
192 */
193 class FuzzingValidator : public IFuzzingValidator {
194 public:
195 /// Signature of a validation function
196 using ValidationFunction = std::function<void(const VariableState &variableState)>;
197
198 explicit FuzzingValidator(ValidationFunction fun) : m_Fun(std::move(fun)) {}
199
200 void validate(const VariableState &variableState) const override { m_Fun(variableState); }
201
202 private:
203 ValidationFunction m_Fun;
204 };
205
206 } // namespace
207
208 std::unique_ptr<IFuzzingValidator> FuzzingValidatorFactory::create(FuzzingValidatorType type)
209 {
210 switch (type) {
211 case FuzzingValidatorType::DATA:
212 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
213 DataValidatorHelper::instance().validate(variableState);
214 });
215 case FuzzingValidatorType::RANGE:
216 return std::make_unique<FuzzingValidator>([](const VariableState &variableState) {
217 auto getVariableRange = [](const Variable &variable) { return variable.range(); };
218 validateRange(variableState.m_Variable, variableState.m_Range, getVariableRange);
219 });
220 default:
221 // Default case returns invalid validator
222 break;
223 }
224
225 // Invalid validator
226 return std::make_unique<FuzzingValidator>(
227 [](const VariableState &) { QFAIL("Invalid validator"); });
228 }
@@ -0,0 +1,40
1 #ifndef SCIQLOP_FUZZINGVALIDATORS_H
2 #define SCIQLOP_FUZZINGVALIDATORS_H
3
4 #include <memory>
5 #include <set>
6
7 #include <QLoggingCategory>
8 #include <QMetaType>
9
10 Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingValidators)
11
12 class VariableState;
13
14 /// Types of validators that can be defined
15 enum class FuzzingValidatorType {
16 DATA, ///< Validates variable's data
17 RANGE ///< Validates variable's range
18 };
19
20 /**
21 * Struct that represents a validator. A validator checks if the state of a variable is valid at the
22 * moment it is called during a fuzzing test
23 */
24 struct IFuzzingValidator {
25 virtual ~IFuzzingValidator() noexcept = default;
26
27 /// Validates the variable's state passed in parameter
28 virtual void validate(const VariableState &variableState) const = 0;
29 };
30
31 /// Factory of @sa IFuzzingValidator
32 struct FuzzingValidatorFactory {
33 /// Creates a validator according to the type passed in parameter
34 static std::unique_ptr<IFuzzingValidator> create(FuzzingValidatorType type);
35 };
36
37 using ValidatorsTypes = std::vector<FuzzingValidatorType>;
38 Q_DECLARE_METATYPE(ValidatorsTypes)
39
40 #endif // SCIQLOP_FUZZINGVALIDATORS_H
@@ -18,8 +18,8 struct SqpRange {
18 18 static SqpRange fromDateTime(const QDate &startDate, const QTime &startTime,
19 19 const QDate &endDate, const QTime &endTime)
20 20 {
21 return {DateUtils::secondsSinceEpoch(QDateTime{startDate, startTime}),
22 DateUtils::secondsSinceEpoch(QDateTime{endDate, endTime})};
21 return {DateUtils::secondsSinceEpoch(QDateTime{startDate, startTime, Qt::UTC}),
22 DateUtils::secondsSinceEpoch(QDateTime{endDate, endTime, Qt::UTC})};
23 23 }
24 24
25 25 /// Start time (UTC)
@@ -85,6 +85,10 signals:
85 85 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
86 86 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
87 87
88 /// Signal emitted when all acquisitions related to the variables have been completed (whether
89 /// validated, canceled, or failed)
90 void acquisitionFinished();
91
88 92 public slots:
89 93 /// Request the data loading of the variable whithin range
90 94 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
@@ -5,6 +5,7 catalogueapi_dep = dependency('CatalogueAPI', required : true, fallback:['Catalo
5 5
6 6 core_moc_headers = [
7 7 'include/Catalogue/CatalogueController.h',
8 'include/Common/SignalWaiter.h',
8 9 'include/Data/IDataProvider.h',
9 10 'include/DataSource/DataSourceController.h',
10 11 'include/DataSource/DataSourceItemAction.h',
@@ -24,6 +25,7 core_moc_files = qt5.preprocess(moc_headers : core_moc_headers)
24 25
25 26 core_sources = [
26 27 'src/Common/DateUtils.cpp',
28 'src/Common/SignalWaiter.cpp',
27 29 'src/Common/StringUtils.cpp',
28 30 'src/Common/MimeTypesDef.cpp',
29 31 'src/Catalogue/CatalogueController.cpp',
@@ -890,6 +890,9 void VariableController::VariableControllerPrivate::updateVariables(QUuid varReq
890 890 // cleaning varRequestId
891 891 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
892 892 m_VarGroupIdToVarIds.erase(varRequestId);
893 if (m_VarGroupIdToVarIds.empty()) {
894 emit q->acquisitionFinished();
895 }
893 896 }
894 897 }
895 898
@@ -1019,6 +1022,9 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid
1019 1022 }
1020 1023 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
1021 1024 m_VarGroupIdToVarIds.erase(varRequestId);
1025 if (m_VarGroupIdToVarIds.empty()) {
1026 emit q->acquisitionFinished();
1027 }
1022 1028 }
1023 1029
1024 1030 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
@@ -72,7 +72,9 tests_sources = [
72 72 'tests/FuzzingOperations.h',
73 73 'tests/FuzzingOperations.cpp',
74 74 'tests/FuzzingUtils.h',
75 'tests/FuzzingUtils.cpp'
75 'tests/FuzzingUtils.cpp',
76 'tests/FuzzingValidators.h',
77 'tests/FuzzingValidators.cpp'
76 78 ]
77 79
78 80 foreach unit_test : tests
@@ -1,8 +1,109
1 1 #include "FuzzingDefs.h"
2 2
3 const QString ACQUISITION_TIMEOUT_PROPERTY = QStringLiteral("acquisitionTimeout");
3 4 const QString NB_MAX_OPERATIONS_PROPERTY = QStringLiteral("component");
5 const QString NB_MAX_SYNC_GROUPS_PROPERTY = QStringLiteral("nbSyncGroups");
4 6 const QString NB_MAX_VARIABLES_PROPERTY = QStringLiteral("nbMaxVariables");
5 7 const QString AVAILABLE_OPERATIONS_PROPERTY = QStringLiteral("availableOperations");
8 const QString CACHE_TOLERANCE_PROPERTY = QStringLiteral("cacheTolerance");
9 const QString INITIAL_RANGE_PROPERTY = QStringLiteral("initialRange");
6 10 const QString MAX_RANGE_PROPERTY = QStringLiteral("maxRange");
7 11 const QString METADATA_POOL_PROPERTY = QStringLiteral("metadataPool");
8 12 const QString PROVIDER_PROPERTY = QStringLiteral("provider");
13 const QString OPERATION_DELAY_BOUNDS_PROPERTY = QStringLiteral("operationDelays");
14 const QString VALIDATORS_PROPERTY = QStringLiteral("validators");
15 const QString VALIDATION_FREQUENCY_BOUNDS_PROPERTY = QStringLiteral("validationFrequencyBounds");
16
17 // //////////// //
18 // FuzzingState //
19 // //////////// //
20
21 const SyncGroup &FuzzingState::syncGroup(SyncGroupId id) const
22 {
23 return m_SyncGroupsPool.at(id);
24 }
25
26 SyncGroup &FuzzingState::syncGroup(SyncGroupId id)
27 {
28 return m_SyncGroupsPool.at(id);
29 }
30
31 const VariableState &FuzzingState::variableState(VariableId id) const
32 {
33 return m_VariablesPool.at(id);
34 }
35
36 VariableState &FuzzingState::variableState(VariableId id)
37 {
38 return m_VariablesPool.at(id);
39 }
40
41 SyncGroupId FuzzingState::syncGroupId(VariableId variableId) const
42 {
43 auto end = m_SyncGroupsPool.cend();
44 auto it
45 = std::find_if(m_SyncGroupsPool.cbegin(), end, [&variableId](const auto &syncGroupEntry) {
46 const auto &syncGroup = syncGroupEntry.second;
47 return syncGroup.m_Variables.find(variableId) != syncGroup.m_Variables.end();
48 });
49
50 return it != end ? it->first : SyncGroupId{};
51 }
52
53 std::vector<SyncGroupId> FuzzingState::syncGroupsIds() const
54 {
55 std::vector<SyncGroupId> result{};
56
57 for (const auto &entry : m_SyncGroupsPool) {
58 result.push_back(entry.first);
59 }
60
61 return result;
62 }
63
64 void FuzzingState::synchronizeVariable(VariableId variableId, SyncGroupId syncGroupId)
65 {
66 if (syncGroupId.isNull()) {
67 return;
68 }
69
70 // Registers variable into sync group: if it's the first variable, sets the variable range as
71 // the sync group range
72 auto &syncGroup = m_SyncGroupsPool.at(syncGroupId);
73 syncGroup.m_Variables.insert(variableId);
74 if (syncGroup.m_Variables.size() == 1) {
75 auto &variableState = m_VariablesPool.at(variableId);
76 syncGroup.m_Range = variableState.m_Range;
77 }
78 }
79
80 void FuzzingState::desynchronizeVariable(VariableId variableId, SyncGroupId syncGroupId)
81 {
82 if (syncGroupId.isNull()) {
83 return;
84 }
85
86 // Unregisters variable from sync group: if there is no more variable in the group, resets the
87 // range
88 auto &syncGroup = m_SyncGroupsPool.at(syncGroupId);
89 syncGroup.m_Variables.erase(variableId);
90 if (syncGroup.m_Variables.empty()) {
91 syncGroup.m_Range = INVALID_RANGE;
92 }
93 }
94
95 void FuzzingState::updateRanges(VariableId variableId, const SqpRange &newRange)
96 {
97 auto syncGroupId = this->syncGroupId(variableId);
98
99 // Retrieves the variables to update:
100 // - if the variable is synchronized to others, updates all synchronized variables
101 // - otherwise, updates only the variable
102 auto variablesToUpdate = syncGroupId.isNull() ? std::set<VariableId>{variableId}
103 : m_SyncGroupsPool.at(syncGroupId).m_Variables;
104
105 // Sets new range
106 for (const auto &variableId : variablesToUpdate) {
107 m_VariablesPool.at(variableId).m_Range = newRange;
108 }
109 }
@@ -1,9 +1,15
1 1 #ifndef SCIQLOP_FUZZINGDEFS_H
2 2 #define SCIQLOP_FUZZINGDEFS_H
3 3
4 #include <Data/SqpRange.h>
5
4 6 #include <QString>
7 #include <QUuid>
5 8 #include <QVariantHash>
6 9
10 #include <memory>
11 #include <set>
12
7 13 // /////// //
8 14 // Aliases //
9 15 // /////// //
@@ -17,15 +23,27 using Properties = QVariantHash;
17 23 // Constants //
18 24 // ///////// //
19 25
26 /// Timeout set for data acquisition for an operation (in ms)
27 extern const QString ACQUISITION_TIMEOUT_PROPERTY;
28
20 29 /// Max number of operations to generate
21 30 extern const QString NB_MAX_OPERATIONS_PROPERTY;
22 31
32 /// Max number of sync groups to create through operations
33 extern const QString NB_MAX_SYNC_GROUPS_PROPERTY;
34
23 35 /// Max number of variables to manipulate through operations
24 36 extern const QString NB_MAX_VARIABLES_PROPERTY;
25 37
26 38 /// Set of operations available for the test
27 39 extern const QString AVAILABLE_OPERATIONS_PROPERTY;
28 40
41 /// Tolerance used for variable's cache (in ratio)
42 extern const QString CACHE_TOLERANCE_PROPERTY;
43
44 /// Range with which the timecontroller is initialized
45 extern const QString INITIAL_RANGE_PROPERTY;
46
29 47 /// Max range that an operation can reach
30 48 extern const QString MAX_RANGE_PROPERTY;
31 49
@@ -35,4 +53,76 extern const QString METADATA_POOL_PROPERTY;
35 53 /// Provider used to retrieve data
36 54 extern const QString PROVIDER_PROPERTY;
37 55
56 /// Min/max times left for an operation to execute
57 extern const QString OPERATION_DELAY_BOUNDS_PROPERTY;
58
59 /// Validators used to validate an operation
60 extern const QString VALIDATORS_PROPERTY;
61
62 /// Min/max number of operations to execute before calling validation of the current test's state
63 extern const QString VALIDATION_FREQUENCY_BOUNDS_PROPERTY;
64
65 // /////// //
66 // Structs //
67 // /////// //
68
69 class Variable;
70 struct VariableState {
71 std::shared_ptr<Variable> m_Variable{nullptr};
72 SqpRange m_Range{INVALID_RANGE};
73 };
74
75 using VariableId = int;
76 using VariablesPool = std::map<VariableId, VariableState>;
77
78 /**
79 * Defines a synchronization group for a fuzzing state. A group reports the variables synchronized
80 * with each other, and the current range of the group (i.e. range of the last synchronized variable
81 * that has been moved)
82 */
83 struct SyncGroup {
84 std::set<VariableId> m_Variables{};
85 SqpRange m_Range{INVALID_RANGE};
86 };
87
88 using SyncGroupId = QUuid;
89 using SyncGroupsPool = std::map<SyncGroupId, SyncGroup>;
90
91 /**
92 * Defines a current state during a fuzzing state. It contains all the variables manipulated during
93 * the test, as well as the synchronization status of these variables.
94 */
95 struct FuzzingState {
96 const SyncGroup &syncGroup(SyncGroupId id) const;
97 SyncGroup &syncGroup(SyncGroupId id);
98
99 const VariableState &variableState(VariableId id) const;
100 VariableState &variableState(VariableId id);
101
102 /// @return the identifier of the synchronization group in which the variable passed in
103 /// parameter is located. If the variable is not in any group, returns an invalid identifier
104 SyncGroupId syncGroupId(VariableId variableId) const;
105
106 /// @return the set of synchronization group identifiers
107 std::vector<SyncGroupId> syncGroupsIds() const;
108
109 /// Updates fuzzing state according to a variable synchronization
110 /// @param variableId the variable that is synchronized
111 /// @param syncGroupId the synchronization group
112 void synchronizeVariable(VariableId variableId, SyncGroupId syncGroupId);
113
114 /// Updates fuzzing state according to a variable desynchronization
115 /// @param variableId the variable that is desynchronized
116 /// @param syncGroupId the synchronization group from which to remove the variable
117 void desynchronizeVariable(VariableId variableId, SyncGroupId syncGroupId);
118
119 /// Updates the range of a variable and all variables to which it is synchronized
120 /// @param the variable for which to affect the range
121 /// @param the range to affect
122 void updateRanges(VariableId variableId, const SqpRange &newRange);
123
124 VariablesPool m_VariablesPool;
125 SyncGroupsPool m_SyncGroupsPool;
126 };
127
38 128 #endif // SCIQLOP_FUZZINGDEFS_H
@@ -8,18 +8,21
8 8
9 9 #include <QUuid>
10 10
11 #include <functional>
12
11 13 Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations")
12 14
13 15 namespace {
14 16
15 17 struct CreateOperation : public IFuzzingOperation {
16 bool canExecute(std::shared_ptr<Variable> variable) const override
18 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
17 19 {
18 20 // A variable can be created only if it doesn't exist yet
19 return variable == nullptr;
21 return fuzzingState.variableState(variableId).m_Variable == nullptr;
20 22 }
21 23
22 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
24 void execute(VariableId variableId, FuzzingState &fuzzingState,
25 VariableController &variableController,
23 26 const Properties &properties) const override
24 27 {
25 28 // Retrieves metadata pool from properties, and choose one of the metadata entries to
@@ -32,29 +35,181 struct CreateOperation : public IFuzzingOperation {
32 35 = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >();
33 36
34 37 auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString());
35 qCInfo(LOG_FuzzingOperations())
36 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")";
38 qCInfo(LOG_FuzzingOperations()).noquote()
39 << "Creating variable" << variableName << "(metadata:" << variableMetadata << ")...";
37 40
38 41 auto newVariable
39 42 = variableController.createVariable(variableName, variableMetadata, variableProvider);
40 std::swap(variable, newVariable);
43
44 // Updates variable's state
45 auto &variableState = fuzzingState.variableState(variableId);
46 variableState.m_Range = properties.value(INITIAL_RANGE_PROPERTY).value<SqpRange>();
47 std::swap(variableState.m_Variable, newVariable);
41 48 }
42 49 };
43 50
44 struct UnknownOperation : public IFuzzingOperation {
45 bool canExecute(std::shared_ptr<Variable> variable) const override
51 struct DeleteOperation : public IFuzzingOperation {
52 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
46 53 {
47 Q_UNUSED(variable);
48 return false;
54 // A variable can be delete only if it exists
55 return fuzzingState.variableState(variableId).m_Variable != nullptr;
49 56 }
50 57
51 void execute(std::shared_ptr<Variable> &variable, VariableController &variableController,
58 void execute(VariableId variableId, FuzzingState &fuzzingState,
59 VariableController &variableController, const Properties &) const override
60 {
61 auto &variableState = fuzzingState.variableState(variableId);
62
63 qCInfo(LOG_FuzzingOperations()).noquote()
64 << "Deleting variable" << variableState.m_Variable->name() << "...";
65 variableController.deleteVariable(variableState.m_Variable);
66
67 // Updates variable's state
68 variableState.m_Range = INVALID_RANGE;
69 variableState.m_Variable = nullptr;
70
71 // Desynchronizes the variable if it was in a sync group
72 auto syncGroupId = fuzzingState.syncGroupId(variableId);
73 fuzzingState.desynchronizeVariable(variableId, syncGroupId);
74 }
75 };
76
77 /**
78 * Defines a move operation through a range.
79 *
80 * A move operation is determined by three functions:
81 * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range
82 * are going during the operation. These functions will be:
83 * -- {<- / <-} for pan left
84 * -- {-> / ->} for pan right
85 * -- {-> / <-} for zoom in
86 * -- {<- / ->} for zoom out
87 * - One 'max move' functions, used to compute the max delta at which the operation can move a
88 * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10},
89 * max deltas will be:
90 * -- {0, 4} for pan left
91 * -- {6, 10} for pan right
92 * -- {3, 3} for zoom in
93 * -- {0, 6} for zoom out (same spacing left and right)
94 */
95 struct MoveOperation : public IFuzzingOperation {
96 using MoveFunction = std::function<double(double currentValue, double maxValue)>;
97 using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>;
98
99 explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun,
100 MaxMoveFunction maxMoveFun,
101 const QString &label = QStringLiteral("Move operation"))
102 : m_RangeStartMoveFun{std::move(rangeStartMoveFun)},
103 m_RangeEndMoveFun{std::move(rangeEndMoveFun)},
104 m_MaxMoveFun{std::move(maxMoveFun)},
105 m_Label{label}
106 {
107 }
108
109 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
110 {
111 return fuzzingState.variableState(variableId).m_Variable != nullptr;
112 }
113
114 void execute(VariableId variableId, FuzzingState &fuzzingState,
115 VariableController &variableController,
52 116 const Properties &properties) const override
53 117 {
54 Q_UNUSED(variable);
55 Q_UNUSED(variableController);
56 Q_UNUSED(properties);
57 // Does nothing
118 auto &variableState = fuzzingState.variableState(variableId);
119 auto variable = variableState.m_Variable;
120
121 // Gets the max range defined
122 auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE))
123 .value<SqpRange>();
124 auto variableRange = variableState.m_Range;
125
126 if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart
127 || variableRange.m_TEnd > maxRange.m_TEnd) {
128 qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range";
129 return;
130 }
131
132 // Computes the max delta at which the variable can move, up to the limits of the max range
133 auto deltaMax = m_MaxMoveFun(variableRange, maxRange);
134
135 // Generates random delta that will be used to move variable
136 auto delta = RandomGenerator::instance().generateDouble(0, deltaMax);
137
138 // Moves variable to its new range
139 auto isSynchronized = !fuzzingState.syncGroupId(variableId).isNull();
140 auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta),
141 m_RangeEndMoveFun(variableRange.m_TEnd, delta)};
142 qCInfo(LOG_FuzzingOperations()).noquote()
143 << "Performing" << m_Label << "on" << variable->name() << "(from" << variableRange
144 << "to" << newVariableRange << ")...";
145 variableController.onRequestDataLoading({variable}, newVariableRange, isSynchronized);
146
147 // Updates state
148 fuzzingState.updateRanges(variableId, newVariableRange);
149 }
150
151 MoveFunction m_RangeStartMoveFun;
152 MoveFunction m_RangeEndMoveFun;
153 MaxMoveFunction m_MaxMoveFun;
154 QString m_Label;
155 };
156
157 struct SynchronizeOperation : public IFuzzingOperation {
158 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
159 {
160 auto variable = fuzzingState.variableState(variableId).m_Variable;
161 return variable != nullptr && !fuzzingState.m_SyncGroupsPool.empty()
162 && fuzzingState.syncGroupId(variableId).isNull();
163 }
164
165 void execute(VariableId variableId, FuzzingState &fuzzingState,
166 VariableController &variableController, const Properties &) const override
167 {
168 auto &variableState = fuzzingState.variableState(variableId);
169
170 // Chooses a random synchronization group and adds the variable into sync group
171 auto syncGroupId = RandomGenerator::instance().randomChoice(fuzzingState.syncGroupsIds());
172 qCInfo(LOG_FuzzingOperations()).noquote()
173 << "Adding" << variableState.m_Variable->name() << "into synchronization group"
174 << syncGroupId << "...";
175 variableController.onAddSynchronized(variableState.m_Variable, syncGroupId);
176
177 // Updates state
178 fuzzingState.synchronizeVariable(variableId, syncGroupId);
179 }
180 };
181
182 struct DesynchronizeOperation : public IFuzzingOperation {
183 bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override
184 {
185 auto variable = fuzzingState.variableState(variableId).m_Variable;
186 return variable != nullptr && !fuzzingState.syncGroupId(variableId).isNull();
187 }
188
189 void execute(VariableId variableId, FuzzingState &fuzzingState,
190 VariableController &variableController, const Properties &) const override
191 {
192 auto &variableState = fuzzingState.variableState(variableId);
193
194 // Gets the sync group of the variable
195 auto syncGroupId = fuzzingState.syncGroupId(variableId);
196
197 qCInfo(LOG_FuzzingOperations()).noquote()
198 << "Removing" << variableState.m_Variable->name() << "from synchronization group"
199 << syncGroupId << "...";
200 variableController.onAddSynchronized(variableState.m_Variable, syncGroupId);
201
202 // Updates state
203 fuzzingState.desynchronizeVariable(variableId, syncGroupId);
204 }
205 };
206
207 struct UnknownOperation : public IFuzzingOperation {
208 bool canExecute(VariableId, const FuzzingState &) const override { return false; }
209
210 void execute(VariableId, FuzzingState &, VariableController &,
211 const Properties &) const override
212 {
58 213 }
59 214 };
60 215
@@ -65,6 +220,42 std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperat
65 220 switch (type) {
66 221 case FuzzingOperationType::CREATE:
67 222 return std::make_unique<CreateOperation>();
223 case FuzzingOperationType::DELETE:
224 return std::make_unique<DeleteOperation>();
225 case FuzzingOperationType::PAN_LEFT:
226 return std::make_unique<MoveOperation>(
227 std::minus<double>(), std::minus<double>(),
228 [](const SqpRange &range, const SqpRange &maxRange) {
229 return range.m_TStart - maxRange.m_TStart;
230 },
231 QStringLiteral("Pan left operation"));
232 case FuzzingOperationType::PAN_RIGHT:
233 return std::make_unique<MoveOperation>(
234 std::plus<double>(), std::plus<double>(),
235 [](const SqpRange &range, const SqpRange &maxRange) {
236 return maxRange.m_TEnd - range.m_TEnd;
237 },
238 QStringLiteral("Pan right operation"));
239 case FuzzingOperationType::ZOOM_IN:
240 return std::make_unique<MoveOperation>(
241 std::plus<double>(), std::minus<double>(),
242 [](const SqpRange &range, const SqpRange &maxRange) {
243 Q_UNUSED(maxRange)
244 return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.;
245 },
246 QStringLiteral("Zoom in operation"));
247 case FuzzingOperationType::ZOOM_OUT:
248 return std::make_unique<MoveOperation>(
249 std::minus<double>(), std::plus<double>(),
250 [](const SqpRange &range, const SqpRange &maxRange) {
251 return std::min(range.m_TStart - maxRange.m_TStart,
252 maxRange.m_TEnd - range.m_TEnd);
253 },
254 QStringLiteral("Zoom out operation"));
255 case FuzzingOperationType::SYNCHRONIZE:
256 return std::make_unique<SynchronizeOperation>();
257 case FuzzingOperationType::DESYNCHRONIZE:
258 return std::make_unique<DesynchronizeOperation>();
68 259 default:
69 260 // Default case returns unknown operation
70 261 break;
@@ -11,28 +11,38
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_FuzzingOperations)
13 13
14 class Variable;
15 14 class VariableController;
16 15
17 16 /**
18 17 * Enumeration of types of existing fuzzing operations
19 18 */
20 enum class FuzzingOperationType { CREATE };
19 enum class FuzzingOperationType {
20 CREATE,
21 DELETE,
22 PAN_LEFT,
23 PAN_RIGHT,
24 ZOOM_IN,
25 ZOOM_OUT,
26 SYNCHRONIZE,
27 DESYNCHRONIZE
28 };
21 29
22 30 /// Interface that represents an operation that can be executed during a fuzzing test
23 31 struct IFuzzingOperation {
24 32 virtual ~IFuzzingOperation() noexcept = default;
25 33
26 /// Checks if the operation can be executed according to the current state of the variable
27 /// passed in parameter
28 virtual bool canExecute(std::shared_ptr<Variable> variable) const = 0;
29 /// Executes the operation on the variable passed in parameter
30 /// @param variable the variable on which to execute the operation
34 /// Checks if the operation can be executed according to the current test's state for the
35 /// variable passed in parameter
36 virtual bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const = 0;
37 /// Executes the operation on the variable for which its identifier is passed in parameter
38 /// @param variableId the variable identifier
39 /// @param fuzzingState the current test's state on which to find the variable and execute the
40 /// operation
31 41 /// @param variableController the controller associated to the operation
32 42 /// @param properties properties that can be used to configure the operation
33 /// @remarks variable is passed as a reference because, according to the operation, it can be
34 /// modified (in/out parameter)
35 virtual void execute(std::shared_ptr<Variable> &variable,
43 /// @remarks fuzzingState is passed as a reference because, according to the operation, it can
44 /// be modified (in/out parameter)
45 virtual void execute(VariableId variableId, FuzzingState &fuzzingState,
36 46 VariableController &variableController,
37 47 const Properties &properties = {}) const = 0;
38 48 };
@@ -43,7 +53,4 struct FuzzingOperationFactory {
43 53 static std::unique_ptr<IFuzzingOperation> create(FuzzingOperationType type);
44 54 };
45 55
46 using OperationsTypes = std::set<FuzzingOperationType>;
47 Q_DECLARE_METATYPE(OperationsTypes)
48
49 56 #endif // SCIQLOP_FUZZINGOPERATIONS_H
@@ -1,6 +1,7
1 1 #ifndef SCIQLOP_FUZZINGUTILS_H
2 2 #define SCIQLOP_FUZZINGUTILS_H
3 3
4 #include <algorithm>
4 5 #include <random>
5 6
6 7 /**
@@ -16,10 +17,17 public:
16 17 /// Generates a random int between [min, max]
17 18 int generateInt(int min, int max);
18 19
19 /// Returns a random element among the elements of a container. If the container is empty,
20 /// returns an element built by default
20 /**
21 * Returns a random element among the elements of a container. An item may be more likely to be
22 * selected if it has an associated weight greater than other items
23 * @param container the container from which to retrieve an element
24 * @param weights the weight associated to each element of the container. The vector must have
25 * the same size of the container for the weights to be effective
26 * @param nbDraws the number of random draws to perform
27 * @return the random element retrieved, an element built by default if the container is empty
28 */
21 29 template <typename T, typename ValueType = typename T::value_type>
22 ValueType randomChoice(const T &container);
30 ValueType randomChoice(const T &container, const std::vector<double> &weights = {});
23 31
24 32 private:
25 33 std::mt19937 m_Mt;
@@ -28,14 +36,28 private:
28 36 };
29 37
30 38 template <typename T, typename ValueType>
31 ValueType RandomGenerator::randomChoice(const T &container)
39 ValueType RandomGenerator::randomChoice(const T &container, const std::vector<double> &weights)
32 40 {
33 41 if (container.empty()) {
34 42 return ValueType{};
35 43 }
36 44
37 auto randomIndex = generateInt(0, container.size() - 1);
38 return container.at(randomIndex);
45 // Generates weights for each element: if the weights passed in parameter are malformed (the
46 // number of weights defined is inconsistent with the number of elements in the container, or
47 // all weights are zero), default weights are used
48 auto nbIndexes = container.size();
49 std::vector<double> indexWeights(nbIndexes);
50 if (weights.size() != nbIndexes || std::all_of(weights.cbegin(), weights.cend(),
51 [](const auto &val) { return val == 0.; })) {
52 std::fill(indexWeights.begin(), indexWeights.end(), 1.);
53 }
54 else {
55 std::copy(weights.begin(), weights.end(), indexWeights.begin());
56 }
57
58 // Performs a draw to determine the index to return
59 std::discrete_distribution<> d{indexWeights.cbegin(), indexWeights.cend()};
60 return container.at(d(m_Mt));
39 61 }
40 62
41 63 #endif // SCIQLOP_FUZZINGUTILS
@@ -1,12 +1,16
1 1 #include "FuzzingDefs.h"
2 2 #include "FuzzingOperations.h"
3 3 #include "FuzzingUtils.h"
4 #include "FuzzingValidators.h"
4 5
5 6 #include "AmdaProvider.h"
6 7
8 #include <Common/SignalWaiter.h>
7 9 #include <Network/NetworkController.h>
10 #include <Settings/SqpSettingsDefs.h>
8 11 #include <SqpApplication.h>
9 12 #include <Time/TimeController.h>
13 #include <Variable/Variable.h>
10 14 #include <Variable/VariableController.h>
11 15
12 16 #include <QLoggingCategory>
@@ -17,29 +21,76
17 21
18 22 Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing")
19 23
24 /**
25 * Macro used to generate a getter for a property in @sa FuzzingTest. The macro generates a static
26 * attribute that is initialized by searching in properties the property and use a default value if
27 * it's not present. Macro arguments are:
28 * - GETTER_NAME : name of the getter
29 * - PROPERTY_NAME: used to generate constants for property's name ({PROPERTY_NAME}_PROPERTY) and
30 * default value ({PROPERTY_NAME}_DEFAULT_VALUE)
31 * - TYPE : return type of the getter
32 */
33 // clang-format off
34 #define DECLARE_PROPERTY_GETTER(GETTER_NAME, PROPERTY_NAME, TYPE) \
35 TYPE GETTER_NAME() const \
36 { \
37 static auto result = m_Properties.value(PROPERTY_NAME##_PROPERTY, PROPERTY_NAME##_DEFAULT_VALUE).value<TYPE>(); \
38 return result; \
39 } \
40 // clang-format on
41
20 42 namespace {
21 43
22 44 // /////// //
23 45 // Aliases //
24 46 // /////// //
25 47
26 using VariableId = int;
48 using IntPair = std::pair<int, int>;
49 using Weight = double;
50 using Weights = std::vector<Weight>;
51
52 struct OperationProperty {
53 Weight m_Weight{1.};
54 bool m_WaitAcquisition{false};
55 };
27 56
28 57 using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >;
29 58 using VariablesOperations = std::vector<VariableOperation>;
30 59
31 using OperationsPool = std::set<std::shared_ptr<IFuzzingOperation> >;
32 using VariablesPool = std::map<VariableId, std::shared_ptr<Variable> >;
60 using OperationsTypes = std::map<FuzzingOperationType, OperationProperty>;
61 using OperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, OperationProperty>;
62
63 using Validators = std::vector<std::shared_ptr<IFuzzingValidator> >;
33 64
34 65 // ///////// //
35 66 // Constants //
36 67 // ///////// //
37 68
38 69 // Defaults values used when the associated properties have not been set for the test
70 const auto ACQUISITION_TIMEOUT_DEFAULT_VALUE = 30000;
39 71 const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 100;
72 const auto NB_MAX_SYNC_GROUPS_DEFAULT_VALUE = 1;
40 73 const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 1;
41 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
42 = QVariant::fromValue(OperationsTypes{FuzzingOperationType::CREATE});
74 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE = QVariant::fromValue(
75 OperationsTypes{{FuzzingOperationType::CREATE, {1., true}},
76 {FuzzingOperationType::DELETE, {0.1}}, // Delete operation is less frequent
77 {FuzzingOperationType::PAN_LEFT, {1.}},
78 {FuzzingOperationType::PAN_RIGHT, {1.}},
79 {FuzzingOperationType::ZOOM_IN, {1.}},
80 {FuzzingOperationType::ZOOM_OUT, {1.}},
81 {FuzzingOperationType::SYNCHRONIZE, {0.8}},
82 {FuzzingOperationType::DESYNCHRONIZE, {0.4}}});
83 const auto CACHE_TOLERANCE_DEFAULT_VALUE = 0.2;
84
85 /// Min/max delays between each operation (in ms)
86 const auto OPERATION_DELAY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(100, 3000));
87
88 /// Validators for the tests (executed in the order in which they're defined)
89 const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue(
90 ValidatorsTypes{{FuzzingValidatorType::RANGE, FuzzingValidatorType::DATA}});
91
92 /// Min/max number of operations to execute before calling validation
93 const auto VALIDATION_FREQUENCY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(1, 10));
43 94
44 95 // /////// //
45 96 // Methods //
@@ -47,37 +98,77 const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE
47 98
48 99 /// Goes through the variables pool and operations pool to determine the set of {variable/operation}
49 100 /// pairs that are valid (i.e. operation that can be executed on variable)
50 VariablesOperations availableOperations(const VariablesPool &variablesPool,
101 std::pair<VariablesOperations, Weights> availableOperations(const FuzzingState &fuzzingState,
51 102 const OperationsPool &operationsPool)
52 103 {
53 104 VariablesOperations result{};
105 Weights weights{};
54 106
55 for (const auto &variablesPoolEntry : variablesPool) {
107 for (const auto &variablesPoolEntry : fuzzingState.m_VariablesPool) {
56 108 auto variableId = variablesPoolEntry.first;
57 auto variable = variablesPoolEntry.second;
58 109
59 for (const auto &operation : operationsPool) {
110 for (const auto &operationsPoolEntry : operationsPool) {
111 auto operation = operationsPoolEntry.first;
112 auto operationProperty = operationsPoolEntry.second;
113
60 114 // A pair is valid if the current operation can be executed on the current variable
61 if (operation->canExecute(variable)) {
115 if (operation->canExecute(variableId, fuzzingState)) {
62 116 result.push_back({variableId, operation});
117 weights.push_back(operationProperty.m_Weight);
63 118 }
64 119 }
65 120 }
66 121
67 return result;
122 return {result, weights};
68 123 }
69 124
70 125 OperationsPool createOperationsPool(const OperationsTypes &types)
71 126 {
72 127 OperationsPool result{};
73 128
129 std::transform(
130 types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) {
131 return std::make_pair(FuzzingOperationFactory::create(type.first), type.second);
132 });
133
134 return result;
135 }
136
137 Validators createValidators(const ValidatorsTypes &types)
138 {
139 Validators result{};
140
74 141 std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()),
75 [](const auto &type) { return FuzzingOperationFactory::create(type); });
142 [](const auto &type) { return FuzzingValidatorFactory::create(type); });
76 143
77 144 return result;
78 145 }
79 146
80 147 /**
148 * Validates all the variables' states passed in parameter, according to a set of validators
149 * @param variablesPool the variables' states
150 * @param validators the validators used for validation
151 */
152 void validate(const VariablesPool &variablesPool, const Validators &validators)
153 {
154 for (const auto &variablesPoolEntry : variablesPool) {
155 auto variableId = variablesPoolEntry.first;
156 const auto &variableState = variablesPoolEntry.second;
157
158 auto variableMessage = variableState.m_Variable ? variableState.m_Variable->name()
159 : QStringLiteral("null variable");
160 qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validating state of variable at index"
161 << variableId << "(" << variableMessage << ")...";
162
163 for (const auto &validator : validators) {
164 validator->validate(VariableState{variableState});
165 }
166
167 qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validation completed.";
168 }
169 }
170
171 /**
81 172 * Class to run random tests
82 173 */
83 174 class FuzzingTest {
@@ -85,79 +176,124 public:
85 176 explicit FuzzingTest(VariableController &variableController, Properties properties)
86 177 : m_VariableController{variableController},
87 178 m_Properties{std::move(properties)},
88 m_VariablesPool{}
179 m_FuzzingState{}
89 180 {
90 181 // Inits variables pool: at init, all variables are null
91 182 for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) {
92 m_VariablesPool[variableId] = nullptr;
183 m_FuzzingState.m_VariablesPool[variableId] = VariableState{};
184 }
185
186 // Inits sync groups and registers them into the variable controller
187 for (auto i = 0; i < nbMaxSyncGroups(); ++i) {
188 auto syncGroupId = SyncGroupId::createUuid();
189 variableController.onAddSynchronizationGroupId(syncGroupId);
190 m_FuzzingState.m_SyncGroupsPool[syncGroupId] = SyncGroup{};
93 191 }
94 192 }
95 193
96 194 void execute()
97 195 {
98 qCInfo(LOG_TestAmdaFuzzing()) << "Running" << nbMaxOperations() << "operations on"
196 qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Running" << nbMaxOperations() << "operations on"
99 197 << nbMaxVariables() << "variable(s)...";
100 198
199
200 // Inits the count of the number of operations before the next validation
201 int nextValidationCounter = 0;
202 auto updateValidationCounter = [this, &nextValidationCounter]() {
203 nextValidationCounter = RandomGenerator::instance().generateInt(
204 validationFrequencies().first, validationFrequencies().second);
205 qCInfo(LOG_TestAmdaFuzzing()).noquote()
206 << "Next validation in " << nextValidationCounter << "operation(s)...";
207 };
208 updateValidationCounter();
209
101 210 auto canExecute = true;
102 211 for (auto i = 0; i < nbMaxOperations() && canExecute; ++i) {
103 212 // Retrieves all operations that can be executed in the current context
104 auto variableOperations = availableOperations(m_VariablesPool, operationsPool());
213 VariablesOperations variableOperations{};
214 Weights weights{};
215 std::tie(variableOperations, weights)
216 = availableOperations(m_FuzzingState, operationsPool());
105 217
106 218 canExecute = !variableOperations.empty();
107 219 if (canExecute) {
220 --nextValidationCounter;
221
108 222 // Of the operations available, chooses a random operation and executes it
109 223 auto variableOperation
110 = RandomGenerator::instance().randomChoice(variableOperations);
224 = RandomGenerator::instance().randomChoice(variableOperations, weights);
111 225
112 226 auto variableId = variableOperation.first;
113 auto variable = m_VariablesPool.at(variableId);
114 227 auto fuzzingOperation = variableOperation.second;
115 228
116 fuzzingOperation->execute(variable, m_VariableController, m_Properties);
229 auto waitAcquisition = nextValidationCounter == 0
230 || operationsPool().at(fuzzingOperation).m_WaitAcquisition;
231
232 fuzzingOperation->execute(variableId, m_FuzzingState, m_VariableController,
233 m_Properties);
117 234
118 // Updates variable pool with the new state of the variable after operation
119 m_VariablesPool[variableId] = variable;
235 if (waitAcquisition) {
236 qCDebug(LOG_TestAmdaFuzzing()) << "Waiting for acquisition to finish...";
237 SignalWaiter{m_VariableController, SIGNAL(acquisitionFinished())}.wait(
238 acquisitionTimeout());
239
240 // Validates variables
241 if (nextValidationCounter == 0) {
242 validate(m_FuzzingState.m_VariablesPool, validators());
243 updateValidationCounter();
244 }
120 245 }
121 246 else {
122 qCInfo(LOG_TestAmdaFuzzing())
247 // Delays the next operation with a randomly generated time
248 auto delay = RandomGenerator::instance().generateInt(operationDelays().first,
249 operationDelays().second);
250 qCDebug(LOG_TestAmdaFuzzing())
251 << "Waiting " << delay << "ms before the next operation...";
252 QTest::qWait(delay);
253 }
254 }
255 else {
256 qCInfo(LOG_TestAmdaFuzzing()).noquote()
123 257 << "No more operations are available, the execution of the test will stop...";
124 258 }
125 259 }
126 260
127 qCInfo(LOG_TestAmdaFuzzing()) << "Execution of the test completed.";
261 qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Execution of the test completed.";
128 262 }
129 263
130 264 private:
131 int nbMaxOperations() const
265 OperationsPool operationsPool() const
132 266 {
133 static auto result
134 = m_Properties.value(NB_MAX_OPERATIONS_PROPERTY, NB_MAX_OPERATIONS_DEFAULT_VALUE)
135 .toInt();
267 static auto result = createOperationsPool(
268 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
269 .value<OperationsTypes>());
136 270 return result;
137 271 }
138 272
139 int nbMaxVariables() const
273 Validators validators() const
140 274 {
141 275 static auto result
142 = m_Properties.value(NB_MAX_VARIABLES_PROPERTY, NB_MAX_VARIABLES_DEFAULT_VALUE).toInt();
276 = createValidators(m_Properties.value(VALIDATORS_PROPERTY, VALIDATORS_DEFAULT_VALUE)
277 .value<ValidatorsTypes>());
143 278 return result;
144 279 }
145 280
146 OperationsPool operationsPool() const
147 {
148 static auto result = createOperationsPool(
149 m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE)
150 .value<OperationsTypes>());
151 return result;
152 }
281 DECLARE_PROPERTY_GETTER(nbMaxOperations, NB_MAX_OPERATIONS, int)
282 DECLARE_PROPERTY_GETTER(nbMaxSyncGroups, NB_MAX_SYNC_GROUPS, int)
283 DECLARE_PROPERTY_GETTER(nbMaxVariables, NB_MAX_VARIABLES, int)
284 DECLARE_PROPERTY_GETTER(operationDelays, OPERATION_DELAY_BOUNDS, IntPair)
285 DECLARE_PROPERTY_GETTER(validationFrequencies, VALIDATION_FREQUENCY_BOUNDS, IntPair)
286 DECLARE_PROPERTY_GETTER(acquisitionTimeout, ACQUISITION_TIMEOUT, int)
153 287
154 288 VariableController &m_VariableController;
155 289 Properties m_Properties;
156 VariablesPool m_VariablesPool;
290 FuzzingState m_FuzzingState;
157 291 };
158 292
159 293 } // namespace
160 294
295 Q_DECLARE_METATYPE(OperationsTypes)
296
161 297 class TestAmdaFuzzing : public QObject {
162 298 Q_OBJECT
163 299
@@ -169,6 +305,9 private slots:
169 305
170 306 void TestAmdaFuzzing::testFuzzing_data()
171 307 {
308 // Note: Comment this line to run fuzzing tests
309 QSKIP("Fuzzing tests are disabled by default");
310
172 311 // ////////////// //
173 312 // Test structure //
174 313 // ////////////// //
@@ -196,6 +335,12 void TestAmdaFuzzing::testFuzzing()
196 335 {
197 336 QFETCH(Properties, properties);
198 337
338 // Sets cache property
339 QSettings settings{};
340 auto cacheTolerance = properties.value(CACHE_TOLERANCE_PROPERTY, CACHE_TOLERANCE_DEFAULT_VALUE);
341 settings.setValue(GENERAL_TOLERANCE_AT_INIT_KEY, cacheTolerance);
342 settings.setValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, cacheTolerance);
343
199 344 auto &variableController = sqpApp->variableController();
200 345 auto &timeController = sqpApp->timeController();
201 346
@@ -215,8 +360,9 void TestAmdaFuzzing::testFuzzing()
215 360
216 361 // Sets initial range on time controller
217 362 SqpRange initialRange{initialRangeStart, initialRangeEnd};
218 qCInfo(LOG_TestAmdaFuzzing()) << "Setting initial range to" << initialRange << "...";
363 qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Setting initial range to" << initialRange << "...";
219 364 timeController.onTimeToUpdate(initialRange);
365 properties.insert(INITIAL_RANGE_PROPERTY, QVariant::fromValue(initialRange));
220 366
221 367 FuzzingTest test{variableController, properties};
222 368 test.execute();
@@ -229,9 +375,13 int main(int argc, char *argv[])
229 375 "*.info=false\n"
230 376 "*.debug=false\n"
231 377 "FuzzingOperations.info=true\n"
378 "FuzzingValidators.info=true\n"
232 379 "TestAmdaFuzzing.info=true\n");
233 380
234 381 SqpApplication app{argc, argv};
382 SqpApplication::setOrganizationName("LPP");
383 SqpApplication::setOrganizationDomain("lpp.fr");
384 SqpApplication::setApplicationName("SciQLop-TestFuzzing");
235 385 app.setAttribute(Qt::AA_Use96Dpi, true);
236 386 TestAmdaFuzzing testObject{};
237 387 QTEST_SET_MAIN_SOURCE_PATH
General Comments 0
You need to be logged in to leave comments. Login now