From 6a5a814eaa0d4f98d6151df5bd96cc8ec1ae5675 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Thu, 25 May 2017 09:08:45 +0930 Subject: Add a Monitor to ModelSchema#load_schema [Vikrant Chaudhary, David Abdemoulaie, Matthew Draper] --- .../test/cases/serialized_attribute_test.rb | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'activerecord/test/cases/serialized_attribute_test.rb') diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index 673392b4c4..e1bdaab5cf 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -349,4 +349,32 @@ class SerializedAttributeTest < ActiveRecord::TestCase topic.foo refute topic.changed? end + + def test_serialized_attribute_works_under_concurrent_initial_access + model = Topic.dup + + topic = model.last + topic.update group: "1" + + model.serialize :group, JSON + model.reset_column_information + + # This isn't strictly necessary for the test, but a little bit of + # knowledge of internals allows us to make failures far more likely. + model.define_singleton_method(:define_attribute) do |*args| + Thread.pass + super(*args) + end + + threads = 4.times.map do + Thread.new do + topic.reload.group + end + end + + # All the threads should retrieve the value knowing it is JSON, and + # thus decode it. If this fails, some threads will instead see the + # raw string ("1"), or raise an exception. + assert_equal [1] * threads.size, threads.map(&:value) + end end -- cgit v1.2.3