Permalink
Browse files

Extract base klasses for cleaner implementation testing; DRY up exist…

…ing threaded && evented tests
  • Loading branch information...
1 parent 951fa1d commit 91d9096b14014f6535595afecea45c31f2c867e3 @methodmissing methodmissing committed Sep 5, 2008
Showing with 158 additions and 56 deletions.
  1. +3 −27 test/evented_test.rb
  2. +151 −1 test/test_helper.rb
  3. +4 −28 test/threaded_test.rb
View
@@ -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
View
@@ -1,2 +1,152 @@
require 'rubygems'
-require 'mysqlplus'
+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
View
@@ -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
+ThreadedMysqlTest.new( 10 ) do |test|
+ test.setup{ Mysql.real_connect('localhost','root') }
+ test.run!
+end

0 comments on commit 91d9096

Please sign in to comment.