@@ -63,6 +63,8 @@ def test_secondary_connection(self):
6363 (secondary_host ,
6464 secondary_port ) = ha_tools .get_secondaries ()[0 ].split (':' )
6565 secondary_port = int (secondary_port )
66+ arbiter_host , arbiter_port = ha_tools .get_arbiters ()[0 ].split (':' )
67+ arbiter_port = int (arbiter_port )
6668
6769 # A Connection succeeds no matter the read preference (although a
6870 # secondary Connection with preference PRIMARY can't be queried)
@@ -83,7 +85,7 @@ def test_secondary_connection(self):
8385 self .assertTrue (conn .is_primary )
8486
8587 if kwargs .get ('read_preference' ) != ReadPreference .SECONDARY :
86- self .assert_ (conn .pymongo_test .test .find_one ())
88+ self .assertTrue (conn .pymongo_test .test .find_one ())
8789
8890 conn = Connection (secondary_host ,
8991 secondary_port ,
@@ -94,18 +96,39 @@ def test_secondary_connection(self):
9496 self .assertFalse (conn .is_primary )
9597
9698 if kwargs .get ('read_preference' ) != ReadPreference .PRIMARY :
97- self .assert_ (conn .pymongo_test .test .find_one ())
99+ self .assertTrue (conn .pymongo_test .test .find_one ())
98100 else :
99101 self .assertRaises (
100102 AutoReconnect , conn .pymongo_test .test .find_one )
101-
102- # Test direct connection to an arbiter
103- host , port = ha_tools .get_arbiters ()[0 ].split (':' )
104- port = int (port )
105- conn = Connection (host , port )
106- self .assertEqual (host , conn .host )
107- self .assertEqual (port , conn .port )
108103
104+ # Since an attempt at an acknowledged write to a secondary from a
105+ # direct connection raises AutoReconnect('not master'), Connection
106+ # should do the same for unacknowledged writes.
107+ try :
108+ conn .pymongo_test .test .insert ({}, safe = False )
109+ except AutoReconnect , e :
110+ self .assertEqual ('not master' , e .message )
111+ else :
112+ self .fail (
113+ 'Unacknowledged insert into secondary connection %s should'
114+ 'have raised exception' % conn )
115+
116+ # Test direct connection to an arbiter
117+ conn = Connection (arbiter_host , arbiter_port , ** kwargs )
118+ self .assertEqual (arbiter_host , conn .host )
119+ self .assertEqual (arbiter_port , conn .port )
120+ self .assertFalse (conn .is_primary )
121+
122+ # See explanation above
123+ try :
124+ conn .pymongo_test .test .insert ({}, safe = False )
125+ except AutoReconnect , e :
126+ self .assertEqual ('not master' , e .message )
127+ else :
128+ self .fail (
129+ 'Unacknowledged insert into arbiter connection %s should'
130+ 'have raised exception' % conn )
131+
109132 def tearDown (self ):
110133 self .c .close ()
111134 ha_tools .kill_all_members ()
0 commit comments