Sha256: 2b51940a069d3bae1f591cead3413eeb13bc9e2d04fa67e0cb966a2463ae8f48

Contents?: true

Size: 1.55 KB

Versions: 11

Compression:

Stored size: 1.55 KB

Contents

# -*- coding: utf-8 -*-
require 'spec_helper'

describe Tengine::Core::OptimisticLock do

  class Tengine::Core::OptimisticLockTestBox1
    include Mongoid::Document
    include Tengine::Core::OptimisticLock

    set_locking_field :version

    field :version, :type => Integer
    field :value, :type => String
  end

  context "update_with_lock" do
    before do
      @test_box1 = Tengine::Core::OptimisticLockTestBox1.create!(:version => 2, :value => "foo")
    end

    it "競合がなければ素直に更新する" do
      test_box = Tengine::Core::OptimisticLockTestBox1.find(@test_box1.id)
      test_box.update_with_lock do
        test_box.value += "o"
      end
      test_box.reload
      test_box.value.should == "fooo"
      test_box.version.should == 3
    end

    it "競合しても単純に上書きしたりせず、最新を取得し直して更新する" do
      test_box1 = Tengine::Core::OptimisticLockTestBox1.find(@test_box1.id)
      test_box2 = Tengine::Core::OptimisticLockTestBox1.find(@test_box1.id)
      # test_box1を更新
      test_box1_count = 0
      test_box1.update_with_lock do
        test_box1_count += 1
        test_box1.value += "o"
      end
      test_box1_count.should == 1
      test_box1.value.should == "fooo"
      test_box1.version.should == 3
      # test_box2を更新
      test_box2_count = 0
      test_box2.update_with_lock do
        test_box2_count += 1
        test_box2.value += "w"
      end
      test_box2_count.should == 2
      test_box2.value.should == "fooow"
      test_box2.version.should == 4
    end
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tengine_core-0.5.39 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.38 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.37 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.36 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.35 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.34 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.33 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.32 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.31 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.30 spec/tengine/core/optimistic_lock_spec.rb
tengine_core-0.5.28 spec/tengine/core/optimistic_lock_spec.rb