From 69645cba727dfa1c18c666d2a2f1c0dedffde938 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 12 Feb 2018 14:16:41 -0500 Subject: Simplify the implementation of custom argument serializers We can speed up things for the supported types by keeping the code in the way it was. We can also avoid to loop trough all serializers in the deserialization by trying to access the class already in the Hash. We could also speed up the custom serialization if we define the class that is going to be serialized when registering the serializers, but that will remove the possibility of defining a serialzer for a superclass and have the subclass serialized using it. --- activejob/test/cases/argument_serialization_test.rb | 2 +- activejob/test/cases/serializers_test.rb | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'activejob/test') diff --git a/activejob/test/cases/argument_serialization_test.rb b/activejob/test/cases/argument_serialization_test.rb index 8f51a2d238..4e26b9a178 100644 --- a/activejob/test/cases/argument_serialization_test.rb +++ b/activejob/test/cases/argument_serialization_test.rb @@ -14,7 +14,7 @@ class ArgumentSerializationTest < ActiveSupport::TestCase [ nil, 1, 1.0, 1_000_000_000_000_000_000_000, "a", true, false, BigDecimal.new(5), :a, 1.day, Date.new(2001, 2, 3), Time.new(2002, 10, 31, 2, 2, 2, "+02:00"), - DateTime.new(2001, 2, 3, 4, 5, 6, '+03:00'), + DateTime.new(2001, 2, 3, 4, 5, 6, "+03:00"), ActiveSupport::TimeWithZone.new(Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone["UTC"]), [ 1, "a" ], { "a" => 1 } diff --git a/activejob/test/cases/serializers_test.rb b/activejob/test/cases/serializers_test.rb index 207ae55b49..a86f168d03 100644 --- a/activejob/test/cases/serializers_test.rb +++ b/activejob/test/cases/serializers_test.rb @@ -58,7 +58,24 @@ class SerializersTest < ActiveSupport::TestCase test "won't deserialize unknown hash" do hash = { "_dummy_serializer" => 123, "_aj_symbol_keys" => [] } - assert_equal({ "_dummy_serializer" => 123 }, ActiveJob::Serializers.deserialize(hash)) + error = assert_raises(ArgumentError) do + ActiveJob::Serializers.deserialize(hash) + end + assert_equal( + 'Serializer name is not present in the argument: {"_dummy_serializer"=>123, "_aj_symbol_keys"=>[]}', + error.message + ) + end + + test "won't deserialize unknown serializer" do + hash = { "_aj_serialized" => "DoNotExist", "value" => 123 } + error = assert_raises(ArgumentError) do + ActiveJob::Serializers.deserialize(hash) + end + assert_equal( + "Serializer DoNotExist is not know", + error.message + ) end test "will deserialize know serialized objects" do @@ -74,7 +91,7 @@ class SerializersTest < ActiveSupport::TestCase test "can't add serializer with the same key twice" do ActiveJob::Serializers.add_serializers DummySerializer - assert_no_difference(-> { ActiveJob::Serializers.serializers.size } ) do + assert_no_difference(-> { ActiveJob::Serializers.serializers.size }) do ActiveJob::Serializers.add_serializers DummySerializer end end -- cgit v1.2.3