<commit_before>require 'test_helper'

class S5::DaemonTest < S5::Test
  class DaemonFiber < S5::Daemon
    attr_accessor :fiber
    def observe
      @fiber = Fiber.new do
        super
      end
    end

    %w(create_or_update delete).each do |name|
      module_eval <<-METHOD, __FILE__, __LINE__
        def #{name}(*args)
          proc = super
          ->(base, relative){
            proc.call(base, relative).tap{
              Fiber.yield
            }
          }
        end
      METHOD
    end

    def resume
      @fiber.resume
    end
  end

  def setup
    @fixture_dir = fixtures_path
    @bucket_name = "#{ENV["USER"]}-s5-daemon-test"
    @daemon = DaemonFiber.new(@fixture_dir, bucket_name: @bucket_name)
    @path = @fixture_dir.join(Time.now.to_f.to_s)
  end

  def teardown
    if File.exists?(@path)
      File.unlink(@path)
    end
  end

  def test_touch_new_file_and_delete
    @daemon.observe
    user_context{ FileUtils.touch @path }
    @daemon.resume
    key = File.basename(@path)
    assert true, s3_object(@bucket_name, key).exists?

    user_context{ FileUtils.rm_rf(@path) }
    @daemon.resume
    assert_raises AWS::S3::Errors::NoSuchKey do
      s3_object(@bucket_name, key).read
    end
  end

  private
  def user_context
    fork do
      sleep 0.1
      yield
    end
  end

  def s3_object(bucket_name, key)
    AWS.s3.buckets[bucket_name].objects[key]
  end
end<commit_msg>Refacotoring.<commit_after>require 'test_helper'

class S5::DaemonTest < S5::Test
  class DaemonFiber < S5::Daemon
    attr_accessor :fiber
    def observe
      @fiber = Fiber.new do
        super
      end
    end

    %w(create_or_update delete).each do |name|
      module_eval <<-METHOD, __FILE__, __LINE__
        def #{name}(*args)
          proc = super
          ->(base, relative){
            proc.call(base, relative).tap{
              Fiber.yield
            }
          }
        end
      METHOD
    end

    def resume
      @fiber.resume
    end
  end

  def setup
    @fixture_dir = fixtures_path
    @bucket_name = "#{ENV["USER"]}-s5-daemon-test"
    @daemon = DaemonFiber.new(@fixture_dir, bucket_name: @bucket_name)
    @path = @fixture_dir.join(Time.now.to_f.to_s)
  end

  def teardown
    if File.exists?(@path)
      File.unlink(@path)
    end
  end

  def test_touch_new_file_and_delete
    @daemon.observe
    user_context{ FileUtils.touch @path }
    @daemon.resume
    key = File.basename(@path)
    assert true, s3_object(key).exists?

    user_context{ FileUtils.rm_rf(@path) }
    @daemon.resume
    assert_raises AWS::S3::Errors::NoSuchKey do
      s3_object(key).read
    end
  end

  private
  def user_context
    fork do
      sleep 0.1
      yield
    end
  end

  def s3_object(key)
    bucket.objects[key]
  end

  def bucket
    AWS.s3.buckets[@bucket_name]
  end
end
{'fraction_non_alphanumeric': 0.10152104704633888, 'fraction_numerical': 0.007782101167315175, 'mean_word_length': 2.280742459396752, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '2062242', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1002, 'n_words': 204}