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-12-04 19:03:48 +01:00
|
|
|
using ClangBackEnd::SlotUsage;
|
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-09-27 17:52:44 +02:00
|
|
|
progressCounter.addTotal(100);
|
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-27 17:52:44 +02:00
|
|
|
NiceMock<MockFunction<void(int, int)>> mockSetProgressCallback;
|
|
|
|
ClangBackEnd::ProgressCounter progressCounter{mockSetProgressCallback.AsStdFunction()};
|
2018-09-11 17:02:45 +02:00
|
|
|
Scheduler scheduler{mockProcessorManager,
|
2018-12-17 12:06:57 +01:00
|
|
|
mockSymbolIndexerTaskQueue,
|
|
|
|
progressCounter,
|
|
|
|
4,
|
|
|
|
ClangBackEnd::CallDoInMainThreadAfterFinished::Yes};
|
2018-09-11 17:02:45 +02:00
|
|
|
Scheduler deferredScheduler{mockProcessorManager,
|
2018-12-17 12:06:57 +01:00
|
|
|
mockSymbolIndexerTaskQueue,
|
|
|
|
progressCounter,
|
|
|
|
4,
|
|
|
|
ClangBackEnd::CallDoInMainThreadAfterFinished::Yes,
|
|
|
|
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-12-04 19:03:48 +01:00
|
|
|
auto slotUsage = deferredScheduler.slotUsage();
|
2018-08-22 21:13:05 +02:00
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
ASSERT_THAT(slotUsage, AllOf(Field(&SlotUsage::free, 2), Field(&SlotUsage::used, 2)));
|
2018-08-22 21:13:05 +02:00
|
|
|
}
|
|
|
|
|
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-12-04 19:03:48 +01:00
|
|
|
auto slotUsage = deferredScheduler.slotUsage();
|
2018-08-22 21:13:05 +02:00
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
ASSERT_THAT(slotUsage, AllOf(Field(&SlotUsage::free, 0), Field(&SlotUsage::used, 6)));
|
2018-08-22 21:13:05 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
auto slotUsage = scheduler.slotUsage();
|
2018-08-22 21:13:05 +02:00
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
ASSERT_THAT(slotUsage, AllOf(Field(&SlotUsage::free, 4), Field(&SlotUsage::used, 0)));
|
2018-08-22 21:13:05 +02:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
scheduler.slotUsage();
|
2018-08-22 21:13:05 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
scheduler.slotUsage();
|
2018-08-28 12:08:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 12:06:57 +01:00
|
|
|
TEST_F(TaskScheduler, FreeSlotsDoNotCallsDoInMainThreadAfterFinishedAfterTheWorkIsDoneIfForbidden)
|
|
|
|
{
|
|
|
|
Scheduler scheduler{mockProcessorManager,
|
|
|
|
mockSymbolIndexerTaskQueue,
|
|
|
|
progressCounter,
|
|
|
|
4,
|
|
|
|
ClangBackEnd::CallDoInMainThreadAfterFinished::No};
|
|
|
|
scheduler.addTasks({nocall, nocall});
|
|
|
|
scheduler.syncTasks();
|
|
|
|
InSequence s;
|
|
|
|
|
|
|
|
EXPECT_CALL(mockSymbolsCollector, doInMainThreadAfterFinished()).Times(0);
|
|
|
|
|
|
|
|
scheduler.slotUsage();
|
|
|
|
scheduler.syncTasks();
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:52:44 +02:00
|
|
|
TEST_F(TaskScheduler, FreeSlotsCallsProgressMethodsAfterTheWorkIsDone)
|
|
|
|
{
|
|
|
|
scheduler.addTasks({nocall, nocall});
|
|
|
|
scheduler.syncTasks();
|
|
|
|
InSequence s;
|
|
|
|
|
|
|
|
EXPECT_CALL(mockSetProgressCallback, Call(2, 100));
|
|
|
|
|
2018-12-04 19:03:48 +01:00
|
|
|
scheduler.slotUsage();
|
2018-09-27 17:52:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|