Skip to content

Instantly share code, notes, and snippets.

@dnagir
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnagir/5329defec514596fdc6e to your computer and use it in GitHub Desktop.
Save dnagir/5329defec514596fdc6e to your computer and use it in GitHub Desktop.
Chronic time_class (how come it doesn't break in Rubinius?)
> rvm use ruby-2.1.1
Using /Users/dnagir/.rvm/gems/ruby-2.1.1
> ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
> ruby test.rb
Run options: --seed 49028
# Running:
...................................................................................................................................................
Finished in 4.785508s, 30.7177 runs/s, 427.1229 assertions/s.
147 runs, 2044 assertions, 0 failures, 0 errors, 0 skips
> rvm use rbx
Using /Users/dnagir/.rvm/gems/rbx-2.2.7
> ruby -v
rubinius 2.2.7 (2.1.0 build 2014-05-20 JI) [x86_64-darwin13.2.0]
> ruby test.rb
Run options: --seed 49590
# Running:
...................................................................................................................................................
Finished in 5.491719s, 26.7676 runs/s, 366.9161 assertions/s.
147 runs, 2015 assertions, 0 failures, 0 errors, 0 skips
> rvm use jruby
Using /Users/dnagir/.rvm/gems/jruby-1.7.12
> ruby -v
jruby 1.7.12 (1.9.3p392) 2014-04-15 643e292 on Java HotSpot(TM) Client VM 1.6.0_65-b14-462-11M4609 [darwin-i386]
> ruby test.rb
Run options: --seed 64654
# Running:
.....................................................E.............................................................................................
Finished in 1.841000s, 79.8479 runs/s, 1082.0206 assertions/s.
1) Error:
parsing across time zones#test_0001_should not break with cuncurent threads (brute force):
NoMethodError: undefined method `new' for #<ActiveSupport::TimeZone:0x602d9d>
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic.rb:146:in `construct'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/repeaters/repeater_day.rb:29:in `this'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/handlers.rb:526:in `get_anchor'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/handlers.rb:414:in `handle_r'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/handler.rb:76:in `invoke'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/parser.rb:247:in `tokens_to_span'
org/jruby/RubyArray.java:1613:in `each'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/parser.rb:244:in `tokens_to_span'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic/parser.rb:61:in `parse'
/Users/dnagir/.rvm/gems/jruby-1.7.12/gems/chronic-0.10.2/lib/chronic.rb:90:in `parse'
test.rb:10:in `__ensure__'
test.rb:8:in `parse_with_tz'
test.rb:41:in `(root)'
org/jruby/RubyFixnum.java:275:in `times'
test.rb:40:in `(root)'
147 runs, 1992 assertions, 0 failures, 1 errors, 0 skips
require 'chronic'
require 'active_support/all'
require 'minitest/autorun'
def parse_with_tz(value)
previous = Chronic.time_class
begin
Chronic.time_class = Time.zone
return Chronic.parse(value)
ensure
Chronic.time_class = previous
end
end
describe "parsing across time zones" do
ActiveSupport::TimeZone.all.each do |tz|
describe "in time zone #{tz}" do
before { @prev_tz = Time.zone; Time.zone = tz }
after { Time.zone = @prev_tz }
it "should parse date correctly" do
assert_equal Time.zone.now.to_date, parse_with_tz('today').to_date
assert_equal 1.day.ago.to_date, parse_with_tz('yesterday').to_date
end
end
end
it "should not break with cuncurent threads (brute force)" do
threads = ActiveSupport::TimeZone.all.map do |tz|
(1..3).map do # threads per zone
Thread.new do
Time.zone = 'Melbourne' # Per thread, can be anything
2.times do
assert_equal Time.zone.now.to_date, parse_with_tz('today').to_date
assert_equal 1.day.ago.to_date, parse_with_tz('yesterday').to_date
end
end
end
end.flatten
threads.each(&:join)
end
end
@dnagir
Copy link
Author

dnagir commented Jun 25, 2014

This is in relation to mojombo/chronic#182

@dnagir
Copy link
Author

dnagir commented Jun 25, 2014

I was expecting it to fail on RBX due to attr_accessor :time_class https://github.com/mojombo/chronic/blob/05ed556dae27bf84d75688d98f3b561cfeb9c5ad/lib/chronic.rb#L75

@dnagir
Copy link
Author

dnagir commented Jun 25, 2014

Note the 2044 assertions on MRU vs 2015 on Rubinius. That needs explanation too.

@dnagir
Copy link
Author

dnagir commented Jun 25, 2014

The implementation similar to this will make it work on JRuby too.

SYNC = Mutex.new

def parse_with_tz(value)
  SYNC.synchronize do
    previous = Chronic.time_class
    begin
      Chronic.time_class = Time.zone
      return Chronic.parse(value)
    ensure
      Chronic.time_class = previous
    end
  end
end

@dnagir
Copy link
Author

dnagir commented Jun 25, 2014

The simplified test actually breaks even on MRI which is expected:
https://gist.github.com/dnagir/80df45c96b49776dd174

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment