Skip navigation.

Using Ruby Multithreading Capabilities for Database Testing

databases & SQL
Using Multithreading and ADODB in Ruby

Ruby Scripting Language is emerging as a tester’s language for test automation. Ruby is a very feature reach and powerful language. We had a situation where we need to connect to different databases and fire a SELECT query at same point of time. I tried this various other alternatives, but no results.

Then I saw multithreading support in Ruby and implemented following code using it. While doing this I wanted to access databases using ADO and I could not found help on how to use ADO in Ruby, after trial and error I got the following code working. I am sharing this code so others can use this as a reference for using ADO in Ruby.


require 'win32ole'

$rsOne = WIN32OLE.new('ADODB.Recordset')
$rsTwo = WIN32OLE.new('ADODB.Recordset')

# Data structure to hold DSN Names
dbs = %w( Test1 #DSN 1 - For SQL Server
        Test2 #DSN 2 - For Oracle
        )

threads = []

for db in dbs

    threads << Thread.new(db) { |myDB|


    puts "Start Time >> " << (Time.now).to_s

    $con = WIN32OLE.new('ADODB.Connection')
    $con['connectionstring'] = myDB
    $con.open

    if myDB == 'Test1' then
        $rsOne = $con.execute('SELECT UserName FROM tblUser')
    elsif myDB == 'Test2' then
        $rsTwo = $con.execute('SELECT UserName FROM tblUser')
    end

    puts "End Time >> " << (Time.now).to_s

    }
end

# Here it will create two threads at the same point of time and fetch the SELECT query results
# into two different recordsets and compare these recordsets with each other

threads.each { |aThread| aThread.join }

until $rsOne['eof'] == true and $rsTwo['eof']== true

    # It is checking first field only, you can add additional until loop for all columns
    # with the help rs.Fields.Count property to check all the columns
    if $rsOne.Fields(0).Value == $rsTwo.Fields(0).Value then
        puts $rsOne.Fields(0).Value << "=" << $rsTwo.Fields(0).Value
    else
        puts $rsOne.Fields(0).Value << "<>" << $rsTwo.Fields(0).Value
    end

    $rsOne.MoveNext()
    $rsTwo.MoveNext()
end