<commit_before>/**
 * This file is part of the CernVM File System.
 */

#include <gtest/gtest.h>

#include <logging.h>
#include "json_document.h"
#include "receiver/reactor.h"
#include "util/pointer.h"
#include "util/string.h"
#include "util_concurrency.h"

using namespace receiver;  // NOLINT

class MockedReactor : public Reactor {
 public:
  MockedReactor(int fdin, int fdout) : Reactor(fdin, fdout) {}
};

class T_Reactor : public ::testing::Test {
 protected:
  virtual void SetUp() {
    ASSERT_NE(pipe(to_reactor_), -1);
    ASSERT_NE(pipe(from_reactor_), -1);

    Context ctx;
    ctx.fds[0] = to_reactor_[0];
    ctx.fds[1] = from_reactor_[1];

    ASSERT_EQ(pthread_create(&thread_, NULL, T_Reactor::ReactorFunction,
                             static_cast<void*>(&ctx)),
              0);

    ASSERT_EQ(ctx.ready.Dequeue(), true);
  }

  virtual void TearDown() {
    ASSERT_EQ(pthread_join(thread_, NULL), 0);
    close(to_reactor_[1]);
    close(from_reactor_[0]);
  }

  struct Context {
    Context() : fds(), ready(1, 1) {}

    int fds[2];
    FifoChannel<bool> ready;
  };

  static void* ReactorFunction(void* data) {
    Context* ctx = static_cast<Context*>(data);

    ctx->ready.Enqueue(true);

    MockedReactor reactor(ctx->fds[0], ctx->fds[1]);
    reactor.run();

    close(ctx->fds[0]);
    close(ctx->fds[1]);

    return NULL;
  }

  pthread_t thread_;
  int to_reactor_[2];
  int from_reactor_[2];
};

TEST_F(T_Reactor, kEcho_kQuit) {
  ASSERT_TRUE(Reactor::WriteRequest(to_reactor_[1], Reactor::kEcho, "Hey"));

  std::string reply;
  ASSERT_TRUE(Reactor::ReadReply(from_reactor_[0], &reply));
  ASSERT_EQ(reply, "Hey");

  ASSERT_TRUE(Reactor::WriteRequest(to_reactor_[1], Reactor::kQuit, ""));

  ASSERT_TRUE(Reactor::ReadReply(from_reactor_[0], &reply));
  ASSERT_EQ(reply, "ok");<commit_msg>Fixing tests in t_reactor.cc<commit_after>/**
 * This file is part of the CernVM File System.
 */

#include <gtest/gtest.h>

#include <logging.h>
#include "json_document.h"
#include "receiver/reactor.h"
#include "util/pointer.h"
#include "util/string.h"
#include "util_concurrency.h"

using namespace receiver;  // NOLINT

class MockedReactor : public Reactor {
 public:
  MockedReactor(int fdin, int fdout) : Reactor(fdin, fdout) {}
};

class T_Reactor : public ::testing::Test {
 protected:
  T_Reactor() : ready_(1, 1) {}

  virtual void SetUp() {
    ASSERT_NE(pipe(to_reactor_), -1);
    ASSERT_NE(pipe(from_reactor_), -1);

    ASSERT_EQ(pthread_create(&thread_, NULL, T_Reactor::ReactorFunction,
                             static_cast<void*>(this)),
              0);

    ASSERT_EQ(ready_.Dequeue(), true);
  }

  virtual void TearDown() {
    ASSERT_EQ(pthread_join(thread_, NULL), 0);
    close(to_reactor_[1]);
    close(from_reactor_[0]);
  }

  static void* ReactorFunction(void* data) {
    T_Reactor* ctx = static_cast<T_Reactor*>(data);

    ctx->ready_.Enqueue(true);

    MockedReactor reactor(ctx->to_reactor_[0], ctx->from_reactor_[1]);
    reactor.run();

    close(ctx->to_reactor_[0]);
    close(ctx->from_reactor_[1]);

    return NULL;
  }

  FifoChannel<bool> ready_;
  pthread_t thread_;
  int to_reactor_[2];
  int from_reactor_[2];
};

TEST_F(T_Reactor, kEcho_kQuit) {
  ASSERT_TRUE(Reactor::WriteRequest(to_reactor_[1], Reactor::kEcho, "Hey"));

  std::string reply;
  ASSERT_TRUE(Reactor::ReadReply(from_reactor_[0], &reply));
  ASSERT_EQ(reply, "Hey");

  ASSERT_TRUE(Reactor::WriteRequest(to_reactor_[1], Reactor::kQuit, ""));

  ASSERT_TRUE(Reactor::ReadReply(from_reactor_[0], &reply));
  ASSERT_EQ(reply, "ok");
{'fraction_non_alphanumeric': 0.15898295613299804, 'fraction_numerical': 0.011455713886560491, 'mean_word_length': 4.226277372262774, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '940876', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1325, 'n_words': 233}