aboutsummaryrefslogtreecommitdiff
path: root/test/mauve_time.rb
blob: d8c57a7f5cf1b9067248c2037015850643072176 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'logger'
require 'time'

module Mauve
  # A fake Time, which we use in testing.  Time#now returns the same value every
  # time, unless we call Time#advance which alters the value of 'now' by a
  # given number of seconds.  There is a simple pass-through for other methods.
  #
  class Time
    class << self
      def reset_to_midnight
        @now = Time.parse("00:00")
        Log.debug "Test time reset to #{@now}"
      end
    
      def now
        reset_to_midnight unless @now
        @now
      end
      
      def advance(seconds)
        @now += seconds
        Log.debug "Test time advanced by #{seconds} to #{@now}, kicking Mauve::Timers"
        Timers.restart_and_then_wait_until_idle
        @now
      end
      
      def at(*a)
        ::Time.at(*a)
      end
      
      def parse(*a)
        ::Time.parse(*a)
      end
    end
  end
end