Skip to content

Commit

Permalink
Merge pull request #24123 from mperham/master
Browse files Browse the repository at this point in the history
Job payload should be symmetric across JSON dump/load
  • Loading branch information
rafaelfranca committed Mar 9, 2016
2 parents 1e92fe7 + 8c98186 commit 0934b9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions activejob/lib/active_job/core.rb
Expand Up @@ -79,7 +79,7 @@ def serialize
'queue_name' => queue_name,
'priority' => priority,
'arguments' => serialize_arguments(arguments),
'locale' => I18n.locale
'locale' => I18n.locale.to_s
}
end

Expand Down Expand Up @@ -108,7 +108,7 @@ def deserialize(job_data)
self.queue_name = job_data['queue_name']
self.priority = job_data['priority']
self.serialized_arguments = job_data['arguments']
self.locale = job_data['locale'] || I18n.locale
self.locale = job_data['locale'] || I18n.locale.to_s
end

private
Expand Down
23 changes: 19 additions & 4 deletions activejob/test/cases/job_serialization_test.rb
Expand Up @@ -2,6 +2,7 @@
require 'jobs/gid_job'
require 'jobs/hello_job'
require 'models/person'
require 'json'

class JobSerializationTest < ActiveSupport::TestCase
setup do
Expand All @@ -15,18 +16,32 @@ class JobSerializationTest < ActiveSupport::TestCase
end

test 'serialize includes current locale' do
assert_equal :en, HelloJob.new.serialize['locale']
assert_equal 'en', HelloJob.new.serialize['locale']
end

test 'serialize and deserialize are symmetric' do
# Round trip a job in memory only
h1 = HelloJob.new
h1.deserialize(h1.serialize)

# Now verify it's identical to a JSON round trip.
# We don't want any non-native JSON elements in the job hash,
# like symbols.
payload = JSON.dump(h1.serialize)
h2 = HelloJob.new
h2.deserialize(JSON.load(payload))
assert_equal h1.serialize, h2.serialize
end

test 'deserialize sets locale' do
job = HelloJob.new
job.deserialize 'locale' => :es
assert_equal :es, job.locale
job.deserialize 'locale' => 'es'
assert_equal 'es', job.locale
end

test 'deserialize sets default locale' do
job = HelloJob.new
job.deserialize({})
assert_equal :en, job.locale
assert_equal 'en', job.locale
end
end

0 comments on commit 0934b9e

Please sign in to comment.