1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
require 'spec_helper'
describe "SeamlessDatabasePool" do
before(:each) do
SeamlessDatabasePool.clear_read_only_connection
end
after(:each) do
SeamlessDatabasePool.clear_read_only_connection
end
it "should use the master connection by default" do
connection = double(:connection, :master_connection => :master_db_connection, :using_master_connection? => false)
SeamlessDatabasePool.read_only_connection_type.should == :master
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
it "should be able to set using persistent read connections" do
connection = double(:connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :persistent
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
end
it "should be able to set using random read connections" do
connection = double(:connection)
connection.should_receive(:random_read_connection).and_return(:read_db_connection_1, :read_db_connection_2)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_random_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :random
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_1
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_2
end
it "should use the master connection if the connection is forcing it" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:using_master_connection?).and_return(true)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
it "should be able to set using the master connection" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_master_connection
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
it "should be able to use persistent read connections within a block" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
SeamlessDatabasePool.use_persistent_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
:test_val
end.should == :test_val
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
it "should be able to use random read connections within a block" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).and_return(:read_db_connection_1, :read_db_connection_2)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
SeamlessDatabasePool.use_random_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_1
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection_2
:test_val
end.should == :test_val
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
it "should be able to use the master connection within a block" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.should_receive(:random_read_connection).once.and_return(:read_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.use_master_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
:test_val
end.should == :test_val
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.clear_read_only_connection
end
it "should be able to use connection blocks within connection blocks" do
connection = double(:connection, :master_connection => :master_db_connection)
connection.stub(:random_read_connection => :read_db_connection)
connection.stub(:using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.use_master_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
SeamlessDatabasePool.use_random_read_connection do
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
end
SeamlessDatabasePool.read_only_connection(connection).should == :master_db_connection
end
end
SeamlessDatabasePool.clear_read_only_connection
end
it "should be able to change the persistent connection" do
connection = double(:connection)
connection.stub(:random_read_connection => :read_db_connection, :using_master_connection? => false)
SeamlessDatabasePool.use_persistent_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :persistent
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.set_persistent_read_connection(connection, :another_db_connection)
SeamlessDatabasePool.read_only_connection(connection).should == :another_db_connection
SeamlessDatabasePool.use_random_read_connection
SeamlessDatabasePool.read_only_connection_type.should == :random
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
SeamlessDatabasePool.set_persistent_read_connection(connection, :another_db_connection)
SeamlessDatabasePool.read_only_connection(connection).should == :read_db_connection
end
it "should be able to specify a default read connection type instead of :master" do
SeamlessDatabasePool.read_only_connection_type.should == :master
SeamlessDatabasePool.read_only_connection_type(nil).should == nil
end
it "should pull out the master configurations for compatibility with rake db:* tasks" do
config = {
'development' => {
'adapter' => 'seamless_database_pool',
'pool_adapter' => 'mysql2',
'database' => 'development',
'username' => 'root',
'master' => {
'host' => 'localhost',
'pool_weight' => 2
},
'read_pool' => {
'host' => 'slavehost',
'pool_weight' => 5
}
},
'test' => {
'adapter' => 'mysql2',
'database' => 'test'
}
}
SeamlessDatabasePool.master_database_configuration(config).should == {
'development' => {
'adapter' => 'mysql2',
'database' => 'development',
'username' => 'root',
'host' => 'localhost'
},
'test' => {
'adapter' => 'mysql2',
'database' => 'test'
}
}
end
end
|