@@ -148,15 +148,53 @@ def test_failure_with_contention(datastore_client, entities_to_delete, database_
148148
149149 entities_to_delete .append (orig_entity )
150150
151- with pytest .raises (Conflict ):
152- with local_client .transaction () as txn :
153- entity_in_txn = local_client .get (key )
151+ with local_client .transaction () as txn :
152+ entity_in_txn = local_client .get (key )
154153
155- # Update the original entity outside the transaction.
156- orig_entity [contention_prop_name ] = "outside"
154+ # Update the original entity outside the transaction.
155+ orig_entity [contention_prop_name ] = "outside"
156+ with pytest .raises (Conflict ):
157157 datastore_client .put (orig_entity )
158158
159- # Try to update the entity which we already updated outside the
160- # transaction.
161- entity_in_txn [contention_prop_name ] = "inside"
162- txn .put (entity_in_txn )
159+ # Try to update the entity which we already updated outside the
160+ # transaction.
161+ entity_in_txn [contention_prop_name ] = "inside"
162+ txn .put (entity_in_txn )
163+ # now that transaction is complete, should be able to update outside
164+ datastore_client .put (orig_entity )
165+
166+
167+ @pytest .mark .parametrize ("database_id" , [None , _helpers .TEST_DATABASE ], indirect = True )
168+ def test_failure_with_contention_no_context_manager (
169+ datastore_client , entities_to_delete , database_id
170+ ):
171+ contention_prop_name = "baz"
172+ local_client = _helpers .clone_client (datastore_client )
173+
174+ # Insert an entity which will be retrieved in a transaction
175+ # and updated outside it with a contentious value.
176+ key = local_client .key ("BreakTxnCM3" , 1234 )
177+ orig_entity = datastore .Entity (key = key )
178+ orig_entity ["foo" ] = "bar"
179+ local_client .put (orig_entity )
180+
181+ entities_to_delete .append (orig_entity )
182+
183+ txn = local_client .transaction ()
184+ txn .begin ()
185+
186+ entity_in_txn = local_client .get (key , transaction = txn )
187+
188+ # Update the original entity outside the transaction.
189+ # should fail due to contention
190+ orig_entity [contention_prop_name ] = "outside"
191+ with pytest .raises (Conflict ):
192+ datastore_client .put (orig_entity )
193+
194+ # Try to update the entity inside the transaction
195+ entity_in_txn [contention_prop_name ] = "inside"
196+ txn .put (entity_in_txn )
197+ txn .commit ()
198+
199+ # now that transaction is complete, should be able to update outside
200+ datastore_client .put (orig_entity )
0 commit comments