Sha256: 8f114b5761f2cea847414f3ff4aebd91514424deb5b01fac3adedeec7495ce1c

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

require 'redis_dedupe'
require 'mock_redis'
require 'spec_helper'

describe RedisDedupe::Set do
  it "is initialized with a redis client and key" do
    dedupe = RedisDedupe::Set.new(:redis, :key)
    expect(dedupe.key).to eq(:key)
  end

  it "defaults expires_in to 7 days" do
    dedupe = RedisDedupe::Set.new(:redis, :key)
    expect(dedupe.expires_in.to_i).to eq((Time.now + (7*24*60*60)).to_i)
  end

  it "optionally receives an expires_in time" do
    dedupe = RedisDedupe::Set.new(:redis, :key, (Time.now + (7*24*60)).to_i)
    expect(dedupe.expires_in.to_i).to eq((Time.now + (7*24*60)).to_i)
  end
end

describe RedisDedupe::Set, "#check" do
  it "prevents a block from yielding multiple times for the same member" do
    dedupe1 = RedisDedupe::Set.new(MockRedis.new, 'spec_key:1')
    dedupe2 = RedisDedupe::Set.new(MockRedis.new, 'spec_key:2')

    @results = []

    dedupe1.check('1') { @results << 'A' }
    dedupe1.check('1') { @results << 'B' }
    dedupe2.check('1') { @results << 'C' }

    expect(@results).to eq(['A', 'C'])
  end

  it "sets the set to expire so it cleans up if the process never completes" do
    redis  = MockRedis.new
    dedupe = RedisDedupe::Set.new(redis, 'spec_key:1', 10)

    dedupe.check('1') {  }

    expect(redis.ttl 'spec_key:1').to be_within(1).of(10)
  end
end

describe RedisDedupe::Set, "#finish" do
  it "removes the set to free up memory" do
    redis  = MockRedis.new
    dedupe = RedisDedupe::Set.new(redis, 'spec_key:1')

    dedupe.check('1') {  }
    dedupe.finish

    expect(redis.exists 'spec_key:1').to be(false)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_dedupe-0.0.2 spec/redis_dedupe_spec.rb