From 91d9096b14014f6535595afecea45c31f2c867e3 Mon Sep 17 00:00:00 2001 From: Lourens Naude Date: Sat, 6 Sep 2008 00:54:48 +0100 Subject: [PATCH] Extract base klasses for cleaner implementation testing; DRY up existing threaded && evented tests --- test/evented_test.rb | 30 +-------- test/test_helper.rb | 152 +++++++++++++++++++++++++++++++++++++++++- test/threaded_test.rb | 32 ++------- 3 files changed, 158 insertions(+), 56 deletions(-) diff --git a/test/evented_test.rb b/test/evented_test.rb index 83ddf14..ec8ad3d 100644 --- a/test/evented_test.rb +++ b/test/evented_test.rb @@ -1,30 +1,6 @@ require File.dirname(__FILE__) + '/test_helper' -@count = 10 -@connections = {} -@count.times do - c = Mysql.real_connect('localhost','root','3421260') - @connections[IO.new(c.socket)] = c -end - -@sockets = @connections.keys - -@done = 0 -@t = Time.now -@connections.each_value do |c| - c.send_query('select sleep(1)') -end - -loop do - res = select(@sockets,nil,nil,nil) - if res - res.first.each do |c| - @connections[c].get_result.each{|r| p r} - @done = @done + 1 - if @done == @count - puts Time.now - @t - exit - end - end - end +EventedMysqlTest.new( 10 ) do |test| + test.setup{ Mysql.real_connect('localhost','root') } + test.run! end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index b257359..3f5078c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,2 +1,152 @@ require 'rubygems' -require 'mysqlplus' \ No newline at end of file +require 'mysqlplus' + +class MysqlTest + + class NotImplemented < StandardError + end + + attr_accessor :queries, + :connections, + :connection_signature, + :start, + :done + + def initialize( queries ) + @queries = queries + @done = [] + yield self if block_given? + end + + def setup( &block ) + @start = Time.now + @connection_signature = block + end + + def run! + raise NotImplemented + end + + def prepare + raise NotImplemented + end + + def teardown + raise NotImplemented + end + + def log( message, prefix = '' ) + puts "[#{timestamp}] #{prefix} #{message}" + end + + def with_logging( message ) + log( message, 'Start' ) + yield + log( message, 'End' ) + end + + def timestamp + Time.now - @start + end + +end + +class EventedMysqlTest < MysqlTest + + attr_accessor :sockets + + def initialize( queries ) + @sockets = [] + @connections = {} + super( queries ) + end + + def setup( &block ) + super( &block ) + with_logging 'Setup connection pool' do + @queries.times do + connection = @connection_signature.call + @connections[ IO.new(connection.socket) ] = connection + @sockets = @connections.keys + end + end + end + + def run! + prepare + + loop do + result = select( @sockets,nil,nil,nil ) + if result + result.first.each do |conn| + @connections[conn].get_result.each{|res| log( "Result for socket #{conn.fileno} : #{res}" ) } + @done << nil + teardown if done? + end + end + end + end + + def prepare + @connections.each_value do |conn| + conn.send_query( "select sleep(3)" ) + end + end + + def teardown + log "done" + exit + end + + protected + + def done? + @done.size == @queries + end + +end + +class ThreadedMysqlTest < MysqlTest + + attr_accessor :threads + + def initialize( queries ) + @connections = [] + @threads = [] + super( queries ) + end + + def setup( &block ) + super( &block ) + with_logging "Setup connection pool" do + @queries.times do + @connections << @connection_signature.call + end + end + end + + def run! + prepare + + with_logging "waiting on threads" do + @threads.each{|t| t.join } + end + end + + def prepare + with_logging "prepare" do + @queries.times do |conn| + @threads << Thread.new do + + log "sending query on connection #{conn}" + + @connections[conn].async_query( "select sleep(3)" ).each do |result| + log "connection #{conn} done" + end + + end + end + end + end + +end \ No newline at end of file diff --git a/test/threaded_test.rb b/test/threaded_test.rb index 3bc75c3..7a18a26 100644 --- a/test/threaded_test.rb +++ b/test/threaded_test.rb @@ -1,30 +1,6 @@ require File.dirname(__FILE__) + '/test_helper' -$count = 10 - -$start = Time.now - -$connections = [] -$count.times do - $connections << Mysql.real_connect('localhost','root','3421260') -end - -puts 'connection pool ready' - -$threads = [] -$count.times do |i| - $threads << Thread.new do - - puts "sending query on connection #{i}" - - $connections[i].async_query("select sleep(3)").each{ |r| - puts "connection #{i} done" - } - - end -end - -puts 'waiting on threads' -$threads.each{|t| t.join } - -puts Time.now - $start \ No newline at end of file +ThreadedMysqlTest.new( 10 ) do |test| + test.setup{ Mysql.real_connect('localhost','root') } + test.run! +end \ No newline at end of file