File: test_notification.cxx

package info (click to toggle)
libpqxx 4.0.1%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,012 kB
  • ctags: 9,469
  • sloc: sh: 11,289; cpp: 10,801; xml: 1,256; makefile: 287; ansic: 195; python: 159
file content (78 lines) | stat: -rw-r--r-- 1,803 bytes parent folder | download | duplicates (2)
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
#include <test_helpers.hxx>

using namespace PGSTD;
using namespace pqxx;


namespace
{
class TestReceiver : public notification_receiver
{
public:
  string payload;
  int backend_pid;
  bool done;

  TestReceiver(connection_base &c, const string &channel_name) :
    notification_receiver(c, channel_name),
    payload(),
    backend_pid(0)
  {
  }

  virtual void operator()(const string &payload_string, int backend)
  {
    this->payload = payload_string;
    this->backend_pid = backend;
  }
};


void test_receive(
	transaction_base &t,
	const string &channel,
	const char payload[] = NULL)
{
  connection_base &conn(t.conn());

  string SQL = "NOTIFY \"" + channel + "\"";
  if (payload) SQL += ", " + t.quote(payload);

  TestReceiver receiver(t.conn(), channel);

  // Clear out any previously pending notifications that might otherwise
  // confuse the test.
  conn.get_notifs();

  // Notify, and receive.
  t.exec(SQL);
  t.commit();

  int notifs = 0;
  for (int i=0; (i < 10) && !notifs; ++i, pqxx::internal::sleep_seconds(1))
    notifs = conn.get_notifs();

  PQXX_CHECK_EQUAL(notifs, 1, "Got wrong number of notifications.");
  PQXX_CHECK_EQUAL(receiver.backend_pid, conn.backendpid(), "Bad pid.");
  if (payload) PQXX_CHECK_EQUAL(receiver.payload, payload, "Bad payload.");
  else PQXX_CHECK(receiver.payload.empty(), "Unexpected payload.");
}


void test_notification(transaction_base &t)
{
  connection_base &conn(t.conn());
  TestReceiver receiver(conn, "mychannel");
  PQXX_CHECK_EQUAL(receiver.channel(), "mychannel", "Bad channel.");

  test_receive(t, "channel1");

  if (conn.supports(connection_base::cap_notify_payload))
  {
    nontransaction u(conn);
    test_receive(u, "channel2", "payload");
  }
}
} // namespace

PQXX_REGISTER_TEST_T(test_notification, nontransaction)