Skip to content

Commit 57d7294

Browse files
committed
RUBY-949 Correct slave_ok flag logic
1 parent 70c8c1f commit 57d7294

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

lib/mongo/operation/read_preferrable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def update_selector(context)
3737
end
3838

3939
def slave_ok?(context)
40-
!context.mongos? && (context.cluster.single? || read.slave_ok?)
40+
(context.cluster.single? && !context.mongos?) || read.slave_ok?
4141
end
4242

4343
def update_options(context)

spec/mongo/operation/read_preferrable_spec.rb

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,33 @@
151151
it_behaves_like 'not a selector updater'
152152
end
153153

154-
context 'when the server is not a mongos' do
154+
context 'when the topology is Single' do
155155

156-
let(:mongos?) do
157-
false
156+
let(:single?) do
157+
true
158158
end
159159

160-
context 'when the topology is Single' do
160+
context 'when the server is a mongos' do
161161

162-
let(:single?) do
162+
let(:mongos?) do
163163
true
164164
end
165165

166+
let(:expected) do
167+
{ }
168+
end
169+
170+
it 'does not set the slave_ok flag' do
171+
expect(read_preferrable.send(:update_options, context)).to eq(expected)
172+
end
173+
end
174+
175+
context 'when the server is not a mongos' do
176+
177+
let(:mongos?) do
178+
false
179+
end
180+
166181
let(:expected) do
167182
{ :flags => [ :slave_ok ] }
168183
end
@@ -171,12 +186,30 @@
171186
expect(read_preferrable.send(:update_options, context)).to eq(expected)
172187
end
173188
end
189+
end
174190

175-
context 'when the topology is not Single' do
191+
context 'when the topology is not Single' do
176192

177-
let(:single?) do
178-
false
193+
let(:single?) do
194+
false
195+
end
196+
197+
context 'when there is no read preference set' do
198+
199+
let(:read_pref) do
200+
Mongo::ServerSelector.get
201+
end
202+
203+
let(:expected) do
204+
{ }
205+
end
206+
207+
it 'does not set the slave_ok flag' do
208+
expect(read_preferrable.send(:update_options, context)).to eq(expected)
179209
end
210+
end
211+
212+
context 'when there is a read preference' do
180213

181214
context 'when the read preference requires the slave_ok flag' do
182215

@@ -208,35 +241,5 @@
208241
end
209242
end
210243
end
211-
212-
context 'when the topology is Single' do
213-
214-
let(:single?) do
215-
true
216-
end
217-
218-
let(:expected) do
219-
{ :flags => [ :slave_ok ] }
220-
end
221-
222-
it 'sets the slave_ok flag' do
223-
expect(read_preferrable.send(:update_options, context)).to eq(expected)
224-
end
225-
end
226-
end
227-
228-
context 'when the server is a mongos' do
229-
230-
let(:mongos?) do
231-
true
232-
end
233-
234-
let(:expected) do
235-
{ }
236-
end
237-
238-
it 'does not set the slave_ok flag' do
239-
expect(read_preferrable.send(:update_options, context)).to eq(expected)
240-
end
241244
end
242245
end

0 commit comments

Comments
 (0)