<commit_before>require 'spec_helper'

describe Oni::Daemon do
  let :example_daemon do
    mapper = Class.new(Oni::Mapper) do
      attr_reader :number

      def map_input(input)
        return input[:number]
      end

      def map_output(number)
        return {:new_number => number}
      end
    end

    worker = Class.new(Oni::Worker) do
      def process(number)
        return number * 2
      end
    end

    Class.new(Oni::Daemon) do
      attr_reader :number
      attr_reader :result

      set :mapper, mapper
      set :worker, worker
      set :threads, 2

      def after_initialize
        @number = 10
      end

      def receive
        yield({:number => 10})
      end

      def complete(message)
        @result = message
      end
    end
  end

  example 'call #after_initialize' do
    example_daemon.new.number.should == 10
  end

  example 'raise for the default receive method' do
    daemon = Oni::Daemon.new

    lambda { daemon.receive }.should raise_error(NotImplementedError)
  end

  example 'return the amount of threads to use' do
    example_daemon.set(:threads, 10)

    example_daemon.new.threads.should == 10
  end

  example 'use the default amount of threads' do
    example_daemon.set(:threads, nil)

    example_daemon.new.threads.should == Oni::Daemon::DEFAULT_THREAD_AMOUNT
  end

  example 'create the mapper without any arguments' do
    mapper = example_daemon.new.create_mapper<commit_msg>Tests for the before_start hook.<commit_after>require 'spec_helper'

describe Oni::Daemon do
  let :example_daemon do
    mapper = Class.new(Oni::Mapper) do
      attr_reader :number

      def map_input(input)
        return input[:number]
      end

      def map_output(number)
        return {:new_number => number}
      end
    end

    worker = Class.new(Oni::Worker) do
      def process(number)
        return number * 2
      end
    end

    Class.new(Oni::Daemon) do
      attr_reader :number, :number2, :result

      set :mapper, mapper
      set :worker, worker
      set :threads, 2

      def after_initialize
        @number = 10
      end

      def before_start
        @number2 = 20
      end

      def receive
        yield({:number => 10})
      end

      def complete(message)
        @result = message
      end
    end
  end

  example 'call #after_initialize' do
    example_daemon.new.number.should == 10
  end

  example 'call #before_start' do
    daemon = example_daemon.new
    daemon.start
    daemon.stop

    daemon.number2.should == 20
  end

  example 'raise for the default receive method' do
    daemon = Oni::Daemon.new

    lambda { daemon.receive }.should raise_error(NotImplementedError)
  end

  example 'return the amount of threads to use' do
    example_daemon.set(:threads, 10)

    example_daemon.new.threads.should == 10
  end

  example 'use the default amount of threads' do
    example_daemon.set(:threads, nil)

    example_daemon.new.threads.should == Oni::Daemon::DEFAULT_THREAD_AMOUNT
  end

  example 'create the mapper without any arguments' do
    mapper = example_daemon.new.create_mapper
{'fraction_non_alphanumeric': 0.07980613893376413, 'fraction_numerical': 0.010016155088852988, 'mean_word_length': 2.506228765571914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6924584', 'n_tokens_mistral': 1010, 'n_tokens_neox': 938, 'n_words': 271}