Files
mozo-backend/spec/support/in_memory_q_counter.rb
T
2014-07-28 17:33:40 +02:00

52 lines
1007 B
Ruby

# This is a non thread safe replacement for the
# couchbase counter mechanism since every test needs
# a clean start and Hash#clear is soooo much faster than
# a couchbase bucket flush
class InMemoryQCounter
attr_reader :store
def initialize
@store = {}
end
def get(key, options = {})
store[key]
end
def set(key, value)
store[key] = value
end
def incr(key, options = {})
# store[key] ||= options[:initial].to_i
# store[key] += 1
if store[key]
store[key] += 1
else
store[key] = options[:initial].to_i
end
end
def decr(key, options = {})
# store[key] ||= options[:initial].to_i
# store[key] -= 1
if store[key]
store[key] -= 1
else
store[key] = options[:initial].to_i
end
end
def flush
store.clear
end
end
=begin Make drb server
require 'drb'
DRb.start_service 'druby://:9000', InMemoryQCounter.new
puts "Counter server running at #{DRb.uri}"
trap("INT") { DRb.stop_service }
DRb.thread.join
=end