|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <aws/core/utils/ResourceManager.h> |
| 7 | +#include <aws/testing/AwsCppSdkGTestSuite.h> |
| 8 | + |
| 9 | +#include <future> |
| 10 | +#include <random> |
| 11 | + |
| 12 | +using namespace Aws::Utils; |
| 13 | + |
| 14 | +class ExclusiveOwnershipResourceManagerTest : public Aws::Testing::AwsCppSdkGTestSuite {}; |
| 15 | + |
| 16 | +TEST_F(ExclusiveOwnershipResourceManagerTest, TestAcquire) { |
| 17 | + class TestResource { |
| 18 | + public: |
| 19 | + TestResource(int startVal) : m_value(startVal) {} |
| 20 | + |
| 21 | + int GetValue() { return m_value; } |
| 22 | + |
| 23 | + void DoSomething() { |
| 24 | + std::unique_lock<std::mutex> lock(m_lock, std::defer_lock); |
| 25 | + |
| 26 | + if (!lock.try_lock()) { |
| 27 | + m_fail = true; // concurrent access detected |
| 28 | + } |
| 29 | + m_value += 1; |
| 30 | + } |
| 31 | + |
| 32 | + bool Fail() { return m_fail; } |
| 33 | + |
| 34 | + protected: |
| 35 | + int m_value = 0; |
| 36 | + std::mutex m_lock; |
| 37 | + bool m_fail = false; |
| 38 | + }; |
| 39 | + |
| 40 | + ExclusiveOwnershipResourceManager<TestResource*> resourceManagerInTest; |
| 41 | + |
| 42 | + ASSERT_FALSE(resourceManagerInTest.HasResourcesAvailable()); |
| 43 | + ASSERT_EQ(nullptr, resourceManagerInTest.TryAcquire(1l)); |
| 44 | + |
| 45 | + static const size_t RESOURCES = 6; |
| 46 | + TestResource* resources[RESOURCES] = {}; |
| 47 | + for (int i = 0; i < (int)RESOURCES; ++i) { |
| 48 | + resources[i] = new TestResource(i); |
| 49 | + resourceManagerInTest.Release(resources[i]); |
| 50 | + } |
| 51 | + |
| 52 | + static const size_t THREADS = 8; |
| 53 | + Aws::Vector<std::future<bool>> futures(THREADS); |
| 54 | + |
| 55 | + auto useResourceFn = [](ExclusiveOwnershipResourceManager<TestResource*>& resourceManager) -> bool { |
| 56 | + std::random_device rd; |
| 57 | + std::mt19937_64 randGen(rd()); |
| 58 | + std::uniform_int_distribution<int64_t> randDist(0, 100); |
| 59 | + |
| 60 | + for (size_t j = 0; j < 50; ++j) { |
| 61 | + TestResource* resource = nullptr; |
| 62 | + while (!resource) { |
| 63 | + resource = resourceManager.TryAcquire(randDist(randGen)); |
| 64 | + } |
| 65 | + // "do something" for rand ms |
| 66 | + int64_t sleepFor = randDist(randGen); |
| 67 | + std::this_thread::sleep_for(std::chrono::milliseconds(sleepFor)); |
| 68 | + const int before = resource->GetValue(); |
| 69 | + resource->DoSomething(); |
| 70 | + EXPECT_NE(before, resource->GetValue()); |
| 71 | + resourceManager.Release(resource); |
| 72 | + } |
| 73 | + return true; |
| 74 | + }; |
| 75 | + |
| 76 | + for (size_t i = 0; i < THREADS; ++i) { |
| 77 | + futures[i] = std::async([&resourceManagerInTest, &useResourceFn]() -> bool { return useResourceFn(resourceManagerInTest); }); |
| 78 | + } |
| 79 | + |
| 80 | + for (size_t i = 0; i < THREADS; ++i) { |
| 81 | + bool batchResult = futures[i].get(); |
| 82 | + ASSERT_TRUE(batchResult); |
| 83 | + } |
| 84 | + |
| 85 | + // single thread test |
| 86 | + Aws::Vector<TestResource*> inUse; |
| 87 | + for (int i = 0; i < (int)RESOURCES; ++i) { |
| 88 | + TestResource* resource = resourceManagerInTest.TryAcquire(0l); |
| 89 | + ASSERT_TRUE(resource); |
| 90 | + const int before = resource->GetValue(); |
| 91 | + resource->DoSomething(); |
| 92 | + EXPECT_NE(before, resource->GetValue()); |
| 93 | + inUse.push_back(resource); |
| 94 | + } |
| 95 | + for (auto resource : inUse) { |
| 96 | + resourceManagerInTest.Release(resource); |
| 97 | + } |
| 98 | + |
| 99 | + Aws::Vector<TestResource*> released = resourceManagerInTest.ShutdownAndWait(RESOURCES); |
| 100 | + ASSERT_EQ(RESOURCES, released.size()); |
| 101 | + |
| 102 | + for (int i = 0; i < (int)RESOURCES; ++i) { |
| 103 | + ASSERT_FALSE(resources[i]->Fail()); |
| 104 | + ASSERT_NE(i, resources[i]->GetValue()); |
| 105 | + delete resources[i]; |
| 106 | + } |
| 107 | +} |
0 commit comments