<commit_before>#include "greatest.h"

#include "event_loop.h"
#include "example_task.h"
#include "test_common.h"
#include "common.h"
#include "state/object_table.h"
#include "state/redis.h"

#include <unistd.h>
#include <ae.h>

SUITE(task_table_tests);

event_loop *g_loop;
TaskBuilder *g_task_builder = NULL;

/* ==== Test operations in non-failure scenario ==== */

/* === A lookup of a task not in the table === */

TaskID lookup_nil_id;
int lookup_nil_success = 0;
const char *lookup_nil_context = "lookup_nil";

void lookup_nil_fail_callback(UniqueID id,
                              void *user_context,
                              void *user_data) {
  /* The fail callback should not be called. */
  CHECK(0);
}

void lookup_nil_success_callback(Task *task, void *context) {
  lookup_nil_success = 1;
  CHECK(task == NULL);
  CHECK(context == (void *) lookup_nil_context);
  event_loop_stop(g_loop);
}

TEST lookup_nil_test(void) {
  lookup_nil_id = globally_unique_id();
  g_loop = event_loop_create();
  DBHandle *db = db_connect(std::string("127.0.0.1"), 6379, "plasma_manager",
                            "127.0.0.1", 0, NULL);
  db_attach(db, g_loop, false);
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = lookup_nil_fail_callback,
  };
  task_table_get_task(db, lookup_nil_id, &retry, lookup_nil_success_callback,
                      (void *) lookup_nil_context);
  /* Disconnect the database to see if the lookup times out. */
  event_loop_run(g_loop);
  db_disconnect(db);
  destroy_outstanding_callbacks(g_loop);
  event_loop_destroy(g_loop);
  ASSERT(lookup_nil_success);
  PASS();
}

/* === A lookup of a task after it's added returns the same spec === */

int add_success = 0;
int lookup_success = 0;
Task *add_lookup_task;
const char *add_lookup_context = "add_lookup";

void add_lookup_fail_callback(UniqueID id,
                              void *user_context,
                              void *user_data) {
  /* The fail callback should not be called. */
  CHECK(0);
}

void lookup_success_callback(Task *task, void *context) {
  lookup_success = 1;
  CHECK(memcmp(task, add_lookup_task, Task_size(task)) == 0);
  event_loop_stop(g_loop);
}

void add_success_callback(TaskID task_id, void *context) {
  add_success = 1;
  CHECK(TaskID_equal(task_id, Task_task_id(add_lookup_task)));

  DBHandle *db = (DBHandle *) context;
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = add_lookup_fail_callback,
  };
  task_table_get_task(db, task_id, &retry, lookup_success_callback,
                      (void *) add_lookup_context);
}

TEST add_lookup_test(void) {
  add_lookup_task = example_task(1, 1, TASK_STATUS_WAITING);
  g_loop = event_loop_create();
  DBHandle *db = db_connect(std::string("127.0.0.1"), 6379, "plasma_manager",
                            "127.0.0.1", 0, NULL);
  db_attach(db, g_loop, false);
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = add_lookup_fail_callback,
  };
  task_table_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
                       NULL, NULL);
  task_table_add_task(db, Task_copy(add_lookup_task), &retry,
                      add_success_callback, (void *) db);
  /* Disconnect the database to see if the lookup times out. */
  event_loop_run(g_loop);
  db_disconnect(db);
  destroy_outstanding_callbacks(g_loop);
  event_loop_destroy(g_loop);
  ASSERT(add_success);
  ASSERT(lookup_success);
  PASS();
}

/* ==== Test if operations time out correctly ==== */

/* === Test subscribe timeout === */

const char *subscribe_timeout_context = "subscribe_timeout";
int subscribe_failed = 0;

void subscribe_done_callback(TaskID task_id, void *user_context) {
  /* The done callback should not be called. */
  CHECK(0);
}<commit_msg>Fix failing task table test (#924)<commit_after>#include "greatest.h"

#include "event_loop.h"
#include "example_task.h"
#include "test_common.h"
#include "common.h"
#include "state/object_table.h"
#include "state/redis.h"

#include <unistd.h>
#include <ae.h>

SUITE(task_table_tests);

event_loop *g_loop;
TaskBuilder *g_task_builder = NULL;

/* ==== Test operations in non-failure scenario ==== */

/* === A lookup of a task not in the table === */

TaskID lookup_nil_id;
int lookup_nil_success = 0;
const char *lookup_nil_context = "lookup_nil";

void lookup_nil_fail_callback(UniqueID id,
                              void *user_context,
                              void *user_data) {
  /* The fail callback should not be called. */
  CHECK(0);
}

void lookup_nil_success_callback(Task *task, void *context) {
  lookup_nil_success = 1;
  CHECK(task == NULL);
  CHECK(context == (void *) lookup_nil_context);
  event_loop_stop(g_loop);
}

TEST lookup_nil_test(void) {
  lookup_nil_id = globally_unique_id();
  g_loop = event_loop_create();
  DBHandle *db = db_connect(std::string("127.0.0.1"), 6379, "plasma_manager",
                            "127.0.0.1", 0, NULL);
  db_attach(db, g_loop, false);
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = lookup_nil_fail_callback,
  };
  task_table_get_task(db, lookup_nil_id, &retry, lookup_nil_success_callback,
                      (void *) lookup_nil_context);
  /* Disconnect the database to see if the lookup times out. */
  event_loop_run(g_loop);
  db_disconnect(db);
  destroy_outstanding_callbacks(g_loop);
  event_loop_destroy(g_loop);
  ASSERT(lookup_nil_success);
  PASS();
}

/* === A lookup of a task after it's added returns the same spec === */

int add_success = 0;
int lookup_success = 0;
Task *add_lookup_task;
const char *add_lookup_context = "add_lookup";

void add_lookup_fail_callback(UniqueID id,
                              void *user_context,
                              void *user_data) {
  /* The fail callback should not be called. */
  CHECK(0);
}

void lookup_success_callback(Task *task, void *context) {
  lookup_success = 1;
  CHECK(memcmp(task, add_lookup_task, Task_size(task)) == 0);
  event_loop_stop(g_loop);
}

void add_success_callback(TaskID task_id, void *context) {
  add_success = 1;
  CHECK(TaskID_equal(task_id, Task_task_id(add_lookup_task)));

  DBHandle *db = (DBHandle *) context;
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = add_lookup_fail_callback,
  };
  task_table_get_task(db, task_id, &retry, lookup_success_callback,
                      (void *) add_lookup_context);
}

void subscribe_success_callback(TaskID task_id, void *context) {
  DBHandle *db = (DBHandle *) context;
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = add_lookup_fail_callback,
  };
  task_table_add_task(db, Task_copy(add_lookup_task), &retry,
                      add_success_callback, (void *) db);
}

TEST add_lookup_test(void) {
  add_lookup_task = example_task(1, 1, TASK_STATUS_WAITING);
  g_loop = event_loop_create();
  DBHandle *db = db_connect(std::string("127.0.0.1"), 6379, "plasma_manager",
                            "127.0.0.1", 0, NULL);
  db_attach(db, g_loop, false);
  RetryInfo retry = {
      .num_retries = 5,
      .timeout = 1000,
      .fail_callback = add_lookup_fail_callback,
  };
  /* Wait for subscription to succeed before adding the task. */
  task_table_subscribe(db, NIL_ID, TASK_STATUS_WAITING, NULL, NULL, &retry,
                       subscribe_success_callback, (void *) db);
  /* Disconnect the database to see if the lookup times out. */
  event_loop_run(g_loop);
  db_disconnect(db);
  destroy_outstanding_callbacks(g_loop);
  event_loop_destroy(g_loop);
  ASSERT(add_success);
  ASSERT(lookup_success);
  PASS();
}

/* ==== Test if operations time out correctly ==== */

/* === Test subscribe timeout === */

const char *subscribe_timeout_context = "subscribe_timeout";
int subscribe_failed = 0;

void subscribe_done_callback(TaskID task_id, void *user_context) {
  /* The done callback should not be called. */
  CHECK(0);
}
{'fraction_non_alphanumeric': 0.12112921278447954, 'fraction_numerical': 0.016415868673050615, 'mean_word_length': 3.4114097641250685, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, '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': '110900', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2720, 'n_words': 606}