-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented compressing proxy for events, which skips the processing …
…of superseded events. Use it in ControlObjectScript Implemented a unit test which tests the compressing behavior in ControlObjectScript
- Loading branch information
1 parent
c0679a2
commit 6a40aae
Showing
7 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "control/controlcompressingproxy.h" | ||
|
||
#include "moc_controlcompressingproxy.cpp" | ||
|
||
// Compressor | ||
CompressingProxy::CompressingProxy(QObject* parent) | ||
: QObject(parent){}; | ||
|
||
bool CompressingProxy::emitCheck() { | ||
m_lastEventRecursionOngoing = true; | ||
|
||
// sendPostedEventstriggers recursive execution of slotValueChanged until no more events for this slot are in the queue | ||
QCoreApplication::sendPostedEvents(this, QEvent::MetaCall); | ||
|
||
// Execution continues here, if last event for this slot is processed | ||
bool isLastEvent = m_lastEventRecursionOngoing; | ||
m_lastEventRecursionOngoing = false; | ||
return isLastEvent; | ||
} | ||
|
||
void CompressingProxy::slotValueChanged(double value, QObject* obj) { | ||
if (emitCheck()) | ||
emit signalValueChanged(value, obj); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <QApplication> | ||
#include <QMetaObject> | ||
|
||
class CompressingProxy : public QObject { | ||
Q_OBJECT | ||
private: | ||
bool emitCheck(); | ||
|
||
bool m_lastEventRecursionOngoing; | ||
|
||
public slots: | ||
void slotValueChanged(double value, QObject* obj); | ||
|
||
signals: | ||
void signalValueChanged(double, QObject*); | ||
|
||
public: | ||
// No default constructor, since the proxy must be a child of the | ||
// target object. | ||
explicit CompressingProxy(QObject* parent); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
#include <QtDebug> | ||
|
||
#include "control/controlobjectscript.h" | ||
#include "control/controlobject.h" | ||
#include "util/memory.h" | ||
#include "test/mixxxtest.h" | ||
|
||
using ::testing::StrictMock; | ||
using ::testing::Return; | ||
using ::testing::Invoke; | ||
using ::testing::DoubleEq; | ||
using ::testing::_; | ||
|
||
namespace { | ||
|
||
const RuntimeLoggingCategory k_logger(QString("test").toLocal8Bit()); | ||
|
||
class MockControlObjectScript : public ControlObjectScript { | ||
public: | ||
MockControlObjectScript(const ConfigKey& key, const RuntimeLoggingCategory& logger, QObject* pParent) | ||
: ControlObjectScript(key, logger, pParent) { | ||
} | ||
~MockControlObjectScript() override { | ||
} | ||
MOCK_METHOD2(slotValueChanged, void(double value, QObject*)); | ||
}; | ||
|
||
class ControlObjectScriptTest : public MixxxTest { | ||
protected: | ||
void SetUp() override { | ||
ck1 = ConfigKey("[Channel1]", "co1"); | ||
ck2 = ConfigKey("[Channel1]", "co2"); | ||
co1 = std::make_unique<ControlObject>(ck1); | ||
co2 = std::make_unique<ControlObject>(ck2); | ||
|
||
coScript1 = std::make_unique<MockControlObjectScript>(ck1, k_logger, nullptr); | ||
coScript2 = std::make_unique <MockControlObjectScript>(ck2, k_logger, nullptr); | ||
|
||
conn1.key = ck1; | ||
conn1.engineJSProxy = nullptr; | ||
conn1.controllerEngine = nullptr; | ||
conn1.callback = "mock_callback1"; | ||
conn1.id = QUuid::createUuid(); | ||
|
||
conn2.key = ck2; | ||
conn2.engineJSProxy = nullptr; | ||
conn2.controllerEngine = nullptr; | ||
conn2.callback = "mock_callback2"; | ||
conn2.id = QUuid::createUuid(); | ||
|
||
|
||
coScript1->addScriptConnection(conn1); | ||
coScript2->addScriptConnection(conn2); | ||
} | ||
|
||
ConfigKey ck1, ck2; | ||
std::unique_ptr<ControlObject> co1; | ||
std::unique_ptr<ControlObject> co2; | ||
|
||
std::unique_ptr<MockControlObjectScript> coScript1; | ||
std::unique_ptr<MockControlObjectScript> coScript2; | ||
|
||
|
||
ScriptConnection conn1, conn2; | ||
}; | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareCount1) { | ||
// Check that slotValueChanged callback is never called for conn2 | ||
EXPECT_CALL(*coScript2, slotValueChanged(_, _)) | ||
.Times(0) | ||
.WillOnce(Return()); | ||
// Check that slotValueChanged callback is called only once (independent of the value) | ||
EXPECT_CALL(*coScript1, slotValueChanged(_, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
co1->set(1.0); | ||
application()->processEvents(); | ||
} | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareValue1) { | ||
EXPECT_CALL(*coScript1, slotValueChanged(2.0, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
|
||
co1->set(2.0); | ||
application()->processEvents(); | ||
|
||
} | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareCount2) { | ||
// Check that slotValueChanged callback is never called for conn2 | ||
EXPECT_CALL(*coScript2, slotValueChanged(_, _)) | ||
.Times(0) | ||
.WillOnce(Return()); | ||
// Check that slotValueChanged callback for conn1 is called only once (independent of the value) | ||
EXPECT_CALL(*coScript1, slotValueChanged(_, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
co1->set(3.0); | ||
co1->set(4.0); | ||
application()->processEvents(); | ||
} | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareValue2) { | ||
// Check that slotValueChanged callback is called with the last set value | ||
EXPECT_CALL(*coScript1, slotValueChanged(6.0, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
co1->set(5.0); | ||
co1->set(6.0); | ||
application()->processEvents(); | ||
} | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareCountMulti) { | ||
// Check that slotValueChanged callback for conn1 and conn2 is called only once (independent of the value) | ||
EXPECT_CALL(*coScript1, slotValueChanged(_, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
// Check that slotValueChanged callback for conn1 and conn2 is called only once (independent of the value) | ||
EXPECT_CALL(*coScript2, slotValueChanged(_, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
co1->set(10.0); | ||
co2->set(11.0); | ||
co1->set(12.0); | ||
co2->set(13.0); | ||
application()->processEvents(); | ||
} | ||
|
||
TEST_F(ControlObjectScriptTest, CompressingProxyCompareValueMulti) { | ||
// Check that slotValueChanged callback is called with the last set value | ||
EXPECT_CALL(*coScript1, slotValueChanged(22.0, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
EXPECT_CALL(*coScript2, slotValueChanged(23.0, _)) | ||
.Times(1) | ||
.WillOnce(Return()); | ||
co1->set(20.0); | ||
co2->set(21.0); | ||
co1->set(22.0); | ||
co2->set(23.0); | ||
application()->processEvents(); | ||
} | ||
|
||
} // namespace |