<commit_before>#include <iostream>

#include "vm/object_utils.hpp"
#include "builtin/channel.hpp"
#include "builtin/list.hpp"
#include "builtin/symbol.hpp"
#include "builtin/thread.hpp"

#include "vm.hpp"
#include "objectmemory.hpp"
#include "event.hpp"

#include <cxxtest/TestSuite.h>
#include <sys/time.h>

using namespace rubinius;

class TestChannel : public CxxTest::TestSuite {
  public:

  VM* state;
  Channel* chan;

  void setUp() {
    state = new VM();
    /** TODO: This nasty hack allows tearing down the watchers.
     *        Should be fixed properly at some point. --rue
     */
    state->events->owner = true;

    chan = Channel::create(state);
  }

  /* TODO: This is a rather nasty workaround.
   *       The default loop issue needs to be
   *       addressed at some point when MVM is
   *       acute. --rue */
  void tearDown() {
    delete state;
  }

  void test_channel_fields() {
    TS_ASSERT_EQUALS(2U, Channel::fields);
  }

  void test_create() {
    TS_ASSERT(kind_of<Channel>(chan));
    TS_ASSERT(kind_of<List>(chan->waiting()));
  }

  void test_send_then_receive() {
    Task* task = Task::create(state, 10);

    state->globals.current_task.set(task);

    TS_ASSERT(chan->value()->nil_p());
    chan->send(state, Qtrue);

    TS_ASSERT(kind_of<List>(chan->value()));
    List* lst = as<List>(chan->value());
    TS_ASSERT_EQUALS(lst->size(), 1U);
    TS_ASSERT_EQUALS(lst->locate(state, 0), Qtrue);

    chan->receive(state);
    TS_ASSERT_EQUALS(task->calculate_sp(), 0);
    TS_ASSERT_EQUALS(task->stack_top(), Qtrue);
    TS_ASSERT_EQUALS(lst->size(), 0U);
  }

  void test_receive_causes_deadlock() {
    Task* task = Task::create(state, 10);

    state->globals.current_task.set(task);

    Thread* thread = Thread::create(state);
    state->globals.current_thread.set(thread);

    bool thrown = false;
    try {
      chan->receive(state);
    } catch(DeadLock& e) {
      thrown = true;
    }

    TS_ASSERT(thrown);
  }

  void test_receive_causes_thread_switch() {
    Thread* other = Thread::create(state);

    Thread* orig = G(current_thread);

    state->queue_thread(other);<commit_msg>Fixed a Channel test with invalid assumption.<commit_after>#include <iostream>

#include "vm/object_utils.hpp"
#include "builtin/channel.hpp"
#include "builtin/list.hpp"
#include "builtin/symbol.hpp"
#include "builtin/thread.hpp"

#include "vm.hpp"
#include "objectmemory.hpp"
#include "event.hpp"

#include <cxxtest/TestSuite.h>
#include <sys/time.h>

using namespace rubinius;

class TestChannel : public CxxTest::TestSuite {
  public:

  VM* state;
  Channel* chan;

  void setUp() {
    state = new VM();
    /** TODO: This nasty hack allows tearing down the watchers.
     *        Should be fixed properly at some point. --rue
     */
    state->events->owner = true;

    chan = Channel::create(state);
  }

  /* TODO: This is a rather nasty workaround.
   *       The default loop issue needs to be
   *       addressed at some point when MVM is
   *       acute. --rue */
  void tearDown() {
    delete state;
  }

  void test_channel_fields() {
    TS_ASSERT_EQUALS(2U, Channel::fields);
  }

  void test_create() {
    TS_ASSERT(kind_of<Channel>(chan));
    TS_ASSERT(kind_of<List>(chan->waiting()));
  }

  void test_send_then_receive() {
    Task* task = Task::create(state, 10);

    state->globals.current_task.set(task);

    TS_ASSERT(chan->value()->nil_p());
    chan->send(state, Qtrue);

    TS_ASSERT(kind_of<List>(chan->value()));
    List* lst = as<List>(chan->value());
    TS_ASSERT_EQUALS(lst->size(), 1U);
    TS_ASSERT_EQUALS(lst->locate(state, 0), Qtrue);

    chan->receive(state);
    TS_ASSERT_EQUALS(task->calculate_sp(), 0);
    TS_ASSERT_EQUALS(task->stack_top(), Qtrue);
    TS_ASSERT_EQUALS(lst->size(), 0U);
  }

  void test_receive_causes_deadlock() {
    Task* task = Task::create(state, 10);

    state->globals.current_task.set(task);

    Thread* thread = Thread::create(state);
    state->globals.current_thread.set(thread);

    bool thrown = false;
    try {
      chan->receive(state);
    } catch(DeadLock& e) {
      thrown = true;
    }

    TS_ASSERT(thrown);
  }

  void test_receive_causes_thread_switch() {
    Thread* other = Thread::create(state);
    other->wakeup(state);

    Thread* orig = G(current_thread);

    state->queue_thread(other);
{'fraction_non_alphanumeric': 0.14825046040515655, 'fraction_numerical': 0.004143646408839779, 'mean_word_length': 3.4246435845213847, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 56, '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': '2233154', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1482, 'n_words': 308}