Skip to content

Commit 54d1a78

Browse files
authored
Merge pull request #296 from NikolayBaranovv/check_master
feat: close connection when master down
2 parents 1894f36 + 6779faa commit 54d1a78

File tree

13 files changed

+262
-202
lines changed

13 files changed

+262
-202
lines changed

tests/advanced/test_replicaset.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from time import time
1818

1919
from bson import SON
20-
from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
20+
from pymongo.errors import AutoReconnect, ConfigurationError, NotPrimaryError
2121
from twisted.internet import defer, reactor
2222
from twisted.trial import unittest
2323

@@ -346,3 +346,26 @@ def test_StaleConnection(self):
346346
finally:
347347
self.__mongod[0].kill(signal.SIGCONT)
348348
yield conn.disconnect()
349+
350+
@defer.inlineCallbacks
351+
def test_CloseConnectionAfterPrimaryStepDown(self):
352+
conn = ConnectionPool(self.master_with_guaranteed_write)
353+
try:
354+
yield conn.db.coll.insert_one({"x": 42})
355+
356+
got_not_primary_error = False
357+
358+
while True:
359+
try:
360+
yield conn.db.coll.find_one()
361+
if got_not_primary_error:
362+
# We got error and then restored — OK
363+
break
364+
yield self.__sleep(1)
365+
yield conn.admin.command({"replSetStepDown": 86400, "force": 1})
366+
except (NotPrimaryError, AutoReconnect):
367+
got_not_primary_error = True
368+
369+
finally:
370+
yield conn.disconnect()
371+
self.flushLoggedErrors(NotPrimaryError)

tests/basic/test_bulk.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,18 @@ def test_OperationFailure(self):
269269

270270
def fake_send_query(*args):
271271
return defer.succeed(
272-
Msg(
273-
body=bson.encode(
274-
{
275-
"ok": 0.0,
276-
"errmsg": "operation was interrupted",
277-
"code": 11602,
278-
"codeName": "InterruptedDueToReplStateChange",
279-
}
280-
)
272+
Msg.create(
273+
{
274+
"ok": 0.0,
275+
"errmsg": "operation was interrupted",
276+
"code": 11602,
277+
"codeName": "InterruptedDueToReplStateChange",
278+
}
281279
)
282280
)
283281

284282
with patch(
285-
"txmongo.protocol.MongoProtocol.send_msg", side_effect=fake_send_query
283+
"txmongo.protocol.MongoProtocol._send_raw_msg", side_effect=fake_send_query
286284
):
287285
yield self.assertFailure(
288286
self.coll.bulk_write(

tests/basic/test_protocol.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,24 @@ def test_EncodeDecodeReply(self):
8787
self.assertEqual(decoded.documents, request.documents)
8888

8989
def test_EncodeDecodeMsg(self):
90-
request = Msg(
91-
response_to=123,
92-
flag_bits=OP_MSG_MORE_TO_COME,
93-
body=bson.encode({"a": 1, "$db": "dbname"}),
90+
request = Msg.create(
91+
body={"a": 1, "$db": "dbname"},
9492
payload={
9593
"documents": [
96-
bson.encode({"a": 1}),
97-
bson.encode({"a": 2}),
94+
{"a": 1},
95+
{"a": 2},
9896
],
9997
"updates": [
100-
bson.encode({"$set": {"z": 1}}),
101-
bson.encode({"$set": {"z": 2}}),
98+
{"$set": {"z": 1}},
99+
{"$set": {"z": 2}},
102100
],
103101
"deletes": [
104-
bson.encode({"_id": ObjectId()}),
105-
bson.encode({"_id": ObjectId()}),
102+
{"_id": ObjectId()},
103+
{"_id": ObjectId()},
106104
],
107105
},
106+
acknowledged=False,
107+
response_to=123,
108108
)
109109

110110
decoded = self._encode_decode(request)

tests/basic/test_queries.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ def test_CursorClosingWithTimeout(self):
203203
{"$where": "sleep(100); true"}, batch_size=5, timeout=0.8
204204
)
205205
with patch.object(
206-
MongoProtocol,
207-
"send_msg",
208-
side_effect=MongoProtocol.send_msg,
209-
autospec=True,
206+
MongoProtocol, "send_msg", side_effect=MongoProtocol.send_msg, autospec=True
210207
) as mock:
211208
with self.assertRaises(TimeExceeded):
212209
yield dfr

tests/mongod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def stop(self):
9696
if self._proc and self._proc.pid:
9797
d = defer.Deferred()
9898
self._notify_stop.append(d)
99-
self._proc.signalProcess("INT")
99+
self.kill("INT")
100100
return d
101101
else:
102102
return defer.fail("Not started yet")

tests/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pymongo.errors import AutoReconnect
12
from twisted.internet import defer
23
from twisted.trial import unittest
34

@@ -15,5 +16,11 @@ def setUp(self):
1516

1617
@defer.inlineCallbacks
1718
def tearDown(self):
18-
yield self.coll.drop()
19+
while True:
20+
try:
21+
yield self.coll.drop()
22+
break
23+
except AutoReconnect:
24+
pass
25+
1926
yield self.conn.disconnect()

txmongo/_bulk.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,18 @@
1818
validate_ok_for_update,
1919
)
2020

21+
from txmongo._bulk_constants import (
22+
_DELETE,
23+
_INSERT,
24+
_UPDATE,
25+
COMMAND_NAME,
26+
PAYLOAD_ARG_NAME,
27+
)
2128
from txmongo.protocol import MongoProtocol, Msg
2229
from txmongo.types import Document
2330

2431
_WriteOp = Union[InsertOne, UpdateOne, UpdateMany, ReplaceOne, DeleteOne, DeleteMany]
2532

26-
_INSERT = 0
27-
_UPDATE = 1
28-
_DELETE = 2
29-
30-
COMMAND_NAME = {
31-
_INSERT: "insert",
32-
_UPDATE: "update",
33-
_DELETE: "delete",
34-
}
35-
36-
PAYLOAD_ARG_NAME = {
37-
_INSERT: "documents",
38-
_UPDATE: "updates",
39-
_DELETE: "deletes",
40-
}
41-
4233

4334
class _Run:
4435
op_type: int

txmongo/_bulk_constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
_INSERT = 0
2+
_UPDATE = 1
3+
_DELETE = 2
4+
COMMAND_NAME = {
5+
_INSERT: "insert",
6+
_UPDATE: "update",
7+
_DELETE: "delete",
8+
}
9+
PAYLOAD_ARG_NAME = {
10+
_INSERT: "documents",
11+
_UPDATE: "updates",
12+
_DELETE: "deletes",
13+
}

0 commit comments

Comments
 (0)