MCPcopy Create free account
hub / github.com/libgit2/pygit2 / test_transaction_isolated_across_threads

Function test_transaction_isolated_across_threads

test/test_transaction.py:135–189  ·  view source on GitHub ↗

Test that transactions from different threads are isolated.

(testrepo: Repository)

Source from the content-addressed store, hash-verified

133
134
135def test_transaction_isolated_across_threads(testrepo: Repository) -> None:
136 """Test that transactions from different threads are isolated."""
137 # Create two test refs
138 ref1_name = 'refs/heads/thread-test-1'
139 ref2_name = 'refs/heads/thread-test-2'
140 target1 = Oid(hex='5ebeeebb320790caf276b9fc8b24546d63316533')
141 target2 = Oid(hex='2be5719152d4f82c7302b1c0932d8e5f0a4a0e98')
142 testrepo.create_reference(ref1_name, target1)
143 testrepo.create_reference(ref2_name, target2)
144
145 results = []
146 errors = []
147 thread1_ref1_locked = threading.Event()
148 thread2_ref2_locked = threading.Event()
149
150 def update_ref1() -> None:
151 try:
152 with testrepo.transaction() as txn:
153 txn.lock_ref(ref1_name)
154 thread1_ref1_locked.set()
155 thread2_ref2_locked.wait(timeout=5)
156 txn.set_target(ref1_name, target2, message='Thread 1 update')
157 results.append('thread1_success')
158 except Exception as e:
159 errors.append(('thread1', str(e)))
160
161 def update_ref2() -> None:
162 try:
163 with testrepo.transaction() as txn:
164 txn.lock_ref(ref2_name)
165 thread2_ref2_locked.set()
166 thread1_ref1_locked.wait(timeout=5)
167 txn.set_target(ref2_name, target1, message='Thread 2 update')
168 results.append('thread2_success')
169 except Exception as e:
170 errors.append(('thread2', str(e)))
171
172 thread1 = threading.Thread(target=update_ref1)
173 thread2 = threading.Thread(target=update_ref2)
174
175 thread1.start()
176 thread2.start()
177 thread1.join()
178 thread2.join()
179
180 # Both threads should succeed - transactions are isolated
181 assert len(errors) == 0, f'Errors: {errors}'
182 assert 'thread1_success' in results
183 assert 'thread2_success' in results
184
185 # Verify both updates were applied
186 ref1 = testrepo.lookup_reference(ref1_name)
187 ref2 = testrepo.lookup_reference(ref2_name)
188 assert str(ref1.target) == '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
189 assert str(ref2.target) == '5ebeeebb320790caf276b9fc8b24546d63316533'
190
191
192def test_transaction_deadlock_prevention(testrepo: Repository) -> None:

Callers

nothing calls this directly

Calls 1

create_referenceMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…