<commit_before>require 'test_helper'
require 'stringio'

require 'eventmachine'
require 'em-synchrony'

module Librato
  class Rack
    class WorkerTest < Minitest::Test

      def test_basic_use
        worker = Worker.new
        counter = 0

        thread = Thread.new do
          worker.run_periodically(0.1) do
            counter += 1
          end
        end

        sleep 0.45
        assert_equal counter, 4
        Thread.kill(thread)
      end

      def test_start_time
        worker = Worker.new

        time = Time.now
        start = worker.start_time(60)
        assert start >= time + 60, 'should be more than 60 seconds from when run'
        assert_equal 0, start.sec, 'should start on a whole minute'

        time = Time.now
        start = worker.start_time(10)
        assert start >= time + 10, 'should be more than 10 seconds from when run'
        assert_equal 0, start.sec%10, 'should be evenly divisible with whole minutes'
      end

      def test_timer_type
        worker = Worker.new
        assert_equal :sleep, worker.timer

        em_worker = Worker.new(:timer => 'eventmachine')
        assert_equal :eventmachine, em_worker.timer

        # tolerate explicit nils
        worker = Worker.new(:timer => nil)
        assert_equal :sleep, worker.timer
      end

      def test_eventmachine_timer
        worker = Worker.new(:timer => :eventmachine)
        counter = 0

        thread = Thread.new do
          EventMachine.run do
            worker.run_periodically(0.1) do
              counter += 1
            end
          end
        end

        sleep 0.45
        assert_equal counter, 4
        Thread.kill(thread)
      end

      def test_em_synchrony_timer
        worker = Worker.new(:timer => :synchrony)
        counter = 0
<commit_msg>Clean things up more gently<commit_after>require 'test_helper'
require 'stringio'

require 'eventmachine'
require 'em-synchrony'

module Librato
  class Rack
    class WorkerTest < Minitest::Test

      def test_basic_use
        worker = Worker.new
        counter = 0

        thread = Thread.new do
          worker.run_periodically(0.1) do
            counter += 1
          end
        end

        sleep 0.45
        assert_equal counter, 4

        worker.stop!
        thread.join
      end

      def test_start_time
        worker = Worker.new

        time = Time.now
        start = worker.start_time(60)
        assert start >= time + 60, 'should be more than 60 seconds from when run'
        assert_equal 0, start.sec, 'should start on a whole minute'

        time = Time.now
        start = worker.start_time(10)
        assert start >= time + 10, 'should be more than 10 seconds from when run'
        assert_equal 0, start.sec%10, 'should be evenly divisible with whole minutes'
      end

      def test_timer_type
        worker = Worker.new
        assert_equal :sleep, worker.timer

        em_worker = Worker.new(:timer => 'eventmachine')
        assert_equal :eventmachine, em_worker.timer

        # tolerate explicit nils
        worker = Worker.new(:timer => nil)
        assert_equal :sleep, worker.timer
      end

      def test_eventmachine_timer
        worker = Worker.new(:timer => :eventmachine)
        counter = 0

        thread = Thread.new do
          EventMachine.run do
            worker.run_periodically(0.1) do
              counter += 1
            end
            EM.add_timer(0.6) { worker.stop!; EM.stop }
          end
        end

        sleep 0.45
        assert_equal counter, 4
        thread.join
      end

      def test_em_synchrony_timer
        worker = Worker.new(:timer => :synchrony)
        counter = 0

{'fraction_non_alphanumeric': 0.07205240174672489, 'fraction_numerical': 0.018558951965065504, 'mean_word_length': 1.834493426140758, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '6061452', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1026, 'n_words': 310}