Created
January 9, 2011 10:12
-
-
Save elskwid/771579 to your computer and use it in GitHub Desktop.
Test the optimistic locking with Sequel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sequel' | |
require 'sqlite3' | |
require 'logger' | |
DB = Sequel.sqlite '', :loggers => [Logger.new($stdout)] | |
Sequel::Model.plugin :instance_filters | |
Sequel::Model.plugin :optimistic_locking | |
DB.create_table! :entries do | |
primary_key :id | |
String :title | |
String :description | |
String :notes | |
Integer :lock_version, :default => 0 | |
end | |
class Entry < Sequel::Model | |
many_to_one :office | |
end | |
def create_entry() | |
entry = Entry.new | |
entry.title = "This is a title" | |
entry.description = "This is a description" | |
entry.notes = "This is a note" | |
entry.save | |
end | |
def concurrency_test(i) | |
e1 = Entry[1] | |
e2 = Entry[1] | |
puts "Before updates:" | |
puts "Lock_version of e1 is #{e1.lock_version}" | |
puts "Lock_version of e2 is #{e2.lock_version}" | |
e1.update(:title=>"This is an updated title #{i}") | |
puts "After update to e1:" | |
puts "Lock_version of e1 is #{e1.lock_version}" | |
puts "Lock_version of e2 is #{e2.lock_version}" | |
e2.update(:title=>"This should not work") | |
puts "After update to e2:" | |
puts "Lock_version of e1 is #{e1.lock_version}" | |
puts "Lock_version of e2 is #{e2.lock_version}" | |
end | |
create_entry | |
10.times.each do |i| | |
begin | |
puts "\n**Run Tests**" | |
concurrency_test(i) | |
rescue Sequel::NoExistingObject | |
puts " Caught the lock error!" | |
ensure | |
sleep 5 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment