local_scheduler_test.cpp 8.74 KB
Newer Older
1
2
3
4
5
#include <iostream>
#include <string>
#include <memory>

// simulation scheduler includes
Pascal Palenda's avatar
Pascal Palenda committed
6
#include <ITA/SimulationScheduler/RoomAcoustics/master_simulation_controller.h>
7

Pascal Palenda's avatar
Pascal Palenda committed
8
9
10
11
12
13
14
15
#include <ITA/SimulationScheduler/result_handler.h>
#include <ITA/SimulationScheduler/update_message.h>
#include <ITA/SimulationScheduler/update_scene.h>
#include <ITA/SimulationScheduler/update_config.h>
#include "../src/ITA/SimulationScheduler/replacement_filter.h"
#include <ITA/SimulationScheduler/RoomAcoustics/rir_simulation_result.h>
#include <ITA/SimulationScheduler/AudibilityFilter/filter_network.h>
#include <ITA/SimulationScheduler/AudibilityFilter/audibility_filter_interface.h>
16
17
18
19
20
21
22
23
24
25
26
27
28

// Vista includes
#include <VistaInterProcComm/Connections/VistaByteBufferSerializer.h>
#include <VistaInterProcComm/Connections/VistaByteBufferDeSerializer.h>
#include <VistaBase/VistaTimeUtils.h>

// GTest
#include "gtest/gtest.h"

#include "enum_to_int.cpp"
#include "mocks.hpp"
#include "test_classes.hpp"

29
30
using namespace ITA::SimulationScheduler;
using namespace ITA::SimulationScheduler::RoomAcoustics;
31
32
33
34
35
36
37
38
39

struct LocalSchedulerTest : public testing::Test
{
	std::unique_ptr<TestLocalScheduler> scheduler;

	MockResultHandler* handler;

	MockWorker* worker;

Pascal Palenda's avatar
Pascal Palenda committed
40
	//MockAudibilityFilter* filter;
41
42
43

	LocalSchedulerTest ( )
	{
44
		AudibilityFilter::CAudibilityFilterFactory::RegisterObject ( "MockFilter",
Pascal Palenda's avatar
Pascal Palenda committed
45
46
																	 MockAudibilityFilter::createFilter,
																	 std::make_shared<MockAudibilityFilter::MockFilterConfig> );
47
		CWorkerFactory::RegisterObject ( "MockWorker", MockWorker::createWorker, std::make_shared<MockWorker::MockWorkerConfig> );
48

Pascal Palenda's avatar
Pascal Palenda committed
49
50
51
52
53
54
		auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );

		const auto workerConfig = std::make_shared<MockWorker::MockWorkerConfig> ( );
		schedulerConfig.vpWorkerConfigs = { workerConfig };

		auto filterConfig = std::make_shared<MockAudibilityFilter::MockFilterConfig> ( );
55
		filterConfig->behaviour = MockAudibilityFilter::Behaviour::onlyID_42_audible;
Pascal Palenda's avatar
Pascal Palenda committed
56
		filterConfig->sFilterName = "Filter";
57
		AudibilityFilter::CFilterNetwork::FilterNetworkConfig filterNetworkConfig;
Pascal Palenda's avatar
Pascal Palenda committed
58
59
60
61
		filterNetworkConfig.vpFilterConfigs = { filterConfig };
		filterNetworkConfig.sStartFilter = filterConfig->sFilterName;

		schedulerConfig.oFilterNetworkConfig = filterNetworkConfig;
62
63
64
65
66

		scheduler = std::make_unique<TestLocalScheduler> ( schedulerConfig );

		worker = dynamic_cast< MockWorker* >( scheduler->getWorkerInterfaces ( ).front ( ) );

Pascal Palenda's avatar
Pascal Palenda committed
67
		//filter = dynamic_cast< MockAudibilityFilter* >( scheduler->getFilterInterfaces ( ).front ( ) );
68
69
70

		handler = new MockResultHandler ( );

71
		scheduler->AttachResultHandler ( handler );
72
73
74
75
76
	}
};

TEST ( LocalSchedulerCreationTest, WithOneWorker )
{
77
	CWorkerFactory::RegisterObject ( "MockWorker", MockWorker::createWorker, std::make_shared<MockWorker::MockWorkerConfig> );
78

Pascal Palenda's avatar
Pascal Palenda committed
79
	auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );
80

Pascal Palenda's avatar
Pascal Palenda committed
81
82
	const auto  workerConfig = std::make_shared<MockWorker::MockWorkerConfig> ( );
	schedulerConfig.vpWorkerConfigs = { workerConfig };
83
84
85
86
87
88
89
90

	auto scheduler = std::make_unique<TestLocalScheduler> ( schedulerConfig );

	ASSERT_EQ ( scheduler->getWorkerInterfaces ( ).size ( ), 1 );
}

TEST ( LocalSchedulerCreationTest, WithOneFilter )
{
91
	AudibilityFilter::CAudibilityFilterFactory::RegisterObject ( "MockFilter",
Pascal Palenda's avatar
Pascal Palenda committed
92
93
																 MockAudibilityFilter::createFilter,
																 std::make_shared<MockAudibilityFilter::MockFilterConfig> );
94

Pascal Palenda's avatar
Pascal Palenda committed
95
	auto schedulerConfig = CScheduler::LocalSchedulerConfig ( );
96

Pascal Palenda's avatar
Pascal Palenda committed
97
	auto filterConfig = std::make_shared<MockAudibilityFilter::MockFilterConfig> ( );
98
	filterConfig->behaviour = MockAudibilityFilter::Behaviour::nothingAudible;
Pascal Palenda's avatar
Pascal Palenda committed
99
	filterConfig->sFilterName = "Filter";
100
	AudibilityFilter::CFilterNetwork::FilterNetworkConfig filterNetworkConfig;
Pascal Palenda's avatar
Pascal Palenda committed
101
102
103
104
	filterNetworkConfig.vpFilterConfigs = { filterConfig };
	filterNetworkConfig.sStartFilter = filterConfig->sFilterName;

	schedulerConfig.oFilterNetworkConfig = filterNetworkConfig;
105
106

	auto scheduler = std::make_unique<TestLocalScheduler> ( schedulerConfig );
Pascal Palenda's avatar
Pascal Palenda committed
107
108

	ASSERT_NE ( scheduler->getNetwork(), nullptr );
109
110
111
112
113
114
}

TEST_F ( LocalSchedulerTest, UpdateToWorker )
{
	for ( int i = 0; i < 50; ++i )
	{
Pascal Palenda's avatar
Pascal Palenda committed
115
		if ( i == 30 )
116
117
118
		{
			VistaTimeUtils::Sleep ( 50 );
		}
Pascal Palenda's avatar
Pascal Palenda committed
119

120
		auto update = std::make_unique<CUpdateScene> ( 1 );
121
		update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
122
										std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
123
		scheduler->PushUpdate ( std::move ( update ) );
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	}

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

	const auto updates = worker->vUpdates;

	EXPECT_GE ( updates.size ( ), 1 );
	EXPECT_LE ( updates.size ( ), 50 );
}

TEST_F ( LocalSchedulerTest, UpdateToHandler )
{
	for ( int i = 0; i < 50; ++i )
	{
		auto update = std::make_unique<CUpdateScene> ( 1 );
140
		update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
141
										std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
142
		scheduler->PushUpdate ( std::move ( update ) );
143
144
145
146
147
148
149
150
151
152
153
154
	}

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

	EXPECT_GE ( handler->resultVector.size ( ), 1 );
	EXPECT_LE ( handler->resultVector.size ( ), 50 );
}

TEST_F ( LocalSchedulerTest, UpdateToHandler1 )
{
	auto update = std::make_unique<CUpdateScene> ( 1 );
155
	update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
156
									std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
157
	scheduler->PushUpdate ( std::move ( update ) );
158
159
160
161
162
163
164
165
166

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

	EXPECT_EQ ( handler->resultVector.size ( ), 1 );
}

TEST_F ( LocalSchedulerTest, Reset )
{
167
	scheduler->PushUpdate ( std::make_unique<CUpdateConfig> ( CUpdateConfig::ConfigChangeType::resetAll ) );
168
169
170
171
172
173
174

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 1000 );

	EXPECT_EQ ( true, worker->bReset );
}

Pascal Palenda's avatar
Pascal Palenda committed
175
TEST_F ( LocalSchedulerTest, Filter )
176
177
178
179
{
	for ( int i = 0; i < 50; ++i )
	{
		auto update = std::make_unique<CUpdateScene> ( 1 );
180
		update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
181
										std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
182
		scheduler->PushUpdate ( std::move ( update ) );
183
184
185
186
187
	}

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

188
	// Send another bunch of updates afterwards, so the filter replace is not being used.
189
190
191
	for ( int i = 0; i < 50; ++i )
	{
		auto update = std::make_unique<CUpdateScene> ( 1 );
192
		update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
193
										std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
194
		scheduler->PushUpdate ( std::move ( update ) );
195
196
197
198
199
200
201
	}

	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

	// The first update is always done
	EXPECT_EQ ( handler->resultVector.size ( ), 1 );
202
203
204
205
206
207
208
209
210
}

TEST_F ( LocalSchedulerTest, correctNumberOfResults )
{
	for ( auto i = 0; i<2;++i)
	{
		for (auto j = 0; j<3;++j)
		{
			auto update = std::make_unique<CUpdateScene> ( j );
211
			update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
212
											std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
213
			scheduler->PushUpdate ( std::move ( update ) );
214
215
216
		}

		auto update = std::make_unique<CUpdateScene> ( 42 );
217
		update->SetSourceReceiverPair ( std::make_unique<C3DObject> ( VistaVector3D { 1,1,1 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::source, 1 ),
218
										std::make_unique<C3DObject> ( VistaVector3D { 3,6,2 }, VistaQuaternion { 1,0,0,0 }, C3DObject::Type::receiver, 1 ) );
219
		scheduler->PushUpdate ( std::move ( update ) );
220
221
222
223
224
225
226
227
228
229

		// We have to wait a bit for the thread inside the controller to do its thing. And the second bunch is not replaced by filterReplace.
		VistaTimeUtils::Sleep ( 250 );
	}
	
	// We have to wait a bit for the thread inside the controller to do its thing.
	VistaTimeUtils::Sleep ( 250 );

	// The first update is always done
	EXPECT_EQ ( handler->resultVector.size ( ), 5 );
230
}