summaryrefslogtreecommitdiffstats
path: root/test-channel.cc
blob: 75c5a598f27b475d6fa91a3877eadf520e54e650 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef BOOST_TEST_DYN_LINK
#define BOOST_TEST_DYN_LINK
#endif

#define BOOST_TEST_NO_MAIN

#include <boost/test/unit_test.hpp>

#include "channel.hh"

struct MyObject
{
  uint64_t a{0};
};

BOOST_AUTO_TEST_SUITE(test_channel)

BOOST_AUTO_TEST_CASE(test_object_queue)
{
  auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>();

  BOOST_CHECK(receiver.getDescriptor() != -1);
  BOOST_CHECK_EQUAL(receiver.isClosed(), false);

  auto got = receiver.receive();
  BOOST_CHECK(!got);

  auto obj = std::make_unique<MyObject>();
  obj->a = 42U;
  BOOST_CHECK_EQUAL(sender.send(std::move(obj)), true);
  BOOST_CHECK(!obj);
  got = receiver.receive();
  BOOST_CHECK(got != std::nullopt && *got);
  BOOST_CHECK_EQUAL((*got)->a, 42U);
}

BOOST_AUTO_TEST_CASE(test_object_queue_full)
{
  auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>();

  {
    auto got = receiver.receive();
    BOOST_CHECK(!got);
  }

  /* add objects to the queue until it becomes full */
  bool blocked = false;
  size_t queued = 0;
  while (!blocked) {
    auto obj = std::make_unique<MyObject>();
    obj->a = 42U;
    blocked = !sender.send(std::move(obj));
    if (blocked) {
      BOOST_CHECK(obj);
    }
    else {
      BOOST_CHECK(!obj);
      ++queued;
    }
  }

  BOOST_CHECK_GT(queued, 1U);

  /* clear the queue */
  blocked = false;
  size_t received = 0;
  while (!blocked) {
    auto got = receiver.receive();
    if (got) {
      ++received;
    }
    else {
      blocked = true;
    }
  }

  BOOST_CHECK_EQUAL(queued, received);

  /* we should be able to write again */
  auto obj = std::make_unique<MyObject>();
  obj->a = 42U;
  BOOST_CHECK(sender.send(std::move(obj)));
  /* and to get it */
  {
    auto got = receiver.receive();
    BOOST_CHECK(got);
  }
}

BOOST_AUTO_TEST_CASE(test_object_queue_throw_on_eof)
{
  auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>();
  sender.close();
  BOOST_CHECK_THROW(receiver.receive(), std::runtime_error);
  BOOST_CHECK_EQUAL(receiver.isClosed(), true);
}

BOOST_AUTO_TEST_CASE(test_object_queue_do_not_throw_on_eof)
{
  auto [sender, receiver] = pdns::channel::createObjectQueue<MyObject>(pdns::channel::SenderBlockingMode::SenderNonBlocking, pdns::channel::ReceiverBlockingMode::ReceiverNonBlocking, 0U, false);
  sender.close();
  auto got = receiver.receive();
  BOOST_CHECK(got == std::nullopt);
  BOOST_CHECK_EQUAL(receiver.isClosed(), true);
}

BOOST_AUTO_TEST_CASE(test_notification_queue_full)
{
  auto [notifier, waiter] = pdns::channel::createNotificationQueue();

  BOOST_CHECK(waiter.getDescriptor() != -1);
  BOOST_CHECK_EQUAL(waiter.isClosed(), false);
  waiter.clear();

  /* add notifications until the queue becomes full */
  bool blocked = false;
  while (!blocked) {
    blocked = notifier.notify();
  }

  /* clear the queue */
  waiter.clear();

  /* we should be able to write again */
  BOOST_CHECK(notifier.notify());
}

BOOST_AUTO_TEST_CASE(test_notification_queue_throw_on_eof)
{
  auto [notifier, waiter] = pdns::channel::createNotificationQueue();

  BOOST_CHECK(waiter.getDescriptor() != -1);
  BOOST_CHECK_EQUAL(waiter.isClosed(), false);

  BOOST_CHECK_EQUAL(notifier.notify(), true);
  waiter.clear();

  notifier = pdns::channel::Notifier();
  BOOST_CHECK_THROW(waiter.clear(), std::runtime_error);
}

BOOST_AUTO_TEST_CASE(test_notification_queue_do_not_throw_on_eof)
{
  auto [notifier, waiter] = pdns::channel::createNotificationQueue(true, 0, false);

  BOOST_CHECK(waiter.getDescriptor() != -1);
  BOOST_CHECK_EQUAL(waiter.isClosed(), false);

  BOOST_CHECK_EQUAL(notifier.notify(), true);
  waiter.clear();

  notifier = pdns::channel::Notifier();
  waiter.clear();
  BOOST_CHECK_EQUAL(waiter.isClosed(), true);
}

BOOST_AUTO_TEST_SUITE_END()