|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +#include "block_blob_client_test.hpp" |
| 5 | + |
| 6 | +namespace Azure { namespace Storage { namespace Test { |
| 7 | + |
| 8 | + // If connection is reused, the requests with the same connection should hit the same sever. So |
| 9 | + // this test verifies whether a series of requests hit the same server. |
| 10 | + TEST_F(BlockBlobClientTest, IsConnectionReused_LIVEONLY_) |
| 11 | + { |
| 12 | + const std::string containerName = LowercaseRandomString(); |
| 13 | + const std::string blobName = LowercaseRandomString(); |
| 14 | + |
| 15 | + auto containerClient1 = GetBlobContainerClientForTest(containerName + "1"); |
| 16 | + auto containerClient2 = GetBlobContainerClientForTest(containerName + "2"); |
| 17 | + containerClient1.Create(); |
| 18 | + containerClient2.Create(); |
| 19 | + |
| 20 | + auto buffer = RandomBuffer(100); |
| 21 | + Core::IO::MemoryBodyStream bodyStream(buffer.data(), buffer.size()); |
| 22 | + |
| 23 | + std::vector<Azure::Storage::Blobs::BlockBlobClient> blobClients; |
| 24 | + for (int i = 0; i < 5; ++i) |
| 25 | + { |
| 26 | + blobClients.push_back(containerClient1.GetBlockBlobClient(blobName + std::to_string(i))); |
| 27 | + blobClients.push_back(containerClient2.GetBlockBlobClient(blobName + std::to_string(i))); |
| 28 | + } |
| 29 | + std::unordered_set<std::string> distinctServers; |
| 30 | + size_t totalHitCount = 0; |
| 31 | + |
| 32 | + auto updateDistinctServers = [&](const Azure::Core::CaseInsensitiveMap& headers) { |
| 33 | + // The third part of a storage request id means the server node id. |
| 34 | + // For Example, RequestId:3bcf963b-601e-0054-1f40-910c39000000, '0054' is the server node |
| 35 | + // which served this request. |
| 36 | + std::string serverId = headers.find("x-ms-request-id")->second.substr(14, 4); |
| 37 | + distinctServers.insert(serverId); |
| 38 | + }; |
| 39 | + |
| 40 | + for (auto& blobClient : blobClients) |
| 41 | + { |
| 42 | + auto uploadResult = blobClient.Upload(bodyStream); |
| 43 | + updateDistinctServers(uploadResult.RawResponse->GetHeaders()); |
| 44 | + ++totalHitCount; |
| 45 | + auto downloadResult = blobClient.Download(); |
| 46 | + ReadBodyStream(downloadResult.Value.BodyStream); |
| 47 | + updateDistinctServers(downloadResult.RawResponse->GetHeaders()); |
| 48 | + ++totalHitCount; |
| 49 | + auto deleteResult = blobClient.Delete(); |
| 50 | + updateDistinctServers(deleteResult.RawResponse->GetHeaders()); |
| 51 | + ++totalHitCount; |
| 52 | + } |
| 53 | + |
| 54 | + size_t distinctServersLessThan = totalHitCount / 5; |
| 55 | + EXPECT_TRUE(distinctServers.size() < distinctServersLessThan); |
| 56 | + } |
| 57 | +}}} // namespace Azure::Storage::Test |
0 commit comments