<commit_before>module Bacon
  class Context
    include ::Elevate
  end
end

describe Elevate do
  extend WebStub::SpecHelpers

  describe "#async" do
    it "runs the specified task asynchronously" do
      async do
        task do
          true
        end

        on_finish do |result, exception|
          @called = result
          resume
        end
      end

      wait_max 1.0 do
        @called.should.be.true
      end
    end

    it "passes provided args to the task as instance variables" do
      async name: "harry" do
        task do
          @name
        end

        on_finish do |name, exception|
          @result = name
          resume
        end
      end

      wait_max 1.0 do
        @result.should == "harry"
      end
    end

    it "allows tasks to report progress" do
      @updates = []

      async do
        task do
          sleep 0.1
          yield 1
          sleep 0.2
          yield 2
          sleep 0.3
          yield 3

          true
        end

        on_update do |count|
          @updates << count
        end

        on_finish do |result, exception|
          resume
        end
      end

      wait_max 1.0 do
        @updates.should == [1,2,3]
      end
    end

    describe "timeouts" do
      before do
        stub_request(:get, "http://example.com/").
          to_return(body: "Hello!", content_type: "text/plain", delay: 1.0)
      end

      it "does not cancel the operation if it completes in time" do
        @timed_out = false

        async do
          timeout 3.0

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should == "finished"
          @timed_out.should.be.false
        end
      end

      it "stops the operation when timeout interval has elapsed" do
        task = async do
          timeout 0.5

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should.not == "finished"

          task.timed_out?.should.be.true
        end
      end

      it "invokes on_timeout when a timeout occurs" do
        @result = ""
        @timed_out = false

        async do
          timeout 0.5

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_timeout do
            @timed_out = true
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should.not == "finished"
          @timed_out.should.be.true
        end<commit_msg>Fix failing spec<commit_after>module Bacon
  class Context
    include ::Elevate
  end
end

describe Elevate do
  extend WebStub::SpecHelpers

  describe "#async" do
    it "runs the specified task asynchronously" do
      async do
        task do
          true
        end

        on_finish do |result, exception|
          @called = result
          resume
        end
      end

      wait_max 1.0 do
        @called.should.be.true
      end
    end

    it "passes provided args to the task as instance variables" do
      async name: "harry" do
        task do
          @name
        end

        on_finish do |name, exception|
          @result = name
          resume
        end
      end

      wait_max 1.0 do
        @result.should == "harry"
      end
    end

    it "allows tasks to report progress" do
      @updates = []

      async do
        task do
          sleep 0.1
          yield 1
          sleep 0.2
          yield 2
          sleep 0.3
          yield 3

          true
        end

        on_update do |count|
          @updates << count
        end

        on_finish do |result, exception|
          resume
        end
      end

      wait_max 1.0 do
        @updates.should == [1,2,3]
      end
    end

    describe "timeouts" do
      before do
        stub_request(:get, "http://example.com/").
          to_return(body: "Hello!", content_type: "text/plain", delay: 1.0)
      end

      it "does not cancel the operation if it completes in time" do
        @timed_out = false

        async do
          timeout 3.0

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should == "finished"
          @timed_out.should.be.false
        end
      end

      it "stops the operation when timeout interval has elapsed" do
        @result = nil

        @task = async do
          timeout 0.5

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should.not == "finished"

          @task.timed_out?.should.be.true
        end
      end

      it "invokes on_timeout when a timeout occurs" do
        @result = ""
        @timed_out = false

        async do
          timeout 0.5

          task do
            Elevate::HTTP.get("http://example.com/")

            "finished"
          end

          on_timeout do
            @timed_out = true
          end

          on_finish do |result, exception|
            @result = result
            resume
          end
        end

        wait_max 5.0 do
          @result.should.not == "finished"
          @timed_out.should.be.true
        end
{'fraction_non_alphanumeric': 0.07082344967807523, 'fraction_numerical': 0.010843781768891902, 'mean_word_length': 1.2034341172079135, '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': '2047492', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1636, 'n_words': 517}