1
1
"""Test suite for blobs."""
2
2
3
3
import copy
4
+ import time
4
5
5
6
import pytest
7
+ from filelock import FileLock
6
8
7
9
from ethereum_test_forks import (
8
10
Cancun ,
18
20
from ..blob_types import CACHED_BLOBS_DIRECTORY , Blob , clear_blob_cache
19
21
20
22
23
+ def increment_counter (timeout : float = 10 ):
24
+ """
25
+ Increment counter in file, creating if doesn't exist.
26
+
27
+ This is needed because we require the unit test 'test_transition_fork_blobs' to run
28
+ at the end without having to include another dependency for ordering tests.
29
+ That test has to run at the end because it assumes that no json blobs not created
30
+ by itself are created while it is running.
31
+
32
+ The hardcoded counter value in the test above has to be updated if any new blob_related
33
+ unit tests that create json blobs are added in the future.
34
+
35
+ """
36
+ file_path = CACHED_BLOBS_DIRECTORY / "blob_unit_test_counter.txt"
37
+ lock_file = file_path .with_suffix (".lock" )
38
+
39
+ with FileLock (lock_file , timeout = timeout ):
40
+ # Read current value or start at 0
41
+ if file_path .exists ():
42
+ current_value = int (file_path .read_text ().strip ())
43
+ else :
44
+ current_value = 0
45
+
46
+ # Increment and write back
47
+ new_value = current_value + 1
48
+ file_path .write_text (str (new_value ))
49
+
50
+ return new_value
51
+
52
+
53
+ def wait_until_counter_reached (target : int , poll_interval : float = 0.1 ):
54
+ """Wait until blob unit test counter reaches target value."""
55
+ file_path = CACHED_BLOBS_DIRECTORY / "blob_unit_test_counter.txt"
56
+ lock_file = file_path .with_suffix (".lock" ) # Add lock file path
57
+
58
+ while True :
59
+ # Use FileLock when reading!
60
+ with FileLock (lock_file , timeout = 10 ):
61
+ if file_path .exists ():
62
+ try :
63
+ current_value = int (file_path .read_text ().strip ())
64
+ if current_value == target :
65
+ # file_path.unlink() # get rid to effectively reset counter to 0
66
+ return current_value
67
+ elif current_value > target :
68
+ pytest .fail (
69
+ f"The blob_unit_test lock counter is too high! "
70
+ f"Expected { target } , but got { current_value } . "
71
+ f"It probably reused an existing file that was not cleared. "
72
+ f"Delete { file_path } manually to fix this."
73
+ )
74
+ except Exception :
75
+ current_value = 0
76
+ else :
77
+ current_value = 0
78
+
79
+ time .sleep (poll_interval )
80
+
81
+
21
82
@pytest .mark .parametrize ("seed" , [0 , 10 , 100 ])
22
83
@pytest .mark .parametrize ("fork" , [Cancun , Prague , Osaka ])
23
84
def test_blob_creation_and_writing_and_reading (
@@ -42,6 +103,8 @@ def test_blob_creation_and_writing_and_reading(
42
103
# ensure file you read equals file you wrote
43
104
assert b .model_dump () == restored .model_dump ()
44
105
106
+ increment_counter ()
107
+
45
108
46
109
@pytest .mark .parametrize (
47
110
"corruption_mode" ,
@@ -71,6 +134,8 @@ def test_blob_proof_corruption(
71
134
"proof is unchanged!"
72
135
)
73
136
137
+ increment_counter ()
138
+
74
139
75
140
@pytest .mark .parametrize ("timestamp" , [14999 , 15000 ])
76
141
@pytest .mark .parametrize (
@@ -81,6 +146,9 @@ def test_transition_fork_blobs(
81
146
timestamp ,
82
147
):
83
148
"""Generates blobs for transition forks (time 14999 is old fork, time 15000 is new fork)."""
149
+ # line below guarantees that this test runs only after the other blob unit tests are done
150
+ wait_until_counter_reached (21 )
151
+
84
152
clear_blob_cache (CACHED_BLOBS_DIRECTORY )
85
153
86
154
print (f"Original fork: { fork } , Timestamp: { timestamp } " )
@@ -109,3 +177,7 @@ def test_transition_fork_blobs(
109
177
f"Transition fork failure! Fork { fork .name ()} at timestamp: { timestamp } should have "
110
178
f"transitioned to { post_transition_fork_at_15k .name ()} but is still at { b .fork .name ()} "
111
179
)
180
+
181
+ # delete counter at last iteration (otherwise re-running all unit tests will fail)
182
+ if timestamp == 15_000 and pre_transition_fork == Prague :
183
+ (CACHED_BLOBS_DIRECTORY / "blob_unit_test_counter.txt" ).unlink ()
0 commit comments