3737GSSAPI_PORT = int (os .environ .get ('GSSAPI_PORT' , '27017' ))
3838PRINCIPAL = os .environ .get ('PRINCIPAL' )
3939
40+
41+ class AutoAuthenticateThread (threading .Thread ):
42+ """Used in testing threaded authentication.
43+ """
44+
45+ def __init__ (self , database ):
46+ super (AutoAuthenticateThread , self ).__init__ ()
47+ self .database = database
48+ self .success = True
49+
50+ def run (self ):
51+ try :
52+ self .database .command ('dbstats' )
53+ except OperationFailure :
54+ self .success = False
55+
56+
4057class TestGSSAPI (unittest .TestCase ):
4158
4259 def setUp (self ):
@@ -80,25 +97,17 @@ def test_gssapi_threaded(self):
8097 self .assertTrue (client .test .authenticate (PRINCIPAL ,
8198 mechanism = 'GSSAPI' ))
8299
83- result = True
84- def try_command ():
85- try :
86- client .foo .command ('dbstats' )
87- except OperationFailure :
88- result = False
89-
90100 threads = []
91- for _ in xrange (2 ):
92- threads .append (threading . Thread ( target = try_command ))
101+ for _ in xrange (4 ):
102+ threads .append (AutoAuthenticateThread ( client . foo ))
93103 for thread in threads :
94104 thread .start ()
95105 for thread in threads :
96106 thread .join ()
97- self .assertTrue (result )
107+ self .assertTrue (thread . success )
98108
99109 set_name = client .admin .command ('ismaster' ).get ('setName' )
100110 if set_name :
101- result = True
102111 preference = ReadPreference .SECONDARY
103112 client = MongoReplicaSetClient (GSSAPI_HOST ,
104113 replicaSet = set_name ,
@@ -108,13 +117,13 @@ def try_command():
108117 self .assertTrue (client .foo .command ('dbstats' ))
109118
110119 threads = []
111- for _ in xrange (2 ):
112- threads .append (threading . Thread ( target = try_command ))
120+ for _ in xrange (4 ):
121+ threads .append (AutoAuthenticateThread ( client . foo ))
113122 for thread in threads :
114123 thread .start ()
115124 for thread in threads :
116125 thread .join ()
117- self .assertTrue (result )
126+ self .assertTrue (thread . success )
118127
119128
120129class TestAuthURIOptions (unittest .TestCase ):
@@ -228,21 +237,25 @@ def test_delegated_auth(self):
228237 userSource = 'pymongo_test' ,
229238 roles = ['read' ])
230239 self .client .admin .logout ()
231- self .assertRaises (OperationFailure , self .client .pymongo_test2 .foo .find_one )
240+ self .assertRaises (OperationFailure ,
241+ self .client .pymongo_test2 .foo .find_one )
232242 # Auth must occur on the db where the user is defined.
233- self .assertFalse (self .client .pymongo_test2 .authenticate ('user' , 'pass' ))
243+ self .assertFalse (self .client .pymongo_test2 .authenticate ('user' ,
244+ 'pass' ))
234245 # Auth directly
235246 self .assertTrue (self .client .pymongo_test .authenticate ('user' , 'pass' ))
236247 self .assertTrue (self .client .pymongo_test2 .foo .find_one ())
237248 self .client .pymongo_test .logout ()
238- self .assertRaises (OperationFailure , self .client .pymongo_test2 .foo .find_one )
249+ self .assertRaises (OperationFailure ,
250+ self .client .pymongo_test2 .foo .find_one )
239251 # Auth using source
240252 self .assertTrue (self .client .pymongo_test2 .authenticate (
241253 'user' , 'pass' , source = 'pymongo_test' ))
242254 self .assertTrue (self .client .pymongo_test2 .foo .find_one ())
243255 # Must logout from the db authenticate was called on.
244256 self .client .pymongo_test2 .logout ()
245- self .assertRaises (OperationFailure , self .client .pymongo_test2 .foo .find_one )
257+ self .assertRaises (OperationFailure ,
258+ self .client .pymongo_test2 .foo .find_one )
246259
247260
248261if __name__ == "__main__" :
0 commit comments