1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
//===- unittests/Basic/LaneBasedExecutionQueueTest.cpp --------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Basic/FileSystem.h"
#include "llbuild/Basic/ExecutionQueue.h"
#include "../BuildSystem/TempDir.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "gtest/gtest.h"
#include <atomic>
#include <condition_variable>
#include <ctime>
#include <future>
#include <mutex>
using namespace llbuild;
using namespace llbuild::basic;
namespace {
class DummyDelegate : public ExecutionQueueDelegate {
public:
DummyDelegate() {}
virtual void queueJobStarted(JobDescriptor*) override {}
virtual void queueJobFinished(JobDescriptor*) override {}
virtual void processStarted(ProcessContext*, ProcessHandle, llbuild_pid_t) override {}
virtual void processHadError(ProcessContext*, ProcessHandle,
const Twine& message) override {}
virtual void processHadOutput(ProcessContext*, ProcessHandle,
StringRef data) override {}
virtual void processFinished(ProcessContext*, ProcessHandle,
const ProcessResult& result) override {}
};
class DummyCommand : public JobDescriptor {
public:
DummyCommand() {}
virtual StringRef getOrdinalName() const { return StringRef(""); }
virtual void getShortDescription(SmallVectorImpl<char> &result) const {}
virtual void getVerboseDescription(SmallVectorImpl<char> &result) const {}
};
TEST(LaneBasedExecutionQueueTest, basic) {
DummyDelegate delegate;
std::unique_ptr<FileSystem> fs = createLocalFileSystem();
TmpDir tempDir{"LaneBasedExecutionQueueTest"};
std::string outputFile = tempDir.str() + "/yes-output.txt";
auto queue = std::unique_ptr<ExecutionQueue>(
createLaneBasedExecutionQueue(delegate, 2,
SchedulerAlgorithm::NamePriority,
getDefaultQualityOfService(),
/*environment=*/nullptr));
auto fn = [&outputFile, &queue](QueueJobContext* context) {
queue->executeShellCommand(context, "yes >" + outputFile);
};
DummyCommand dummyCommand;
queue->addJob(QueueJob(&dummyCommand, fn));
// Busy wait until `outputFile` appears which indicates that `yes` is
// running.
time_t start = ::time(NULL);
while (fs->getFileInfo(outputFile).isMissing()) {
if (::time(NULL) > start + 5) {
// We can't fail gracefully because the `LaneBasedExecutionQueue` will
// always wait for spawned processes to exit
abort();
}
}
queue->cancelAllJobs();
queue.reset();
}
TEST(LaneBasedExecutionQueueTest, workingDirectory) {
DummyDelegate delegate;
std::unique_ptr<FileSystem> fs = createLocalFileSystem();
TmpDir tempDir{"LaneBasedExecutionQueueTest"};
std::string outputFile = tempDir.str() + "/yes-output.txt";
auto queue = std::unique_ptr<ExecutionQueue>(
createLaneBasedExecutionQueue(delegate, 2,
SchedulerAlgorithm::NamePriority,
getDefaultQualityOfService(),
/*environment=*/nullptr));
auto fn = [&tempDir, &queue](QueueJobContext* context) {
std::string yescmd = "yes >yes-output.txt";
std::vector<StringRef> commandLine(
{ DefaultShellPath, "-c", yescmd.c_str() });
std::promise<ProcessStatus> p;
auto result = p.get_future();
queue->executeProcess(context, commandLine, {}, {true, false, tempDir.str()},
{[&p](ProcessResult result) mutable {
p.set_value(result.status);
}});
result.get();
};
DummyCommand dummyCommand;
queue->addJob(QueueJob(&dummyCommand, fn));
// Busy wait until `outputFile` appears which indicates that `yes` is
// running.
time_t start = ::time(NULL);
while (fs->getFileInfo(outputFile).isMissing()) {
if (::time(NULL) > start + 5) {
// We can't fail gracefully because the `LaneBasedExecutionQueue` will
// always wait for spawned processes to exit
abort();
}
}
queue->cancelAllJobs();
queue.reset();
}
TEST(LaneBasedExecutionQueueTest, exhaustsQueueAfterCancellation) {
DummyDelegate delegate;
std::mutex queueMutex;
auto queue = std::unique_ptr<ExecutionQueue>(
createLaneBasedExecutionQueue(delegate, 1,
SchedulerAlgorithm::NamePriority,
getDefaultQualityOfService(),
/*environment=*/nullptr));
bool buildStarted { false };
std::condition_variable buildStartedCondition;
std::mutex buildStartedMutex;
std::atomic<int> executions { 0 };
auto fn = [&buildStarted, &buildStartedCondition, &buildStartedMutex,
&executions, &queueMutex, &queue](QueueJobContext* context) {
executions++;
{
std::lock_guard<std::mutex> lock(queueMutex);
if (queue) { queue->cancelAllJobs(); }
}
std::unique_lock<std::mutex> lock(buildStartedMutex);
buildStarted = true;
buildStartedCondition.notify_all();
};
DummyCommand dummyCommand1;
DummyCommand dummyCommand2;
{
std::lock_guard<std::mutex> lock(queueMutex);
queue->addJob(QueueJob(&dummyCommand1, fn));
queue->addJob(QueueJob(&dummyCommand2, fn));
}
{
std::unique_lock<std::mutex> lock(buildStartedMutex);
while (!buildStarted) {
buildStartedCondition.wait(lock);
}
}
{
std::lock_guard<std::mutex> lock(queueMutex);
queue.reset();
}
// Busy wait until our executions are done, but also have a timeout in case they never finish
time_t start = ::time(NULL);
while (executions < 2) {
if (::time(NULL) > start + 5) {
break;
}
}
EXPECT_EQ(executions, 2);
}
}
|