Sha256: cc7e4cf7fe32b4d8df4b43ab7a5ab00b96f2d461ca761d6b35df9f79b5e878fa

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

require 'spec_helper'
require 'support/time_machine/structure'

describe 'models with counter cache' do
  include ChronoTest::TimeMachine::Helpers

  adapter.create_table 'sections', temporal: true, no_journal: %w( articles_count ) do |t|
    t.string :name
    t.integer :articles_count, default: 0
  end

  adapter.create_table 'articles', temporal: true do |t|
    t.string :title
    t.references :section
  end

  class ::Section < ActiveRecord::Base
    include ChronoModel::TimeMachine

    has_many :articles
  end

  class ::Article < ActiveRecord::Base
    include ChronoModel::TimeMachine

    belongs_to :section, counter_cache: true
  end

  describe 'are not subject to race condition if no_journal is set on the counter cache column' do
    specify do
      section = Section.create!

      expect(section.articles_count).to eq(0)
      Article.create!(section_id: section.id)
      expect(section.reload.articles_count).to eq(1)

      num_threads = 10

      expect {
        Array.new(num_threads).map do
          Thread.new { Article.create!(section_id: section.id) }
        end.each(&:join)
      }.to_not raise_error
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
chrono_model-1.2.2 spec/chrono_model/time_machine/counter_cache_race_spec.rb
chrono_model-1.2.1 spec/chrono_model/time_machine/counter_cache_race_spec.rb