<commit_before>require File.dirname(__FILE__) + '/test_helper'
require 'memcache'

TEST_EXPIRY = 1

class Foo
  extend MethodCache

  def foo(i)
    @i ||= 0
    @i  += i
  end
  cache_method :foo

  cache_method :bar
  def bar
    @i ||= 0
    @i  += 1
  end

  @@i = 0
  def baz(i)
    @@i += i
  end
  cache_method :baz, :cache => :remote

  cache_class_method :bap
  def self.bap(i)
    @i ||= 0
    @i  += i
  end

  cache_class_method :zap, :counter => true
  def self.zap
    0
  end

  attr_accessor :z
  def zang
    self.z ||= 1
    self.z  += 1
    nil
  end
  cache_method :zang

  cache_method :pow, :expiry => TEST_EXPIRY
  def pow(ignored)
    Time.now
  end

  attr_accessor :bam_updated_at

  cache_method :bam, :load_validation => lambda {|val, meta|
    if bam_updated_at
      meta[:cached_at] > bam_updated_at
    else
      true
    end
  }

  def bam
    @i ||= 0
    @i  += 1
  end
end

module Bar
  extend MethodCache

  cache_method :foo
  def foo(i)
    @i ||= 0
    @i  += i
  end

  cache_method :foo_count, :counter => true, :cache => :default
  def foo_count(key)
    100
  end
end

class Baz
  include Bar
  extend  Bar
end

class A
  extend MethodCache
end

class B < A
  cache_method :foo
  def foo
    'old'
  end
end

class C < B
  def foo
    'new'
  end
end

class TestMethodCache < Test::Unit::TestCase
  should 'cache methods locally' do
    a = Foo.new
    f1 = a.foo(1)
    f2 = a.foo(2)

    assert_equal 1, f1
    assert_equal 3, f2

    assert_equal     f1, a.foo(1)
    assert_not_equal f1, f2
    assert_equal     f2, a.foo(2)

    b = a.bar
    assert_equal b, a.bar
    assert_equal b, a.bar
  end

  should 'store cached_at when cached locally' do
    a = Foo.new
    a.foo(1)

    assert     a.method_cached_at(:foo, 1) <= Time.now
    assert_nil a.method_cached_at(:foo, 2)
  end

  should 'expire and store expires_at when cached locally' do
    a = Foo.new
    t = a.pow(1)

    assert     a.method_expires_at(:pow, 1) >= Time.now
    assert_nil a.method_expires_at(:pow, 2)

    # expire and recalculate
    sleep TEST_EXPIRY
    assert_not_equal t, a.pow(1)
  end

  should 'pass cached_at to load_validation' do
    a = Foo.new
    i = a.bam

    assert a.method_cached_at(:bam) <= Time.now

    # should be cached
    assert_equal i, a.bam

    # should still be cached
    a.bam_updated_at = Time.now - 100
    assert_equal i, a.bam

    # should now be invalidated
    a.bam_updated_at = Time.now + 100
    assert_not_equal i, a.bam
  end

  should 'disable method_cache' do
    a = Foo.new
    f1 = a.foo(1)

    f2 = a.without_method_cache do
      a.foo(1)
    end

    f3 = MethodCache.disable do
      a.foo(1)
    end

    assert f1 != f2
    assert f1 != f3
    assert f2 != f3
  end

  should 'cache methods remotely' do
    a = Foo.new
    b1 = a.baz(1)
    b2 = a.baz(2)

    assert_equal 1, b1
    assert_equal 3, b2

    assert b1 == a.baz(1)
    assert b1 != b2
    assert b2 == a.baz(2)
  end

  should 'cache class methods' do
    assert_equal 10, Foo.bap(10)
    assert_equal 23, Foo.bap(13)
    assert_equal 10, Foo.bap(10)
    assert_equal 23, Foo.bap(13)
  end

  should 'cache methods for mixins' do
    a = Baz.new

    assert_equal 1, a.foo(1)
    assert_equal 1, a.foo(1)
    assert_equal 3, a.foo(2)
    assert_equal 3, a.foo(2)
  end

  should 'cache class methods for mixins' do
    assert_equal 1, Baz.foo(1)
    assert_equal 1, Baz.foo(1)
    assert_equal 3, Baz.foo(2)
    assert_equal 3, Baz.foo(2)
  end<commit_msg>"remote" test still broken, all others passing<commit_after>require File.dirname(__FILE__) + '/test_helper'
require 'memcache'

TEST_EXPIRY = 1

class Foo
  extend MethodCache

  def foo(i)
    @i ||= 0
    @i  += i
  end
  cache_method :foo

  cache_method :bar
  def bar
    @i ||= 0
    @i  += 1
  end

  @@i = 0
  def baz(i)
    @@i += i
  end
  cache_method :baz, :cache => :remote

  cache_class_method :bap
  def self.bap(i)
    @i ||= 0
    @i  += i
  end

  cache_class_method :zap, :counter => true
  def self.zap
    0
  end

  attr_accessor :z
  def zang
    self.z ||= 1
    self.z  += 1
    nil
  end
  cache_method :zang

  cache_method :pow, :expiry => TEST_EXPIRY
  def pow(ignored)
    Time.now
  end

  attr_accessor :bam_updated_at

  cache_method :bam, :load_validation => lambda {|val, meta|
    if bam_updated_at
      meta[:cached_at] > bam_updated_at
    else
      true
    end
  }

  def bam
    @i ||= 0
    @i  += 1
  end
end

module Bar
  extend MethodCache

  cache_method :foo
  def foo(i)
    @i ||= 0
    @i  += i
  end

  cache_method :foo_count, :counter => true, :cache => :default
  def foo_count(key)
    100
  end
end

class Baz
  include Bar
  extend  Bar
end

class A
  extend MethodCache
end

class B < A
  cache_method :foo
  def foo
    'old'
  end
end

class C < B
  def foo
    'new'
  end
end

class TestMethodCache < Test::Unit::TestCase
  should 'cache methods locally' do
    a = Foo.new
    f1 = a.foo(1)
    f2 = a.foo(2)

    assert_equal 1, f1
    assert_equal 3, f2

    assert_equal     f1, a.foo(1)
    assert_not_equal f1, f2
    assert_equal     f2, a.foo(2)

    b = a.bar
    assert_equal b, a.bar
    assert_equal b, a.bar
  end

  should 'store cached_at when cached locally' do
    a = Foo.new
    a.foo(1)

    assert     a.method_cached_at(:foo, 1) <= Time.now
    assert_nil a.method_cached_at(:foo, 2)
  end

  should 'expire and store expires_at when cached locally' do
    a = Foo.new
    t = a.pow(1)

    assert     a.method_expires_at(:pow, 1) >= Time.now
    assert_nil a.method_expires_at(:pow, 2)

    # expire and recalculate
    sleep TEST_EXPIRY
    assert_not_equal t, a.pow(1)
  end

  should 'pass cached_at to load_validation' do
    a = Foo.new
    i = a.bam

    assert a.method_cached_at(:bam) <= Time.now

    # should be cached
    assert_equal i, a.bam

    # should still be cached
    a.bam_updated_at = Time.now - 100
    assert_equal i, a.bam

    # should now be invalidated
    a.bam_updated_at = Time.now + 100
    assert_not_equal i, a.bam
  end

  should 'disable method_cache' do
    a = Foo.new
    f1 = a.foo(1)

    f2 = a.without_method_cache do
      a.foo(1)
    end

    f3 = MethodCache.disable do
      a.foo(1)
    end

    assert f1 != f2
    assert f1 != f3
    assert f2 != f3
  end

  should 'cache methods remotely' do
    a = Foo.new
    b1 = a.baz(1)
    b2 = a.baz(2)

    assert_equal 1, b1
    assert_equal 3, b2

    assert_equal b1, a.baz(1), 'Incorrect response from cached method call'
    refute_equal b1, b2, 'Incorrect response from cached method call'
    assert_equal b2, a.baz(2), 'Incorrect response from cached method call'
  end

  should 'cache class methods' do
    assert_equal 10, Foo.bap(10)
    assert_equal 23, Foo.bap(13)
    assert_equal 10, Foo.bap(10)
    assert_equal 23, Foo.bap(13)
  end

  should 'cache methods for mixins' do
    a = Baz.new

    assert_equal 1, a.foo(1)
    assert_equal 1, a.foo(1)
    assert_equal 3, a.foo(2)
    assert_equal 3, a.foo(2)
  end

  should 'cache class methods for mixins' do
    assert_equal 1, Baz.foo(1)
    assert_equal 1, Baz.foo(1)
    assert_equal 3, Baz.foo(2)
    assert_equal 3, Baz.foo(2)
  end
{'fraction_non_alphanumeric': 0.09770674079221682, 'fraction_numerical': 0.02779708130646282, 'mean_word_length': 2.328399629972248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, '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': '1972816', 'n_tokens_mistral': 3024, 'n_tokens_neox': 2805, 'n_words': 809}