| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | /****************************************************************************
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Copyright (C) 2018 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** This file is part of Qt Creator. | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and The Qt Company. For licensing terms | 
					
						
							|  |  |  | ** and conditions see https://www.qt.io/terms-conditions. For further
 | 
					
						
							|  |  |  | ** information use the contact form at https://www.qt.io/contact-us.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ** GNU General Public License Usage | 
					
						
							|  |  |  | ** Alternatively, this file may be used under the terms of the GNU | 
					
						
							|  |  |  | ** General Public License version 3 as published by the Free Software | 
					
						
							|  |  |  | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
					
						
							|  |  |  | ** included in the packaging of this file. Please review the following | 
					
						
							|  |  |  | ** information to ensure the GNU General Public License requirements will | 
					
						
							|  |  |  | ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
 | 
					
						
							|  |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "googletest.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | #include "mocksymbolindexertaskqueue.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | #include "mockprocessormanager.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | #include "mocksymbolscollector.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | #include <taskscheduler.h>
 | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | #include <QCoreApplication>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | using Task = std::function<void(ClangBackEnd::ProcessorInterface&)>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using ClangBackEnd::ProcessorInterface; | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | using ClangBackEnd::SymbolsCollectorInterface; | 
					
						
							|  |  |  | using ClangBackEnd::SymbolStorageInterface; | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | using NiceMockProcessorManager = NiceMock<MockProcessorManager>; | 
					
						
							|  |  |  | using Scheduler = ClangBackEnd::TaskScheduler<NiceMockProcessorManager, Task>; | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | class TaskScheduler : public testing::Test | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     void SetUp() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |         ON_CALL(mockProcessorManager, unusedProcessor()).WillByDefault(ReturnRef(mockSymbolsCollector)); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  |     void TearDown() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         scheduler.syncTasks(); | 
					
						
							|  |  |  |         QCoreApplication::processEvents(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  |     MockFunction<void()> mock; | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     Task call{[&] (ClangBackEnd::ProcessorInterface&) { mock.Call(); }}; | 
					
						
							|  |  |  |     Task nocall{[&] (ClangBackEnd::ProcessorInterface&) {}}; | 
					
						
							|  |  |  |     NiceMockProcessorManager mockProcessorManager; | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  |     NiceMock<MockSymbolsCollector> mockSymbolsCollector; | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  |     NiceMock<MockSymbolIndexerTaskQueue> mockSymbolIndexerTaskQueue; | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     Scheduler scheduler{mockProcessorManager, | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  |                 mockSymbolIndexerTaskQueue, | 
					
						
							|  |  |  |                 4}; | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     Scheduler deferredScheduler{mockProcessorManager, | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  |                 mockSymbolIndexerTaskQueue, | 
					
						
							|  |  |  |                 4, | 
					
						
							|  |  |  |                 std::launch::deferred}; | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, AddTasks) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     deferredScheduler.addTasks({nocall}); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     ASSERT_THAT(deferredScheduler.futures(), SizeIs(1)); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, AddTasksCallsFunction) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     EXPECT_CALL(mock, Call()).Times(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scheduler.addTasks({call, call}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, FreeSlots) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     deferredScheduler.addTasks({nocall, nocall}); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     auto count = deferredScheduler.freeSlots(); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(count, 2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, ReturnZeroFreeSlotsIfMoreCallsThanCores) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     deferredScheduler.addTasks({nocall, nocall, nocall, nocall, nocall, nocall}); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     auto count = deferredScheduler.freeSlots(); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(count, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, FreeSlotsAfterFinishing) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     scheduler.addTasks({nocall, nocall}); | 
					
						
							|  |  |  |     scheduler.syncTasks(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto count = scheduler.freeSlots(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(count, 4); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, NoFuturesAfterFreeSlots) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     scheduler.addTasks({nocall, nocall}); | 
					
						
							|  |  |  |     scheduler.syncTasks(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     scheduler.freeSlots(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_THAT(scheduler.futures(), IsEmpty()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, FreeSlotsCallsCleanupMethodsAfterTheWorkIsDone) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     scheduler.addTasks({nocall, nocall}); | 
					
						
							|  |  |  |     scheduler.syncTasks(); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  |     InSequence s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     EXPECT_CALL(mockSymbolsCollector, doInMainThreadAfterFinished()); | 
					
						
							|  |  |  |     EXPECT_CALL(mockSymbolsCollector, setIsUsed(false)); | 
					
						
							|  |  |  |     EXPECT_CALL(mockSymbolsCollector, clear()); | 
					
						
							|  |  |  |     EXPECT_CALL(mockSymbolsCollector, doInMainThreadAfterFinished()); | 
					
						
							|  |  |  |     EXPECT_CALL(mockSymbolsCollector, setIsUsed(false)); | 
					
						
							|  |  |  |     EXPECT_CALL(mockSymbolsCollector, clear()); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scheduler.freeSlots(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, AddTaskCallSymbolsCollectorManagerUnusedSymbolsCollector) | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     EXPECT_CALL(mockProcessorManager, unusedProcessor()).Times(2); | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scheduler.addTasks({nocall, nocall}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  | TEST_F(TaskScheduler, CallProcessTasksInQueueAfterFinishedTasks) | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  |     InSequence s; | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  |     EXPECT_CALL(mock, Call()); | 
					
						
							| 
									
										
										
										
											2018-09-11 17:02:45 +02:00
										 |  |  |     EXPECT_CALL(mockSymbolIndexerTaskQueue, processEntries()); | 
					
						
							| 
									
										
										
										
											2018-08-28 12:08:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     scheduler.addTasks({call}); | 
					
						
							|  |  |  |     scheduler.syncTasks(); | 
					
						
							| 
									
										
										
										
											2018-08-27 15:54:08 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-08-22 21:13:05 +02:00
										 |  |  | } |