From 913003eaf0456e4645cad91f2622354deae10841 Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Wed, 3 Aug 2016 12:09:19 +0300 Subject: Fix Relationship.instances cache. This PR aims to fix several issues with Relationship cache: 1) It's not threadsafe, so I propose to use a TLS variable for this. 2) Memory obtained by cache remains non-freed before the next run of `serialize`. I think it should be freed immediately. 3) Memory should be freed in `ensure` block to prevent memory bloating in case of exception. *There are only two hard things in Computer Science: cache invalidation and naming things.* --- test/rels/tc_relationship.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/rels') diff --git a/test/rels/tc_relationship.rb b/test/rels/tc_relationship.rb index add1654f..845f789e 100644 --- a/test/rels/tc_relationship.rb +++ b/test/rels/tc_relationship.rb @@ -13,6 +13,14 @@ class TestRelationships < Test::Unit::TestCase instance = Axlsx::Relationship.new(source_obj, Axlsx::WORKSHEET_R, 'target') assert_equal instance.Id, Axlsx::Relationship.new(source_obj, Axlsx::WORKSHEET_R, 'target').Id end + + def test_instances_cache_is_thread_safe + cache1, cache2 = nil + t1 = Thread.new { cache1 = Axlsx::Relationship.instances } + t2 = Thread.new { cache2 = Axlsx::Relationship.instances } + [t1, t2].each(&:join) + assert_not_same(cache1, cache2) + end def test_target_is_only_considered_for_same_attributes_check_if_target_mode_is_external source_obj = Object.new -- cgit v1.2.3