lib/mock_redis/stream.rb in mock_redis-0.24.0 vs lib/mock_redis/stream.rb in mock_redis-0.25.0
- old
+ new
@@ -21,18 +21,33 @@
@last_id.to_s
end
def add(id, values)
@last_id = MockRedis::Stream::Id.new(id, min: @last_id)
+ if @last_id.to_s == '0-0'
+ raise Redis::CommandError,
+ 'ERR The ID specified in XADD is equal or smaller than ' \
+ 'the target stream top item'
+ # TOOD: Redis version 6.0.4, w redis 4.2.1 generates the following error message:
+ # 'ERR The ID specified in XADD must be greater than 0-0'
+ end
members.add [@last_id, Hash[values.map { |k, v| [k.to_s, v.to_s] }]]
@last_id.to_s
end
def trim(count)
deleted = @members.size - count
- @members = @members.to_a[-count..-1].to_set
- deleted
+ if deleted > 0
+ @members = if count == 0
+ Set.new
+ else
+ @members.to_a[-count..-1].to_set
+ end
+ deleted
+ else
+ 0
+ end
end
def range(start, finish, reversed, *opts_in)
opts = options opts_in, ['count']
start_id = MockRedis::Stream::Id.new(start)
@@ -41,9 +56,14 @@
.select { |m| (start_id <= m[0]) && (finish_id >= m[0]) }
.map { |m| [m[0].to_s, m[1]] }
items.reverse! if reversed
return items.first(opts['count'].to_i) if opts.key?('count')
items
+ end
+
+ def read(id)
+ stream_id = MockRedis::Stream::Id.new(id)
+ members.select { |m| (stream_id < m[0]) }.map { |m| [m[0].to_s, m[1]] }
end
def each
members.each { |m| yield m }
end