|
| 1 | +#include "stdafx.h" |
| 2 | +#include <memory> |
| 3 | +#include "../../../src/http/common/connection_pool_helpers.h" |
| 4 | + |
| 5 | +using namespace web::http::client::details; |
| 6 | + |
| 7 | +SUITE(connection_pooling) { |
| 8 | + TEST(empty_returns_nullptr) { |
| 9 | + connection_pool_stack<int> connectionStack; |
| 10 | + VERIFY_ARE_EQUAL(connectionStack.try_acquire(), std::shared_ptr<int>{}); |
| 11 | + } |
| 12 | + |
| 13 | + static int noisyCount = 0; |
| 14 | + struct noisy { |
| 15 | + noisy() = delete; |
| 16 | + noisy(int) { ++noisyCount; } |
| 17 | + noisy(const noisy&) = delete; |
| 18 | + noisy(noisy&&) { ++noisyCount; } |
| 19 | + noisy& operator=(const noisy&) = delete; |
| 20 | + noisy& operator=(noisy&&) = delete; |
| 21 | + ~noisy() { --noisyCount; } |
| 22 | + }; |
| 23 | + |
| 24 | + TEST(cycled_connections_survive) { |
| 25 | + connection_pool_stack<noisy> connectionStack; |
| 26 | + VERIFY_ARE_EQUAL(0, noisyCount); |
| 27 | + connectionStack.release(std::make_shared<noisy>(42)); |
| 28 | + connectionStack.release(std::make_shared<noisy>(42)); |
| 29 | + connectionStack.release(std::make_shared<noisy>(42)); |
| 30 | + VERIFY_ARE_EQUAL(3, noisyCount); |
| 31 | + VERIFY_IS_TRUE(connectionStack.free_stale_connections()); |
| 32 | + auto tmp = connectionStack.try_acquire(); |
| 33 | + VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy>{}); |
| 34 | + connectionStack.release(std::move(tmp)); |
| 35 | + VERIFY_ARE_EQUAL(tmp, std::shared_ptr<noisy>{}); |
| 36 | + tmp = connectionStack.try_acquire(); |
| 37 | + VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy>{}); |
| 38 | + connectionStack.release(std::move(tmp)); |
| 39 | + VERIFY_IS_TRUE(connectionStack.free_stale_connections()); |
| 40 | + VERIFY_ARE_EQUAL(1, noisyCount); |
| 41 | + VERIFY_IS_FALSE(connectionStack.free_stale_connections()); |
| 42 | + VERIFY_ARE_EQUAL(0, noisyCount); |
| 43 | + VERIFY_IS_FALSE(connectionStack.free_stale_connections()); |
| 44 | + } |
| 45 | +}; |
0 commit comments