diff --git a/ext/mysql.c b/ext/mysql.c index d9bb6fa..9176911 100644 --- a/ext/mysql.c +++ b/ext/mysql.c @@ -60,6 +60,7 @@ struct mysql { MYSQL handler; char connection; char query_with_result; + char blocking; }; struct mysql_res { @@ -268,6 +269,11 @@ static VALUE real_connect(int argc, VALUE* argv, VALUE klass) myp->handler.reconnect = 0; myp->connection = Qtrue; + + my_bool was_blocking; + vio_blocking(myp->handler.net.vio, 0, &was_blocking); + myp->blocking = vio_is_blocking( myp->handler.net.vio ); + myp->query_with_result = Qtrue; rb_obj_call_init(obj, argc, argv); @@ -756,6 +762,11 @@ static VALUE socket(VALUE obj) return INT2NUM(m->net.fd); } +/* blocking */ +static VALUE blocking(VALUE obj){ + return ( GetMysqlStruct(obj)->blocking ? Qtrue : Qfalse ); +} + /* readable(timeout=nil) */ static VALUE readable( int argc, VALUE* argv, VALUE obj ) { @@ -2113,6 +2124,7 @@ void Init_mysql(void) rb_define_method(cMysql, "send_query", send_query, 1); rb_define_method(cMysql, "get_result", get_result, 0); rb_define_method(cMysql, "readable?", readable, -1); + rb_define_method(cMysql, "blocking?", blocking, 0); rb_define_method(cMysql, "socket", socket, 0); rb_define_method(cMysql, "refresh", refresh, 1); rb_define_method(cMysql, "reload", reload, 0); diff --git a/lib/mysqlplus.rb b/lib/mysqlplus.rb index 66a4b83..5303436 100644 --- a/lib/mysqlplus.rb +++ b/lib/mysqlplus.rb @@ -1,10 +1,12 @@ require 'mysql' class Mysql + 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' + puts "** Blocking ? #{blocking?().inspect}" if ENV['MYSQL_BLOCKING_STATUS'] == '1' send_query(sql) select [ (@sockets ||= {})[socket] ||= IO.new(socket) ], nil, nil, nil get_result diff --git a/test/evented_test.rb b/test/evented_test.rb index c74b297..0155db4 100644 --- a/test/evented_test.rb +++ b/test/evented_test.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/test_helper' EventedMysqlTest.new( 10 ) do |test| test.setup{ Mysql.real_connect('localhost','root') } + test.log_blocking_status = true test.run! end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3466411..4e5c96e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,7 +11,8 @@ class MysqlTest :connection_signature, :start, :done, - :c_async_query + :c_async_query, + :log_blocking_status def initialize( queries ) @queries = queries @@ -27,8 +28,10 @@ class MysqlTest def run! c_or_native_ruby_async_query do - prepare - yield + with_blocking_status do + prepare + yield + end end end @@ -67,6 +70,15 @@ class MysqlTest yield end + def with_blocking_status + if @log_blocking_status + ENV['MYSQL_BLOCKING_STATUS'] = '1' + else + ENV['MYSQL_BLOCKING_STATUS'] = '0' + end + yield + end + end class EventedMysqlTest < MysqlTest diff --git a/test/threaded_test.rb b/test/threaded_test.rb index 5a70158..122c817 100644 --- a/test/threaded_test.rb +++ b/test/threaded_test.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/test_helper' ThreadedMysqlTest.new( 10 ) do |test| test.setup{ Mysql.real_connect('localhost','root') } + test.log_blocking_status = true test.run! end