Sha256: 40ea1a1410e147199a1f676f6e862017bea3a3bf25ad540aad1d062d06ed1055

Contents?: true

Size: 1.28 KB

Versions: 33

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Mutant
  class Timer
    include Concord.new(:process)

    # The now monotonic time
    #
    # @return [Float]
    def now
      process.clock_gettime(Process::CLOCK_MONOTONIC)
    end

    class Deadline
      include Anima.new(:timer, :allowed_time)

      def initialize(*arguments)
        super(*arguments)
        @start_at = timer.now
      end

      # Test if deadline is expired
      #
      # @return [Boolean]
      def expired?
        time_left <= 0
      end

      # Deadline status snapshot
      class Status
        include Concord::Public.new(:time_left)

        # Test if deadline is not yet expired
        def ok?
          time_left.nil? || time_left.positive?
        end
      end # Status

      # Capture a deadline status
      #
      # @return [Status]
      def status
        Status.new(time_left)
      end

      # Probe the time left
      #
      # @return [Float, nil]
      def time_left
        allowed_time - (timer.now - @start_at)
      end

      # Deadline that never expires
      class None < self
        include Concord.new

        # The time left
        #
        # @return [Float, nil]
        def time_left; end

        def expired?
          false
        end
      end
    end # Deadline
  end # Timer
end # Mutant

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
mutant-0.11.18 lib/mutant/timer.rb
mutant-0.11.17 lib/mutant/timer.rb
mutant-0.11.16 lib/mutant/timer.rb
mutant-0.11.15 lib/mutant/timer.rb
mutant-0.11.14 lib/mutant/timer.rb
mutant-0.11.13 lib/mutant/timer.rb
mutant-0.11.12 lib/mutant/timer.rb
mutant-0.11.11 lib/mutant/timer.rb
mutant-0.11.10 lib/mutant/timer.rb
mutant-0.11.9 lib/mutant/timer.rb
mutant-0.11.8 lib/mutant/timer.rb
mutant-0.11.7 lib/mutant/timer.rb
mutant-0.11.6 lib/mutant/timer.rb
mutant-0.11.5 lib/mutant/timer.rb
mutant-0.11.4 lib/mutant/timer.rb
mutant-0.11.3 lib/mutant/timer.rb
mutant-0.11.2 lib/mutant/timer.rb
mutant-0.11.1 lib/mutant/timer.rb
mutant-0.11.0 lib/mutant/timer.rb
mutant-0.10.35 lib/mutant/timer.rb