[PATCH] D68273: [clangd] Fix raciness in code completion tests
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 1 07:34:48 PDT 2019
kadircet created this revision.
kadircet added reviewers: sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay.
Herald added a project: clang.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D68273
Files:
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Index: clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -27,6 +27,8 @@
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include <condition_variable>
+#include <mutex>
namespace clang {
namespace clangd {
@@ -1112,8 +1114,10 @@
bool
fuzzyFind(const FuzzyFindRequest &Req,
llvm::function_ref<void(const Symbol &)> Callback) const override {
- std::lock_guard<std::mutex> Lock(Mut);
+ std::unique_lock<std::mutex> Lock(Mut);
Requests.push_back(Req);
+ ReceivedRequest = true;
+ ReceivedRequestCV.notify_one();
return true;
}
@@ -1132,7 +1136,9 @@
size_t estimateMemoryUsage() const override { return 0; }
const std::vector<FuzzyFindRequest> consumeRequests() const {
- std::lock_guard<std::mutex> Lock(Mut);
+ std::unique_lock<std::mutex> Lock(Mut);
+ ReceivedRequestCV.wait(Lock, [this]() { return ReceivedRequest; });
+ ReceivedRequest = false;
auto Reqs = std::move(Requests);
Requests = {};
return Reqs;
@@ -1140,7 +1146,9 @@
private:
// We need a mutex to handle async fuzzy find requests.
+ mutable std::condition_variable ReceivedRequestCV;
mutable std::mutex Mut;
+ mutable bool ReceivedRequest = false;
mutable std::vector<FuzzyFindRequest> Requests;
};
@@ -2098,9 +2106,6 @@
auto CompleteAtPoint = [&](StringRef P) {
cantFail(runCodeComplete(Server, File, Test.point(P), Opts));
- // Sleep for a while to make sure asynchronous call (if applicable) is also
- // triggered before callback is invoked.
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
};
CompleteAtPoint("1");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68273.222611.patch
Type: text/x-patch
Size: 1871 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191001/395ab419/attachment-0001.bin>
More information about the cfe-commits
mailing list