File: connection_pool_tests.cpp

package info (click to toggle)
cpprest 2.10.19-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,916 kB
  • sloc: cpp: 71,086; sh: 275; makefile: 170; javascript: 147
file content (50 lines) | stat: -rw-r--r-- 1,783 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"

#include "../../../src/http/common/connection_pool_helpers.h"
#include <memory>

using namespace web::http::client::details;

SUITE(connection_pooling)
{
    TEST(empty_returns_nullptr)
    {
        connection_pool_stack<int> connectionStack;
        VERIFY_ARE_EQUAL(connectionStack.try_acquire(), std::shared_ptr<int> {});
    }

    static int noisyCount = 0;
    struct noisy
    {
        noisy() = delete;
        noisy(int) { ++noisyCount; }
        noisy(const noisy&) = delete;
        noisy(noisy&&) { ++noisyCount; }
        noisy& operator=(const noisy&) = delete;
        noisy& operator=(noisy&&) = delete;
        ~noisy() { --noisyCount; }
    };

    TEST(cycled_connections_survive)
    {
        connection_pool_stack<noisy> connectionStack;
        VERIFY_ARE_EQUAL(0, noisyCount);
        connectionStack.release(std::make_shared<noisy>(42));
        connectionStack.release(std::make_shared<noisy>(42));
        connectionStack.release(std::make_shared<noisy>(42));
        VERIFY_ARE_EQUAL(3, noisyCount);
        VERIFY_IS_TRUE(connectionStack.free_stale_connections());
        auto tmp = connectionStack.try_acquire();
        VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy> {});
        connectionStack.release(std::move(tmp));
        VERIFY_ARE_EQUAL(tmp, std::shared_ptr<noisy> {});
        tmp = connectionStack.try_acquire();
        VERIFY_ARE_NOT_EQUAL(tmp, std::shared_ptr<noisy> {});
        connectionStack.release(std::move(tmp));
        VERIFY_IS_TRUE(connectionStack.free_stale_connections());
        VERIFY_ARE_EQUAL(1, noisyCount);
        VERIFY_IS_FALSE(connectionStack.free_stale_connections());
        VERIFY_ARE_EQUAL(0, noisyCount);
        VERIFY_IS_FALSE(connectionStack.free_stale_connections());
    }
};