Allow tests to run with either the native Ruby or C based Mysql#async_query

This commit is contained in:
Lourens Naude 2008-09-06 17:17:06 +01:00
parent c695d4d842
commit 70c58ce67a
5 changed files with 92 additions and 34 deletions

View File

@ -756,13 +756,32 @@ static VALUE socket(VALUE obj)
return INT2NUM(m->net.fd); return INT2NUM(m->net.fd);
} }
/* send_query(sql,timeout=nil) */ /* readable(timeout=nil) */
static VALUE send_query(int argc, VALUE* argv, VALUE obj) static VALUE readable( int argc, VALUE* argv, VALUE obj )
{ {
MYSQL* m = GetHandler(obj); MYSQL* m = GetHandler(obj);
VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout); VALUE timeout;
rb_scan_args(argc, argv, "01", &timeout);
if ( NIL_P( timeout ) ){
timeout = m->net.read_timeout;
}
if( vio_poll_read( m->net.vio, INT2NUM(timeout) ) == 0 ){
rb_warn( "Socket readable" );
return Qtrue;
}else{
rb_warn( "Socket not readable" );
return Qfalse;
}
}
/* send_query(sql) */
static VALUE send_query(VALUE obj, VALUE sql)
{
MYSQL* m = GetHandler(obj);
Check_Type(sql, T_STRING); Check_Type(sql, T_STRING);
if (GetMysqlStruct(obj)->connection == Qfalse) { if (GetMysqlStruct(obj)->connection == Qfalse) {
@ -789,17 +808,17 @@ static VALUE get_result(VALUE obj)
return store_result(obj); return store_result(obj);
} }
/* async_query */ /* async_query(sql,timeout=nil) */
/* static VALUE async_query(int argc, VALUE* argv, VALUE obj)
comment it out until I figure out how it works
static VALUE async_query(VALUE obj, VALUE sql)
{ {
VALUE sql, timeout;
rb_scan_args(argc, argv, "11", &sql, &timeout);
send_query(obj,sql); send_query(obj,sql);
rb_io_wait_readable(socket(obj)); rb_io_wait_readable(socket(obj));
return get_result(obj); return get_result(obj);
} }
*/
#if MYSQL_VERSION_ID >= 40100 #if MYSQL_VERSION_ID >= 40100
/* server_version() */ /* server_version() */
@ -2090,9 +2109,10 @@ void Init_mysql(void)
#endif #endif
rb_define_method(cMysql, "query", query, 1); rb_define_method(cMysql, "query", query, 1);
rb_define_method(cMysql, "real_query", query, 1); rb_define_method(cMysql, "real_query", query, 1);
/*rb_define_method(cMysql, "async_query", async_query, 1);*/ rb_define_method(cMysql, "async_query", async_query, -1);
rb_define_method(cMysql, "send_query", send_query, -1); rb_define_method(cMysql, "send_query", send_query, 1);
rb_define_method(cMysql, "get_result", get_result, 0); rb_define_method(cMysql, "get_result", get_result, 0);
rb_define_method(cMysql, "readable?", readable, -1);
rb_define_method(cMysql, "socket", socket, 0); rb_define_method(cMysql, "socket", socket, 0);
rb_define_method(cMysql, "refresh", refresh, 1); rb_define_method(cMysql, "refresh", refresh, 1);
rb_define_method(cMysql, "reload", reload, 0); rb_define_method(cMysql, "reload", reload, 0);

View File

@ -1,11 +1,15 @@
require 'mysql' require 'mysql'
class Mysql class Mysql
def async_query(sql) alias_method :c_async_query, :async_query
def async_query(sql, timeout = nil)
c_async_query(sql, timeout) if ENV['MYSQL_C_ASYNC_QUERY'] == '1'
send_query(sql) send_query(sql)
select [ (@sockets ||= {})[socket] ||= IO.new(socket) ], nil, nil, nil select [ (@sockets ||= {})[socket] ||= IO.new(socket) ], nil, nil, nil
get_result get_result
end end
end end
class Mysql::Result class Mysql::Result

View File

@ -1,6 +1,12 @@
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'
EventedMysqlTest.new( 10 ) do |test| EventedMysqlTest.new( 10 ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root','3421260') }
test.run!
end
EventedMysqlTest.new( 10 ) do |test|
test.setup{ Mysql.real_connect('localhost','root','3421260') }
test.c_async_query = true
test.run! test.run!
end end

View File

@ -10,11 +10,13 @@ class MysqlTest
:connections, :connections,
:connection_signature, :connection_signature,
:start, :start,
:done :done,
:c_async_query
def initialize( queries ) def initialize( queries )
@queries = queries @queries = queries
@done = [] @done = []
@c_async_query = false
yield self if block_given? yield self if block_given?
end end
@ -24,7 +26,10 @@ class MysqlTest
end end
def run! def run!
raise NotImplemented c_or_native_ruby_async_query do
prepare
yield
end
end end
def prepare def prepare
@ -49,6 +54,19 @@ class MysqlTest
Time.now - @start Time.now - @start
end end
protected
def c_or_native_ruby_async_query
if @c_async_query
ENV['MYSQL_C_ASYNC_QUERY'] = '1'
log "** using C based async_query"
else
ENV['MYSQL_C_ASYNC_QUERY'] = '0'
log "** using native Ruby async_query"
end
yield
end
end end
class EventedMysqlTest < MysqlTest class EventedMysqlTest < MysqlTest
@ -73,15 +91,19 @@ class EventedMysqlTest < MysqlTest
end end
def run! def run!
prepare super do
catch :END_EVENT_LOOP do
loop do loop do
result = select( @sockets,nil,nil,nil ) result = select( @sockets,nil,nil,nil )
if result if result
result.first.each do |conn| result.first.each do |conn|
@connections[conn].get_result.each{|res| log( "Result for socket #{conn.fileno} : #{res}" ) } @connections[conn].get_result.each{|res| log( "Result for socket #{conn.fileno} : #{res}" ) }
@done << nil @done << nil
teardown if done? if done?
teardown
end
end
end
end end
end end
end end
@ -95,7 +117,7 @@ class EventedMysqlTest < MysqlTest
def teardown def teardown
log "done" log "done"
exit throw :END_EVENT_LOOP
end end
protected protected
@ -126,10 +148,10 @@ class ThreadedMysqlTest < MysqlTest
end end
def run! def run!
prepare super do
with_logging "waiting on threads" do
with_logging "waiting on threads" do @threads.each{|t| t.join }
@threads.each{|t| t.join } end
end end
end end

View File

@ -1,6 +1,12 @@
require File.dirname(__FILE__) + '/test_helper' require File.dirname(__FILE__) + '/test_helper'
ThreadedMysqlTest.new( 10 ) do |test| ThreadedMysqlTest.new( 10 ) do |test|
test.setup{ Mysql.real_connect('localhost','root') } test.setup{ Mysql.real_connect('localhost','root','3421260') }
test.run!
end
ThreadedMysqlTest.new( 10 ) do |test|
test.setup{ Mysql.real_connect('localhost','root','3421260') }
test.c_async_query = true
test.run! test.run!
end end