2010-04-14 19:55:27 +00:00
|
|
|
# encoding: UTF-8
|
2010-07-06 22:41:44 +00:00
|
|
|
require 'spec_helper'
|
2010-04-14 19:55:27 +00:00
|
|
|
require 'mysql2/em'
|
|
|
|
|
|
|
|
describe Mysql2::EM::Client do
|
|
|
|
it "should support async queries" do
|
|
|
|
results = []
|
|
|
|
EM.run do
|
|
|
|
client1 = Mysql2::EM::Client.new
|
2010-09-27 20:25:10 +00:00
|
|
|
defer1 = client1.query "SELECT sleep(0.1) as first_query"
|
2010-04-14 19:55:27 +00:00
|
|
|
defer1.callback do |result|
|
|
|
|
results << result.first
|
|
|
|
EM.stop_event_loop
|
|
|
|
end
|
|
|
|
|
|
|
|
client2 = Mysql2::EM::Client.new
|
|
|
|
defer2 = client2.query "SELECT sleep(0.025) second_query"
|
|
|
|
defer2.callback do |result|
|
|
|
|
results << result.first
|
|
|
|
end
|
|
|
|
end
|
2010-09-27 13:46:30 +00:00
|
|
|
|
2010-04-14 19:55:27 +00:00
|
|
|
results[0].keys.should include("second_query")
|
|
|
|
results[1].keys.should include("first_query")
|
|
|
|
end
|
2010-09-27 13:46:30 +00:00
|
|
|
|
|
|
|
it "should support queries in callbacks" do
|
|
|
|
results = []
|
|
|
|
EM.run do
|
|
|
|
client = Mysql2::EM::Client.new
|
|
|
|
defer1 = client.query "SELECT sleep(0.025) as first_query"
|
|
|
|
defer1.callback do |result|
|
|
|
|
results << result.first
|
|
|
|
defer2 = client.query "SELECT sleep(0.025) as second_query"
|
|
|
|
defer2.callback do |result|
|
|
|
|
results << result.first
|
|
|
|
EM.stop_event_loop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
results[0].keys.should include("first_query")
|
|
|
|
results[1].keys.should include("second_query")
|
|
|
|
end
|
2010-07-06 22:41:44 +00:00
|
|
|
end
|