@@ -252,39 +252,30 @@ def test_add_spent(self):
252252 # that the handle_* functions succeeded.
253253 EXPECTED_HANDLE_ADD_SUCCESS = 2
254254 EXPECTED_HANDLE_SPENT_SUCCESS = 1
255- handle_add_succeeds = 0
256- handle_spent_succeeds = 0
257255
258- expected_utxocache_spents = []
259256 expected_utxocache_adds = []
257+ expected_utxocache_spents = []
258+
259+ actual_utxocache_adds = []
260+ actual_utxocache_spents = []
260261
261262 def compare_utxo_with_event (utxo , event ):
262- """Returns 1 if a utxo is identical to the event produced by BPF, otherwise"""
263- try :
264- assert_equal (utxo ["txid" ], bytes (event .txid [::- 1 ]).hex ())
265- assert_equal (utxo ["index" ], event .index )
266- assert_equal (utxo ["height" ], event .height )
267- assert_equal (utxo ["value" ], event .value )
268- assert_equal (utxo ["is_coinbase" ], event .is_coinbase )
269- except AssertionError :
270- self .log .exception ("Assertion failed" )
271- return 0
272- else :
273- return 1
263+ """Compare a utxo dict to the event produced by BPF"""
264+ assert_equal (utxo ["txid" ], bytes (event .txid [::- 1 ]).hex ())
265+ assert_equal (utxo ["index" ], event .index )
266+ assert_equal (utxo ["height" ], event .height )
267+ assert_equal (utxo ["value" ], event .value )
268+ assert_equal (utxo ["is_coinbase" ], event .is_coinbase )
274269
275270 def handle_utxocache_add (_ , data , __ ):
276- nonlocal handle_add_succeeds
277271 event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
278272 self .log .info (f"handle_utxocache_add(): { event } " )
279- add = expected_utxocache_adds .pop (0 )
280- handle_add_succeeds += compare_utxo_with_event (add , event )
273+ actual_utxocache_adds .append (event )
281274
282275 def handle_utxocache_spent (_ , data , __ ):
283- nonlocal handle_spent_succeeds
284276 event = ctypes .cast (data , ctypes .POINTER (UTXOCacheChange )).contents
285277 self .log .info (f"handle_utxocache_spent(): { event } " )
286- spent = expected_utxocache_spents .pop (0 )
287- handle_spent_succeeds += compare_utxo_with_event (spent , event )
278+ actual_utxocache_spents .append (event )
288279
289280 bpf ["utxocache_add" ].open_perf_buffer (handle_utxocache_add )
290281 bpf ["utxocache_spent" ].open_perf_buffer (handle_utxocache_spent )
@@ -320,19 +311,18 @@ def handle_utxocache_spent(_, data, __):
320311 "is_coinbase" : block_index == 0 ,
321312 })
322313
323- assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , len (expected_utxocache_adds ))
324- assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS ,
325- len (expected_utxocache_spents ))
326-
327314 bpf .perf_buffer_poll (timeout = 200 )
328- bpf .cleanup ()
315+
316+ assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , len (expected_utxocache_adds ), len (actual_utxocache_adds ))
317+ assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS , len (expected_utxocache_spents ), len (actual_utxocache_spents ))
329318
330319 self .log .info (
331320 f"check that we successfully traced { EXPECTED_HANDLE_ADD_SUCCESS } adds and { EXPECTED_HANDLE_SPENT_SUCCESS } spent" )
332- assert_equal (0 , len (expected_utxocache_adds ))
333- assert_equal (0 , len (expected_utxocache_spents ))
334- assert_equal (EXPECTED_HANDLE_ADD_SUCCESS , handle_add_succeeds )
335- assert_equal (EXPECTED_HANDLE_SPENT_SUCCESS , handle_spent_succeeds )
321+ for expected_utxo , actual_event in zip (expected_utxocache_adds + expected_utxocache_spents ,
322+ actual_utxocache_adds + actual_utxocache_spents ):
323+ compare_utxo_with_event (expected_utxo , actual_event )
324+
325+ bpf .cleanup ()
336326
337327 def test_flush (self ):
338328 """ Tests the utxocache:flush tracepoint API.
0 commit comments