Skip to content

Commit

Permalink
Add support for memory usage ... command (#301)
Browse files Browse the repository at this point in the history
`MEMORY USAGE key` currently results in `NoMethodError: undefined method
'memory' for #<MockRedis::Database...`

This change will return a hardcoded numeric value

```console
> m = MockRedis.new
> m.call(["memory", "usage", "foo"])
=> nil

> m.set("foo", "anything")
=> "OK"

> m.call(["memory", "usage", "foo"])
=> 160
```
  • Loading branch information
jasonpenny authored Jan 24, 2024
1 parent bc05694 commit 2665966
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/mock_redis/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'mock_redis/geospatial_methods'
require 'mock_redis/stream_methods'
require 'mock_redis/connection_method'
require 'mock_redis/memory_method'

class MockRedis
class Database
Expand All @@ -26,6 +27,7 @@ class Database
include GeospatialMethods
include StreamMethods
include ConnectionMethod
include MemoryMethod

attr_reader :data, :expire_times

Expand Down
11 changes: 11 additions & 0 deletions lib/mock_redis/memory_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MockRedis
module MemoryMethod
def memory(usage, key = nil, *_options)
raise ArgumentError, "unhandled command `memory #{usage}`" if usage != 'usage'

return nil unless @data.key?(key)

160
end
end
end
23 changes: 23 additions & 0 deletions spec/commands/memory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'spec_helper'

RSpec.describe '#memory usage [mock only]' do
it 'only handles the usage subcommand' do
expect { @redises.mock.call(%w[memory stats]) }.to raise_error(ArgumentError)
end

context 'when the key does not exist' do
before { @redises.real.del('foo') }

it 'returns nil' do
expect(@redises.call(%w[memory usage foo])).to be_nil
end
end

context 'when the key does exist' do
before { @redises.set('foo', 'a' * 100) }

it 'returns the memory usage' do
expect(@redises.call(%w[memory usage foo])).to be_a(Integer)
end
end
end

0 comments on commit 2665966

Please sign in to comment.