<commit_before>require 'rspec/message/within/version'
require 'rspec/mocks'

module RSpec
  module Message
    module Within

      def self.until(within_time)
        if within_time && within_time > 0.0
          time = Time.now + within_time
          while time > Time.now
            break if yield
            sleep 0
          end
        end
      end

      module MessageExpectationPatch
        def self.included(base)
          # Seems to not work. Why?
          base.send :include, InstanceMethods

          base.send :alias_method, :orig_verify_messages_received, :verify_messages_received
          base.send :undef_method, :verify_messages_received
          base.send :define_method, :verify_messages_received, InstanceMethods.instance_method(:verify_messages_received)
        end

        module InstanceMethods
          def verify_messages_received
            Within.until(@within_time || 0) do
              expected_messages_received? and !negative?
            end

            orig_verify_messages_received
          end

          def within(time)
            @within_time = time
            self
          end
        end
      end

      module ReceivePatch
        def self.included(base)
          base.send :include, InstanceMethods
        end

        module InstanceMethods
          def within(*args, &block)
            @recorded_customizations << ::RSpec::Mocks::Matchers::Receive::Customization.new(:within, args, block)
            self
          end
          def seconds
            self
          end
        end
      end

      module ExpectationChainPatch
        def self.included(base)
          base.send :alias_method, :orig_expectation_fulfilled?, :expectation_fulfilled?
          base.send :undef_method, :expectation_fulfilled?
          base.send :define_method, :expectation_fulfilled?, InstanceMethods.instance_method(:expectation_fulfilled?)
        end

        module InstanceMethods
          def expectation_fulfilled?
            Within.until(@within_time || 0) do
              orig_expectation_fulfilled?
            end

            orig_expectation_fulfilled?
          end
        end
      end

      module ChainPatch
        def within(time)
          @within_time = time
          self
        end
      end

      ::RSpec::Mocks::MessageExpectation.send :include, MessageExpectationPatch
      ::RSpec::Mocks::AnyInstance::Chain.send :include, ChainPatch
      ::RSpec::Mocks::AnyInstance::ExpectationChain.send :include, ExpectationChainPatch
      ::RSpec::Mocks::Matchers::Receive.send :include, ReceivePatch
    end
  end<commit_msg>Direct inject to class definition instead if including patch modules.<commit_after>require 'rspec/message/within/version'
require 'rspec/mocks'

module RSpec
  module MessageWithin
    def self.until(within_time)
      if within_time && within_time > 0.0
        time = Time.now + within_time
        while time > Time.now
          break if yield
          sleep 0
        end
      end
    end
  end
end

module RSpec
  module Mocks
    class MessageExpectation
      alias_method :orig_verify_messages_received, :verify_messages_received
      def verify_messages_received
        MessageWithin.until(@within_time || 0) do
          expected_messages_received? and !negative?
        end

        orig_verify_messages_received
      end

      def within(time)
        @within_time = time
        self
      end
    end

    module Matchers
      class Receive
        def within(*args, &block)
          @recorded_customizations << ::RSpec::Mocks::Matchers::Receive::Customization.new(:within, args, block)
          self
        end

        def seconds
          self
        end
      end
    end

    module AnyInstance
      class ExpectationChain
        alias_method :orig_expectation_fulfilled?, :expectation_fulfilled?

        def expectation_fulfilled?
          RSpec::MessageWithin.until(@within_time || 0) do
            orig_expectation_fulfilled?
          end

          orig_expectation_fulfilled?
        end

        def within(time)
          @within_time = time
          self
        end
      end
    end
  end
{'fraction_non_alphanumeric': 0.06266570257893468, 'fraction_numerical': 0.002410219329959026, 'mean_word_length': 2.0537159676232526, 'pattern_counts': {'":': 0, '<': 7, '<?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': '939899', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1076, 'n_words': 261}