<commit_before>require 'spec_helper'

# Timeouts should be at least this precise (in seconds) to pass the tests
# Typical precision should be better than this, but if it's worse it will fail
# the tests
TIMEOUT_PRECISION = 0.1

describe NIO::Selector do
  let(:pair)   { IO.pipe }
  let(:reader) { pair.first }
  let(:writer) { pair.last }

  context "register" do
    it "registers IO objects" do
      monitor = subject.register(reader, :r)
      monitor.should_not be_closed
    end

    it "raises TypeError if asked to register non-IO objects" do
      expect { subject.register(42, :r) }.to raise_exception TypeError
    end
  end

  it "knows which IO objects are registered" do
    subject.register(reader, :r)
    subject.should be_registered(reader)
    subject.should_not be_registered(writer)
  end

  it "deregisters IO objects" do
    subject.register(reader, :r)

    monitor = subject.deregister(reader)
    subject.should_not be_registered(reader)
    monitor.should be_closed
  end

  # This spec might seem a bit silly, but this actually something the
  # Java NIO API specifically precludes that we need to work around
  it "allows regegistration of the same IO object across select calls" do
    monitor = subject.register(reader, :r)
    writer << "ohai"

    subject.select.should include monitor
    reader.read(4).should == "ohai"
    subject.deregister(reader)

    new_monitor = subject.register(reader, :r)<commit_msg>Hurr, reregistration, not regegistration<commit_after>require 'spec_helper'

# Timeouts should be at least this precise (in seconds) to pass the tests
# Typical precision should be better than this, but if it's worse it will fail
# the tests
TIMEOUT_PRECISION = 0.1

describe NIO::Selector do
  let(:pair)   { IO.pipe }
  let(:reader) { pair.first }
  let(:writer) { pair.last }

  context "register" do
    it "registers IO objects" do
      monitor = subject.register(reader, :r)
      monitor.should_not be_closed
    end

    it "raises TypeError if asked to register non-IO objects" do
      expect { subject.register(42, :r) }.to raise_exception TypeError
    end
  end

  it "knows which IO objects are registered" do
    subject.register(reader, :r)
    subject.should be_registered(reader)
    subject.should_not be_registered(writer)
  end

  it "deregisters IO objects" do
    subject.register(reader, :r)

    monitor = subject.deregister(reader)
    subject.should_not be_registered(reader)
    monitor.should be_closed
  end

  # This spec might seem a bit silly, but this actually something the
  # Java NIO API specifically precludes that we need to work around
  it "allows reregistration of the same IO object across select calls" do
    monitor = subject.register(reader, :r)
    writer << "ohai"

    subject.select.should include monitor
    reader.read(4).should == "ohai"
    subject.deregister(reader)

    new_monitor = subject.register(reader, :r)
{'fraction_non_alphanumeric': 0.0815347721822542, 'fraction_numerical': 0.0034258307639602604, 'mean_word_length': 3.6056782334384856, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '88146', 'n_tokens_mistral': 900, 'n_tokens_neox': 852, 'n_words': 306}