|
| 1 | +/* |
| 2 | + * Copyright 2022-Present Couchbase, Inc. |
| 3 | + * |
| 4 | + * Use of this software is governed by the Business Source License included |
| 5 | + * in the file licenses/BSL-Couchbase.txt. As of the Change Date specified |
| 6 | + * in that file, in accordance with the Business Source License, use of this |
| 7 | + * software will be governed by the Apache License, Version 2.0, included in |
| 8 | + * the file licenses/APL2.txt. |
| 9 | + */ |
| 10 | + |
| 11 | +#include "bucket_destroyer.h" |
| 12 | +#include "buckets.h" |
| 13 | +#include "memcached.h" |
| 14 | +#include "memcached/engine.h" |
| 15 | +#include "engines/nobucket/nobucket_public.h" |
| 16 | +#include "stats.h" |
| 17 | +#include <folly/portability/GTest.h> |
| 18 | + |
| 19 | +class BucketManagerTest : public ::testing::Test, public BucketManager { |
| 20 | +}; |
| 21 | + |
| 22 | +class BucketDestroyerIntrospector { |
| 23 | +public: |
| 24 | + static void setNextLogConnections( |
| 25 | + BucketDestroyer& destroyer, |
| 26 | + std::chrono::steady_clock::time_point tp) { |
| 27 | + destroyer.nextLogConnections = tp; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +// Destroy bucket with no clients. |
| 32 | +TEST_F(BucketManagerTest, DestroyBucketTest) { |
| 33 | + auto& bucket = all_buckets.back(); |
| 34 | + bucket.setEngine(create_no_bucket_instance()); |
| 35 | + BucketDestroyer destroyer(bucket, "", false); |
| 36 | + EXPECT_EQ(cb::engine_errc::success, destroyer.drive()); |
| 37 | +} |
| 38 | + |
| 39 | +// We shouldn't destroy a bucket with clients. |
| 40 | +TEST_F(BucketManagerTest, DestroyBucketWaitsForClients) { |
| 41 | + auto& bucket = all_buckets.back(); |
| 42 | + bucket.setEngine(create_no_bucket_instance()); |
| 43 | + // Add a client and verify that we wait for it. |
| 44 | + bucket.clients++; |
| 45 | + |
| 46 | + using std::chrono::steady_clock; |
| 47 | + using namespace std::chrono_literals; |
| 48 | + BucketDestroyer destroyer(bucket, "", false); |
| 49 | + // Cannot complete the bucket delete as we've got a client. Run twice to |
| 50 | + // make sure the result is the same. |
| 51 | + EXPECT_EQ(cb::engine_errc::would_block, destroyer.drive()); |
| 52 | + EXPECT_EQ(cb::engine_errc::would_block, destroyer.drive()); |
| 53 | + |
| 54 | + // "Move" the connection log time, such that on the next run it has elapsed. |
| 55 | + BucketDestroyerIntrospector::setNextLogConnections( |
| 56 | + destroyer, steady_clock::now() - 1s); |
| 57 | + |
| 58 | + // We should still be unable to delete the bucket. Run twice to make sure |
| 59 | + // the result is the same. |
| 60 | + EXPECT_EQ(cb::engine_errc::would_block, destroyer.drive()); |
| 61 | + EXPECT_EQ(cb::engine_errc::would_block, destroyer.drive()); |
| 62 | + |
| 63 | + // We should succeed in deleting now that clients == 0. |
| 64 | + bucket.clients--; |
| 65 | + EXPECT_EQ(cb::engine_errc::success, destroyer.drive()); |
| 66 | +} |
0 commit comments