Module: ActiveRecord::AdvisoryLock::Etest

Defined in:
lib/vex/active_record/advisory_lock/sqlite_adapter.rb

Overview

Note: these tests are un within multiple threads of a single application. This is not the usual intention of locking code: AbstractAdapter#lock should be used to lock across processes!

Consequently locking does not work on in-memory sqlite databases.

Defined Under Namespace

Classes: Test

Instance Method Summary collapse

Instance Method Details

#test_sqliteObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vex/active_record/advisory_lock/sqlite_adapter.rb', line 24

def test_sqlite
  @run = 0

  connection = Test.connection

  #
  # In this example th2 would run first, because th1 goes to sleep
  # first.
  th1 = Thread.new { 
    Thread.sleep 0.1
    connection.locked("etest") do
      assert_equal(2, @run)
      @run = 1
    end
  }

  th2 = Thread.new { 
    Thread.sleep 0.05
    connection.locked("etest") do
      @run = 2
    end
  }

  th1.join
  th2.join
  
  assert_equal(1, @run)

  #
  # In this example th1 would lock using the connection before th2 tries to
  # lock it. Consequenty the th1 action would run first.
  @run = 0
  
  th1 = Thread.new { 
    connection.locked("etest") do
      Thread.sleep 0.1
      assert_equal(0, @run)
      @run = 1
    end
  }

  th2 = Thread.new { 
    Thread.sleep 0.05
    connection.locked("etest") do
      assert_equal(1, @run)
      @run = 2
    end
  }

  th1.join
  th2.join
  
  assert_equal(2, @run)
end