<commit_before>#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 pending notifications.
  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))<commit_msg>Clearer comment.<commit_after>#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))
{'fraction_non_alphanumeric': 0.14095180357684148, 'fraction_numerical': 0.004849954531676266, 'mean_word_length': 4.602716468590832, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4603330', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1082, 'n_words': 257}