Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't treat NameError in deserialize as network error #728

Merged
merged 2 commits into from Jul 26, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -447,6 +447,9 @@ def deserialize(value, flags)
rescue ArgumentError
raise if $!.message !~ /undefined class|marshal data too short/
raise UnmarshalError, "Unable to unmarshal value: #{$!.message}"
rescue NameError
raise if $!.message !~ /uninitialized constant/
raise UnmarshalError, "Unable to unmarshal value: #{$!.message}"
rescue Zlib::Error
raise UnmarshalError, "Unable to uncompress value: #{$!.message}"
end
@@ -146,4 +146,69 @@
end.must_raise Dalli::ValueOverMaxSize
end
end

describe 'deserialize' do
subject { Dalli::Server.new('127.0.0.1') }

it 'uses Marshal as default serializer' do
assert_equal subject.serializer, Marshal
end

it 'deserializes serialized value' do
value = 'some_value'
deserialized = subject.send(:deserialize, Marshal.dump(value), Dalli::Server::FLAG_SERIALIZED)
assert_equal value, deserialized
end

it 'raises UnmarshalError for broken data' do
assert_raises Dalli::UnmarshalError do
subject.send(:deserialize, :not_marshaled_value, Dalli::Server::FLAG_SERIALIZED)
end
end

describe 'custom serializer' do
let(:serializer) { Minitest::Mock.new }
subject { Dalli::Server.new('127.0.0.1', serializer: serializer) }

it 'uses custom serializer' do
assert subject.serializer === serializer
end

it 'reraises general NameError' do
serializer.expect(:load, nil) do
raise NameError, 'ddd'
end
assert_raises NameError do
subject.send(:deserialize, :some_value, Dalli::Server::FLAG_SERIALIZED)
end
end

it 'raises UnmarshalError on uninitialized constant' do
serializer.expect(:load, nil) do
raise NameError, 'uninitialized constant Ddd'
end
assert_raises Dalli::UnmarshalError do
subject.send(:deserialize, :some_value, Dalli::Server::FLAG_SERIALIZED)
end
end

it 'reraises general ArgumentError' do
serializer.expect(:load, nil) do
raise ArgumentError, 'ddd'
end
assert_raises ArgumentError do
subject.send(:deserialize, :some_value, Dalli::Server::FLAG_SERIALIZED)
end
end

it 'raises UnmarshalError on undefined class' do
serializer.expect(:load, nil) do
raise ArgumentError, 'undefined class Ddd'
end
assert_raises Dalli::UnmarshalError do
subject.send(:deserialize, :some_value, Dalli::Server::FLAG_SERIALIZED)
end
end
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.