|
| 1 | +/** |
| 2 | + * Copyright (C) 2018-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + * |
| 17 | + * As a special exception, the copyright holders give permission to link the |
| 18 | + * code of portions of this program with the OpenSSL library under certain |
| 19 | + * conditions as described in each individual source file and distribute |
| 20 | + * linked combinations including the program with the OpenSSL library. You |
| 21 | + * must comply with the Server Side Public License in all respects for |
| 22 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 23 | + * with this exception, you may extend this exception to your version of the |
| 24 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 25 | + * delete this exception statement from your version. If you delete this |
| 26 | + * exception statement from all source files in the program, then also delete |
| 27 | + * it in the license file. |
| 28 | + */ |
| 29 | + |
| 30 | +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest |
| 31 | + |
| 32 | +#include "mongo/platform/basic.h" |
| 33 | + |
| 34 | +#include "mongo/db/operation_id.h" |
| 35 | +#include "mongo/logv2/log.h" |
| 36 | +#include "mongo/platform/mutex.h" |
| 37 | +#include "mongo/unittest/death_test.h" |
| 38 | +#include "mongo/unittest/unittest.h" |
| 39 | + |
| 40 | +namespace mongo { |
| 41 | +namespace { |
| 42 | + |
| 43 | +using UniqueOperationIdRegistryTestHarness = |
| 44 | + UniqueOperationIdRegistry::UniqueOperationIdRegistryTestHarness; |
| 45 | + |
| 46 | +TEST(OperationIdTest, OperationIdIncrementsProperly) { |
| 47 | + auto registry = UniqueOperationIdRegistry::create(); |
| 48 | + auto slot = registry->acquireSlot(); |
| 49 | + ASSERT_EQ(slot.getId(), 1); |
| 50 | + |
| 51 | + { |
| 52 | + auto slot2 = registry->acquireSlot(); |
| 53 | + ASSERT_EQ(slot2.getId(), 2); |
| 54 | + } |
| 55 | + |
| 56 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 57 | + ASSERT(!UniqueOperationIdRegistryTestHarness::isActive(*registry, 2)); |
| 58 | + |
| 59 | + slot = registry->acquireSlot(); |
| 60 | + ASSERT_EQ(slot.getId(), 3); |
| 61 | + |
| 62 | + ASSERT(!UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 63 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 3)); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(OperationIdTest, OperationIdsAreUnique) { |
| 67 | + auto registry = UniqueOperationIdRegistry::create(); |
| 68 | + auto slot = registry->acquireSlot(); |
| 69 | + ASSERT_EQ(slot.getId(), 1); |
| 70 | + |
| 71 | + auto slot2 = registry->acquireSlot(); |
| 72 | + ASSERT_EQ(slot2.getId(), 2); |
| 73 | + |
| 74 | + // If the registry's state points to a currently active operation ID, we will |
| 75 | + // find the next hole and issue that instead. |
| 76 | + UniqueOperationIdRegistryTestHarness::setNextOpId(*registry, 1); |
| 77 | + auto slot3 = registry->acquireSlot(); |
| 78 | + ASSERT_EQ(slot3.getId(), 3); |
| 79 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 80 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 2)); |
| 81 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 3)); |
| 82 | +} |
| 83 | + |
| 84 | +TEST(OperationIdTest, OperationIdsAreMovable) { |
| 85 | + auto registry = UniqueOperationIdRegistry::create(); |
| 86 | + auto slot = registry->acquireSlot(); |
| 87 | + ASSERT_EQ(slot.getId(), 1); |
| 88 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 89 | + |
| 90 | + auto moveSlot(std::move(slot)); |
| 91 | + ASSERT_EQ(moveSlot.getId(), 1); |
| 92 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 93 | + |
| 94 | + auto assignSlot = std::move(moveSlot); |
| 95 | + ASSERT_EQ(assignSlot.getId(), 1); |
| 96 | + ASSERT(UniqueOperationIdRegistryTestHarness::isActive(*registry, 1)); |
| 97 | +} |
| 98 | + |
| 99 | +DEATH_TEST(OperationIdTest, TooManyTransactionsShouldCrash, "invariant") { |
| 100 | + auto registry = UniqueOperationIdRegistry::create(); |
| 101 | + std::vector<OperationIdSlot> slots; |
| 102 | + |
| 103 | + while (true) { |
| 104 | + slots.push_back(registry->acquireSlot()); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace |
| 109 | + |
| 110 | +} // namespace mongo |
0 commit comments