diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3359e16..03f2731 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -76,7 +76,11 @@ class Enterprise < Organization end after_save do |e| - e.products.each{ |p| p.enterprise_updated(e) } + e.delay.update_products_position + end + + def update_products_position + products.each{ |p| p.enterprise_updated(self) } end def closed? diff --git a/test/integration/performance_test.rb b/test/integration/performance_test.rb index 176cf6b..15e8b44 100644 --- a/test/integration/performance_test.rb +++ b/test/integration/performance_test.rb @@ -51,6 +51,26 @@ class PerformanceTest < ActionController::IntegrationTest assert a1 > a2*2, "#{a1} should be larger than #{a2} by at least a factor of 2" end + should 'not have a linear increase in time to save enterprise due to amount of products' do + enterprise0 = Enterprise.create!(:name => 'Enterprise 0', :identifier => 'enterprise0') + enterprise1 = Enterprise.create!(:name => 'Enterprise 1', :identifier => 'enterprise1') + enterprise2 = Enterprise.create!(:name => 'Enterprise 2', :identifier => 'enterprise2') + + create_products(enterprise1,25) + create_products(enterprise2,50) + + enterprise0.reload + enterprise1.reload + enterprise2.reload + + time0 = (Benchmark.measure { 10.times { enterprise0.save! } }).total + time1 = (Benchmark.measure { 10.times { enterprise1.save! } }).total + time2 = (Benchmark.measure { 10.times { enterprise2.save! } }).total + + assert (time1 - time0) < time0*0.5 + assert (time2 - time0) < time0*0.5 + end + protected def create_profile(name) @@ -68,5 +88,13 @@ class PerformanceTest < ActionController::IntegrationTest end end + def create_products(enterprise,n) + number = Product.all.count + pc = ProductCategory.create!(:name => "Any Category #{n}", :environment => Environment.default) + n.times do |i| + Product.create!(:enterprise => enterprise, :product_category => pc, :name => "Product #{i+number+1}") + end + end + end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index e633489..0f8a44d 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -109,7 +109,7 @@ class ProductTest < Test::Unit::TestCase prod = ent.products.create!(:name => 'test product', :product_category => @product_category) ent.lat = 45.0; ent.lng = 45.0; ent.save! - + process_delayed_job_queue prod.reload assert_in_delta 45.0, prod.lat, 0.0001 -- libgit2 0.21.2