@@ -22,11 +22,15 @@ module Cassandra
22
22
module LoadBalancing
23
23
module Policies
24
24
describe ( TokenAware ) do
25
- let ( :policy ) { double ( 'load balancing policy' ) . as_null_object }
25
+ let ( :policy ) { double ( 'load balancing policy' ) }
26
26
let ( :cluster ) { double ( 'cassandra cluster' ) }
27
27
28
28
subject { TokenAware . new ( policy ) }
29
29
30
+ before do
31
+ allow ( policy ) . to receive ( :respond_to? ) { true }
32
+ end
33
+
30
34
describe ( '#setup' ) do
31
35
it 'sets up wrapped policy' do
32
36
expect ( policy ) . to receive ( :setup ) . once . with ( cluster )
@@ -67,6 +71,7 @@ module Policies
67
71
68
72
context ( 'when set up' ) do
69
73
before do
74
+ allow ( policy ) . to receive ( :setup )
70
75
subject . setup ( cluster )
71
76
expect ( cluster ) . to receive ( :find_replicas ) . once . with ( keyspace , statement ) . and_return ( replicas )
72
77
end
@@ -91,21 +96,57 @@ module Policies
91
96
}
92
97
let ( :plan ) { subject . plan ( keyspace , statement , options ) }
93
98
94
- it 'prioritizes found replicas' do
95
- expect ( plan . next ) . to eq ( replicas [ 0 ] )
96
- expect ( plan . next ) . to eq ( replicas [ 1 ] )
97
- expect ( plan . next ) . to eq ( replicas [ 2 ] )
99
+ context ( 'and all replicas are local' ) do
100
+ before do
101
+ allow ( policy ) . to receive ( :distance ) { :local }
102
+ end
103
+
104
+ it 'prioritizes found replicas' do
105
+ expect ( plan . has_next? ) . to eq ( true )
106
+ expect ( plan . next ) . to eq ( replicas [ 0 ] )
107
+ expect ( plan . has_next? ) . to eq ( true )
108
+ expect ( plan . next ) . to eq ( replicas [ 1 ] )
109
+ expect ( plan . has_next? ) . to eq ( true )
110
+ expect ( plan . next ) . to eq ( replicas [ 2 ] )
111
+ end
112
+
113
+ context ( 'and all replicas failed' ) do
114
+ before do
115
+ replicas . size . times do
116
+ expect ( plan . has_next? ) . to eq ( true )
117
+ plan . next
118
+ end
119
+
120
+ allow ( child_plan ) . to receive ( :next ) . and_return ( next_host )
121
+ allow ( child_plan ) . to receive ( :has_next? ) . and_return ( true , false )
122
+ allow ( policy ) . to receive ( :plan ) . and_return ( child_plan )
123
+ end
124
+
125
+ let ( :next_host ) { double ( 'next host from the wrapped policy plan' ) }
126
+ let ( :child_plan ) { double ( 'wrapped policy plan' ) }
127
+
128
+ it 'delegates to the wrapped policy' do
129
+ expect ( plan . has_next? ) . to be_truthy
130
+ expect ( plan . next ) . to eq ( next_host )
131
+ end
132
+
133
+ context ( 'and replica host returned from the child plan' ) do
134
+ let ( :next_host ) { replicas . sample }
135
+
136
+ it 'is ignored' do
137
+ expect ( plan . has_next? ) . to be_falsey
138
+ end
139
+ end
140
+ end
98
141
end
99
142
100
- context ( 'and all replicas failed ' ) do
143
+ context ( 'and all replicas are not local ' ) do
101
144
before do
102
- replicas . size . times do
103
- plan . next
104
- end
145
+ allow ( policy ) . to receive ( :distance ) { :remote }
146
+ allow ( policy ) . to receive ( :plan ) { child_plan }
105
147
106
- allow ( child_plan ) . to receive ( :next ) . and_return ( next_host )
107
- allow ( child_plan ) . to receive ( :has_next? ) . and_return ( true , false )
108
- allow ( policy ) . to receive ( :plan ) . and_return ( child_plan )
148
+ allow ( child_plan ) . to receive ( :next ) { next_host }
149
+ allow ( child_plan ) . to receive ( :has_next? ) { true }
109
150
end
110
151
111
152
let ( :next_host ) { double ( 'next host from the wrapped policy plan' ) }
@@ -115,14 +156,6 @@ module Policies
115
156
expect ( plan . has_next? ) . to be_truthy
116
157
expect ( plan . next ) . to eq ( next_host )
117
158
end
118
-
119
- context ( 'and replica host returned from the child plan' ) do
120
- let ( :next_host ) { replicas . sample }
121
-
122
- it 'is ignored' do
123
- expect ( plan . has_next? ) . to be_falsey
124
- end
125
- end
126
159
end
127
160
end
128
161
end
0 commit comments