55
66from __future__ import with_statement
77
8+ import Queue as queue_module
9+
810from python_toolbox .context_managers import ReentrantContextManager
911from python_toolbox import cute_testing
1012
@@ -111,4 +113,41 @@ def reentrant_exit(self, exc_type, exc_value, exc_traceback):
111113 my_set .add (8 )
112114 assert my_set == set ([0 , 1 , 2 , 3 , 4 ])
113115
114-
116+
117+
118+ def test_order_of_depth_modification ():
119+ ''' '''
120+
121+ depth_log = queue_module .Queue ()
122+
123+ class JohnnyReentrantContextManager (ReentrantContextManager ):
124+ def reentrant_enter (self ):
125+ depth_log .put (self .depth )
126+ return self
127+ def reentrant_exit (self , exc_type , exc_value , exc_traceback ):
128+ depth_log .put (self .depth )
129+
130+ johnny_reentrant_context_manager = JohnnyReentrantContextManager ()
131+ assert johnny_reentrant_context_manager .depth == 0
132+ with johnny_reentrant_context_manager :
133+ assert johnny_reentrant_context_manager .depth == 1
134+
135+ # `reentrant_enter` saw a depth of 0, because the depth increment
136+ # happens *after* `reentrant_enter` is called:
137+ assert depth_log .get (block = False ) == 0
138+
139+ with johnny_reentrant_context_manager :
140+
141+ assert johnny_reentrant_context_manager .depth == 2
142+ assert depth_log .qsize () == 0 # We're in a depth greater than 1,
143+ # so `reentrant_enter` wasn't even
144+ # called.
145+
146+ assert johnny_reentrant_context_manager .depth == 1
147+
148+ assert depth_log .qsize () == 0 # We came out of a depth greater than 1,
149+ # so `reentrant_exit` wasn't even called.
150+
151+ # `reentrant_exit` saw a depth of 1, because the depth decrement happens
152+ # *after* `reentrant_exit` is called:
153+ assert depth_log .get (block = False ) == 1
0 commit comments