root/trunk/test/client/test_bdrb_cluster_connection.rb

Revision 327, 5.9 kB (checked in by gethema..@gmail.com, 5 months ago)

sync code with git

Line 
1 require File.join(File.dirname(__FILE__) + "/../bdrb_test_helper")
2 require File.join(File.dirname(__FILE__) + "/../bdrb_client_test_helper")
3
4 context "For Cluster connection" do
5   class BackgrounDRb::Connection
6     attr_accessor :server_ip,:server_port,:cluster_conn,:connection_status
7     def establish_connection
8       @connection_status = true
9     end
10
11     def close_connection
12       @connection_status = false
13     end
14     def server_info; "#{@server_ip}:#{server_port}"; end
15   end
16
17   setup do
18     BDRB_CONFIG.set({:schedules=> {
19         :foo_worker => { :barbar=>{:trigger_args=>"*/5 * * * * * *"}}},
20       :backgroundrb=>{:port=>11008, :ip=>"0.0.0.0", :environment=> "production"},
21       :client => "localhost:11001,localhost:11002,localhost:11003"
22     })
23
24     @cluster_connection = BackgrounDRb::ClusterConnection.new
25     class << @cluster_connection
26       def ivar(var)
27         return instance_variable_get("@#{var}")
28       end
29       def iset(var,value)
30         instance_variable_set("@#{var}",value)
31       end
32     end
33   end
34
35   specify "should read config file and connect to specified servers" do
36     @cluster_connection.backend_connections.length.should == 4
37     @cluster_connection.bdrb_servers.length.should == 4
38     @cluster_connection.ivar(:round_robin).length.should == 4
39     @cluster_connection.backend_connections[0].server_info.should == "localhost:11001"
40   end
41
42   specify "should return worker chosen in round robin manner if nothing specified" do
43     t_conn = @cluster_connection.choose_server
44     t_conn.server_info.should == "localhost:11001"
45     t_conn = @cluster_connection.choose_server
46     t_conn.server_info.should == "localhost:11002"
47   end
48
49   specify "should return connection from chosen host if specified" do
50     t_conn = @cluster_connection.find_connection("localhost:11001")
51     t_conn.server_info.should == "localhost:11001"
52   end
53
54   specify "should return connection from local host if specified" do
55     t_conn = @cluster_connection.find_local
56     t_conn.server_info.should == "0.0.0.0:11008"
57   end
58
59   specify "should not return disconnected connections" do
60     t_conn = @cluster_connection.find_next_except_these(Array("localhost:11001"))
61     @cluster_connection.ivar(:disconnected_connections).size.should == 1
62     @cluster_connection.backend_connections.size.should == 3
63     server_infos = @cluster_connection.backend_connections.map(&:server_info)
64     server_infos.should.include "0.0.0.0:11008"
65     server_infos.should.include "localhost:11002"
66     server_infos.should.include "localhost:11003"
67     server_infos.should.not.include "localhost:11001"
68     t_conn.server_info.should.not == "localhost:11001"
69   end
70
71   specify "should discover new connections when time to discover" do
72     t_conn = @cluster_connection.find_next_except_these(Array("localhost:11001"))
73     @cluster_connection.ivar(:disconnected_connections).size.should == 1
74     @cluster_connection.backend_connections.size.should == 3
75     @cluster_connection.iset(:request_count,9)
76     worker_proxy = @cluster_connection.worker(:hello_worker)
77     @cluster_connection.ivar(:disconnected_connections).size.should == 0
78     @cluster_connection.backend_connections.size.should == 4
79   end
80
81   specify "discover service should run only when connections were dropped" do
82     @cluster_connection.iset(:request_count,9)
83     @cluster_connection.expects(:disover_periodically).never
84     worker_proxy = @cluster_connection.worker(:hello_worker)
85   end
86
87   specify "should work with new_worker method calls" do
88     @cluster_connection.backend_connections.each do |t_conn|
89       t_conn.expects(:new_worker).with(:worker => :hello_worker,:worker_key => "boy",:data => "boy").returns(true)
90     end
91     a = @cluster_connection.new_worker(:worker => :hello_worker,:worker_key => "boy",:data => "boy")
92     a.should == "boy"
93   end
94
95   specify "should work with all worker info methods" do
96     @cluster_connection.backend_connections.each do |t_conn|
97       t_conn.expects(:all_worker_info).returns(:status => :running)
98     end
99     foo = @cluster_connection.all_worker_info
100     foo.should == {"0.0.0.0:11008"=>{:status=>:running}, "localhost:11001"=>{:status=>:running}, "localhost:11002"=>{:status=>:running}, "localhost:11003"=>{:status=>:running}}
101   end
102 end
103
104 context "For single connections" do
105   class BackgrounDRb::Connection
106     attr_accessor :server_ip,:server_port,:cluster_conn,:connection_status
107   end
108
109   setup do
110     BDRB_CONFIG.set({:schedules=> {
111         :foo_worker => { :barbar=>{:trigger_args=>"*/5 * * * * * *"}}},
112       :backgroundrb=>{:port=>11008, :ip=>"0.0.0.0", :environment=> "production"}
113     })
114
115     @cluster_connection = BackgrounDRb::ClusterConnection.new
116     class << @cluster_connection
117       def ivar(var)
118         return instance_variable_get("@#{var}")
119       end
120     end
121   end
122
123   specify "should read config file and connect to servers" do
124     @cluster_connection.backend_connections.length.should == 1
125     @cluster_connection.bdrb_servers.length.should == 1
126     @cluster_connection.ivar(:round_robin).length.should == 1
127     @cluster_connection.backend_connections[0].server_info.should == "0.0.0.0:11008"
128   end
129 end
130
131 context "For memcache based result storage" do
132   setup do
133     options = { :schedules =>
134       {
135         :foo_worker => { :barbar=>{:trigger_args=>"*/5 * * * * * *"}}
136       },
137       :backgroundrb =>
138       {
139         :port=>11008, :ip=>"0.0.0.0", :environment=> "production",:result_storage => 'memcache'
140       },
141       :client => "localhost:11001,localhost:11002,localhost:11003",
142       :memcache => "10.0.0.1:11211,10.0.0.2:11211"
143     }
144     BDRB_CONFIG.set(options)
145
146     @cluster_connection = BackgrounDRb::ClusterConnection.new
147     class << @cluster_connection
148       def ivar(var)
149         return instance_variable_get("@#{var}")
150       end
151       def iset(var,value)
152         instance_variable_set("@#{var}",value)
153       end
154     end
155   end
156
157
158   specify "should use memcache based result storage if specified" do
159     @cluster_connection.cache.should.not == nil
160     @cluster_connection.cache.class.should == MemCache
161   end
162 end
Note: See TracBrowser for help on using the browser.