summaryrefslogtreecommitdiffstats
path: root/test-connectionmanagement_hh.cc
blob: 83205183c329dbfeb4adc37d83eb52b6333a9762 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_NO_MAIN

#include <boost/test/unit_test.hpp>

#include "connection-management.hh"

BOOST_AUTO_TEST_SUITE(connectionmanagement_hh)

BOOST_AUTO_TEST_CASE(test_ConnectionManagementEnabled) {
  size_t maxConns = 10;

  ConcurrentConnectionManager manager(maxConns);

  for (size_t idx = 0; idx < maxConns; idx++) {
    BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  }

  /* we are full */
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  manager.releaseConnection();
  /* we can register one additional connection now that we released one */
  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  /* but not two */
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  /* raise the number of slots */
  maxConns = 12;
  manager.setMaxConcurrentConnections(maxConns);
  BOOST_CHECK_EQUAL(manager.getMaxConcurrentConnections(), maxConns);
  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  /* release everything */
  for (size_t idx = 0; idx < maxConns; idx++) {
    manager.releaseConnection();
  }

  /* decrease the number of slots */
  maxConns = 2;
  manager.setMaxConcurrentConnections(maxConns);
  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  /* decrease the number of slots with some connections still registered */
  maxConns = 1;
  manager.setMaxConcurrentConnections(maxConns);

  BOOST_CHECK_EQUAL(manager.registerConnection(), false);
  for (size_t idx = 0; idx < 2; idx++) {
    manager.releaseConnection();
  }

  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);
}

BOOST_AUTO_TEST_CASE(test_ConnectionManagementDisabledThenEnabled) {
  /* 0 means no limit */
  size_t maxConns = 0;
  ConcurrentConnectionManager manager(maxConns);

  for (size_t idx = 0; idx < 10; idx++) {
    BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  }

  /* set a limit to 5 connections */
  maxConns = 5;
  manager.setMaxConcurrentConnections(maxConns);
  /* we can no longer register new sessions */
  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  /* release all of them */
  for (size_t idx = 0; idx < 10; idx++) {
    manager.releaseConnection();
  }

  /* register as many as we now can */
  for (size_t idx = 0; idx < maxConns; idx++) {
    BOOST_CHECK_EQUAL(manager.registerConnection(), true);
  }

  BOOST_CHECK_EQUAL(manager.registerConnection(), false);

  manager.releaseConnection();

  BOOST_CHECK_EQUAL(manager.registerConnection(), true);
}

BOOST_AUTO_TEST_SUITE_END()