Skip to content

Commit

Permalink
Add test coverage for RedisEventStore and resque approriately, fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
awead committed Mar 4, 2016
1 parent 8bef322 commit 507aa45
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 20 deletions.
46 changes: 26 additions & 20 deletions lib/sufia/redis_event_store.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
module Sufia
class RedisEventStore
class << self
def for(key)
new(key)
end

# @return [Fixnum] the id of the event
def create(action, timestamp)
event_id = Redis.current.incr("events:latest_id")
Redis.current.hmset("events:#{event_id}", "action", action, "timestamp", timestamp)
event_id
rescue Redis::CommandError => e
logger.error("unable to create event: #{e}")
nil
end

def logger
@logger = (defined?(Rails) && Rails.logger) || CurationConcerns::NullLogger.new
end
end

def initialize(key)
@key = key
end

def fetch(size)
Redis.current.lrange(@key, 0, size).map do |event_id|
{
action: Redis.current.hget("events:#{event_id}", "action"),
timestamp: Redis.current.hget("events:#{event_id}", "timestamp")
}
end
rescue
rescue Redis::CommandError
[]
end

# Adds a value to the end of a list identified by key
def push(value)
Redis.current.lpush(@key, value)
rescue
nil
end

def initialize(key)
@key = key
end

def self.for(key)
new(key)
end

# @return [Fixnum] the id of the event
def self.create(action, timestamp)
event_id = Redis.current.incr("events:latest_id")
Redis.current.hmset("events:#{event_id}", "action", action, "timestamp", timestamp)
event_id
rescue => e
logger.error("unable to create event: #{e}") if logger
rescue Redis::CommandError
nil
end
end
Expand Down
43 changes: 43 additions & 0 deletions spec/lib/sufia/redis_event_store_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'

describe Sufia::RedisEventStore do
before do
Redis.current.keys('events:*').each { |key| Redis.current.del key }
end

describe "::create" do
subject { described_class.create("some action", "1234") }
context "when it is successful" do
it { is_expected.to eq(1) }
end
context "when the Redis command fails" do
before { allow(Redis).to receive(:current).and_raise(Redis::CommandError) }
context "without a logger" do
before { allow(Rails).to receive(:logger).and_return(false) }
it { is_expected.to be_nil }
end
context "with a logger" do
it "logs the error" do
expect(Rails.logger).to receive(:error).exactly(:once).with("unable to create event: Redis::CommandError")
expect(subject).to be_nil
end
end
end
end

describe "#fetch" do
subject { described_class.new("key").fetch("size") }
context "when the Redis command fails" do
before { allow(Redis).to receive(:current).and_raise(Redis::CommandError) }
it { is_expected.to eq([]) }
end
end

describe "#push" do
subject { described_class.new("key").push("some value") }
context "when the Redis command fails" do
before { allow(Redis).to receive(:current).and_raise(Redis::CommandError) }
it { is_expected.to be_nil }
end
end
end

0 comments on commit 507aa45

Please sign in to comment.