<commit_before>require 'test_helper'

class SpymemcachedTest < Test::Unit::TestCase
  def setup
    @client = binary_client
    @client.flush_all
  end

  def test_expiry
    @client.add('add_key1', 'v1', 0)
    @client.add('add_key2', 'v2', 1)

    @client.set('set_key1', 'v1', 0)
    @client.set('set_key2', 'v2', 1)

    @client.fetch('fetch_key1', 0) { 'v1' }
    @client.fetch('fetch_key2', 1) { 'v2' }

    @client.add('cas_key1', 'v0')
    @client.add('cas_key2', 'v0')
    @client.cas('cas_key1', 0) { 'v1' }
    @client.cas('cas_key2', 1) { 'v2' }

    @client.add('replace_key1', 'v0')
    @client.add('replace_key2', 'v0')
    @client.replace('replace_key1', 'v1', 0)
    @client.replace('replace_key2', 'v2', 1)

    @client.add('touch_key1', 'v1', 1)

    sleep 0.5
    @client.touch('touch_key1')

    sleep 0.7
    assert_equal 'v1', @client['touch_key1']

    assert_equal 'v1', @client.get('add_key1')
    assert_nil @client.get('add_key2')

    assert_equal 'v1', @client.get('set_key1')
    assert_nil @client.get('set_key2')

    assert_equal 'v1', @client.get('fetch_key1')
    assert_nil @client.get('fetch_key2')

    assert_equal 'v1', @client.get('cas_key1')
    assert_nil @client.get('cas_key2')

    assert_equal 'v1', @client.get('replace_key1')
    assert_nil @client.get('replace_key2')
  end

  def test_timeout
    @client = Spymemcached.new('localhost:11111', :timeout => 0.01)
    assert_timeout = lambda do |action, *args|
      start_at = Time.now
      assert_raise Spymemcached::TimeoutError do
        @client.send(action, *args) { 'v1' }
      end
      time = Time.now - start_at
      assert time < 0.025, "Timeout is 0.01, actual time of action #{action} when timeout: #{time}"
    end
<commit_msg>refactoring expiry test to void random failure<commit_after>require 'test_helper'

class SpymemcachedTest < Test::Unit::TestCase
  def setup
    @client = binary_client
    @client.flush_all
  end

  def test_expiry
    @client.add('add_key1', 'v1', 0)
    @client.add('add_key2', 'v2', 2)

    @client.set('set_key1', 'v1', 0)
    @client.set('set_key2', 'v2', 2)

    @client.fetch('fetch_key1', 0) { 'v1' }
    @client.fetch('fetch_key2', 2) { 'v2' }

    @client.add('cas_key1', 'v0')
    @client.add('cas_key2', 'v0')
    @client.cas('cas_key1', 0) { 'v1' }
    @client.cas('cas_key2', 2) { 'v2' }

    @client.add('replace_key1', 'v0')
    @client.add('replace_key2', 'v0')
    @client.replace('replace_key1', 'v1', 0)
    @client.replace('replace_key2', 'v2', 2)

    @client.add('touch_key1', 'v1', 2)

    sleep 0.1

    assert_equal 'v2', @client.get('add_key2')
    assert_equal 'v2', @client.get('set_key2')
    assert_equal 'v2', @client.get('fetch_key2')
    assert_equal 'v2', @client.get('cas_key2')
    assert_equal 'v2', @client.get('replace_key2')

    assert @client.touch('touch_key1')

    sleep 2
    assert_equal 'v1', @client['touch_key1']

    assert_equal 'v1', @client.get('add_key1')
    assert_nil @client.get('add_key2')

    assert_equal 'v1', @client.get('set_key1')
    assert_nil @client.get('set_key2')

    assert_equal 'v1', @client.get('fetch_key1')
    assert_nil @client.get('fetch_key2')

    assert_equal 'v1', @client.get('cas_key1')
    assert_nil @client.get('cas_key2')

    assert_equal 'v1', @client.get('replace_key1')
    assert_nil @client.get('replace_key2')
  end

  def test_timeout
    @client = Spymemcached.new('localhost:11111', :timeout => 0.01)
    assert_timeout = lambda do |action, *args|
      start_at = Time.now
      assert_raise Spymemcached::TimeoutError do
        @client.send(action, *args) { 'v1' }
      end
      time = Time.now - start_at
      assert time < 0.025, "Timeout is 0.01, actual time of action #{action} when timeout: #{time}"
    end

{'fraction_non_alphanumeric': 0.17308202813910273, 'fraction_numerical': 0.044332359968144414, 'mean_word_length': 3.6461159062885327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7455434', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1449, 'n_words': 274}