Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (87 sloc) 2.488 kb
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
1 require "spec/helper/all"
2 require "em-synchrony/activerecord"
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
3 require "em-synchrony/fiber_iterator"
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
4
d2ff2e3 @dgutov Create the widgets table
dgutov authored
5 # mysql < spec/widgets.sql
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
6
7 class Widget < ActiveRecord::Base; end;
8
9 describe "Fiberized ActiveRecord driver for mysql2" do
10 DELAY = 0.25
11 QUERY = "SELECT sleep(#{DELAY})"
12
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
13 def establish_connection
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
14 ActiveRecord::Base.establish_connection(
15 :adapter => 'em_mysql2',
16 :database => 'widgets',
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
17 :username => 'root',
ad28cde @prepor Fiber-local caching logic moved to AR ConnectionPool subclass
prepor authored
18 :pool => 10
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
19 )
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
20 Widget.delete_all
21 end
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
22
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
23 it "should establish AR connection" do
24 EventMachine.synchrony do
25 establish_connection
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
26
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
27 result = Widget.find_by_sql(QUERY)
28 result.size.should eql(1)
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
29 EventMachine.stop
30 end
31 end
32
33 it "should fire sequential, synchronous requests within single fiber" do
34 EventMachine.synchrony do
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
35 establish_connection
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
36
37 start = now
38 res = []
39
40 res.push Widget.find_by_sql(QUERY)
41 res.push Widget.find_by_sql(QUERY)
42
43 (now - start.to_f).should be_within(DELAY * res.size * 0.15).of(DELAY * res.size)
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
44 res.size.should eql(2)
86feed2 @igrigorik activerecord adapter for em_mysql2
authored
45
46 EventMachine.stop
47 end
48 end
49
0887f8a @prepor Fiber aware ActiveRecord which works not only in FiberPool
prepor authored
50 it "should fire 100 requests in fibers" do
51 EM.synchrony do
52 establish_connection
53 EM::Synchrony::FiberIterator.new(1..100, 40).each do |i|
54 widget = Widget.create title: 'hi'
55 widget.update_attributes title: 'hello'
56 end
57 EM.stop
58 end
59 end
60
61 it "should create widget" do
62 EM.synchrony do
63 establish_connection
64 Widget.create
65 Widget.create
66 Widget.count.should eql(2)
67 EM.stop
68 end
69 end
70
71 it "should update widget" do
72 EM.synchrony do
73 establish_connection
74 ActiveRecord::Base.connection.execute("TRUNCATE TABLE widgets;")
75 widget = Widget.create title: 'hi'
76 widget.update_attributes title: 'hello'
77 Widget.find(widget.id).title.should eql('hello')
78 EM.stop
79 end
80 end
81
82 describe "transactions" do
83 it "should work properly" do
84 EM.synchrony do
85 establish_connection
86 EM::Synchrony::FiberIterator.new(1..50, 30).each do |i|
87 widget = Widget.create title: "hi#{i}"
88 ActiveRecord::Base.transaction do
89 widget.update_attributes title: "hello"
90 end
91 ActiveRecord::Base.transaction do
92 raise ActiveRecord::Rollback
93 end
94 end
95 Widget.all.each do |widget|
96 widget.title.should eq('hello')
97 end
98 EM.stop
99 end
100 end
101 end
102
d2ff2e3 @dgutov Create the widgets table
dgutov authored
103 end
Something went wrong with that request. Please try again.