multi_resource_lock_ut.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "multi_resource_lock.h"
  2. #include <util/generic/xrange.h>
  3. #include <library/cpp/threading/future/async.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. namespace NYql {
  6. using namespace NThreading;
  7. Y_UNIT_TEST_SUITE(TMultiResourceLock) {
  8. Y_UNIT_TEST(ManyResources) {
  9. TMultiResourceLock multiLock;
  10. const int workersCount = 3;
  11. TVector<TVector<int>> workersData;
  12. workersData.resize(workersCount);
  13. TAdaptiveThreadPool queue;
  14. queue.Start(0);
  15. TVector<NThreading::TFuture<void>> workers;
  16. workers.reserve(workersCount);
  17. TManualEvent startEvent;
  18. for (int i = 0; i < workersCount; ++i) {
  19. TString resourceId = ToString(i);
  20. TVector<int>& data = workersData.at(i);
  21. NThreading::TFuture<void> f = NThreading::Async([&, resourceId]() {
  22. startEvent.Wait();
  23. for (int j = 0; j < 1000; ++j) {
  24. const auto& l = multiLock.Acquire(resourceId);
  25. Y_UNUSED(l);
  26. data.push_back(j);
  27. }
  28. }, queue);
  29. workers.push_back(std::move(f));
  30. }
  31. startEvent.Signal();
  32. NThreading::TFuture<void> all = NThreading::WaitExceptionOrAll(workers);
  33. all.GetValueSync();
  34. queue.Stop();
  35. // analyze workersData:
  36. auto range0_999 = xrange(0, 1000);
  37. for (auto& w : workersData) {
  38. UNIT_ASSERT_VALUES_EQUAL(w.size(), 1000);
  39. UNIT_ASSERT(std::equal(range0_999.begin(), range0_999.end(), w.begin()));
  40. }
  41. }
  42. }
  43. }