diff --git a/lib/common/cache_file_store.rb b/lib/common/cache_file_store.rb index eeeaf7aa..fb4d67b5 100644 --- a/lib/common/cache_file_store.rb +++ b/lib/common/cache_file_store.rb @@ -35,11 +35,9 @@ class CacheFileStore end def read_entry(key) - entry_file_path = get_entry_file_path(key) - - if File.exists?(entry_file_path) - return @serializer.load(File.read(entry_file_path)) - end + @serializer.load(File.read(get_entry_file_path(key))) + rescue + nil end def write_entry(key, data_to_store, cache_ttl) diff --git a/spec/lib/common/cache_file_store_spec.rb b/spec/lib/common/cache_file_store_spec.rb index 4bce8a10..1fa3523a 100644 --- a/spec/lib/common/cache_file_store_spec.rb +++ b/spec/lib/common/cache_file_store_spec.rb @@ -41,9 +41,25 @@ describe CacheFileStore do end end - describe '#read_entry (nonexistent entry)' do - it 'should return nil' do - expect(@cache.read_entry(Digest::SHA1.hexdigest('hello world'))).to be_nil + describe '#read_entry' do + after { expect(@cache.read_entry(key)).to eq @expected } + + context 'when the entry does not exist' do + let(:key) { Digest::SHA1.hexdigest('hello world') } + + it 'should return nil' do + @expected = nil + end + end + + context 'when the file exist but is empty (marshal data too short error)' do + let(:key) { 'empty-file' } + + it 'returns nil' do + File.new(File.join(@cache.storage_path, key), File::CREAT) + + @expected = nil + end end end