@@ -2,7 +2,11 @@ package block
22
33import (
44 "context"
5+ "sync"
6+ "testing"
57 "time"
8+
9+ "github.com/stretchr/testify/require"
610)
711
812// AggregationLoop is responsible for aggregating transactions into rollup-blocks.
@@ -123,3 +127,62 @@ func getRemainingSleep(start time.Time, interval time.Duration) time.Duration {
123127
124128 return time .Millisecond
125129}
130+
131+ // TestNormalAggregationLoop_TxNotification tests that transaction notifications are handled in normal mode
132+ func TestNormalAggregationLoop_TxNotification (t * testing.T ) {
133+ require := require .New (t )
134+
135+ blockTime := 100 * time .Millisecond
136+ m , pubMock := setupTestManager (t , blockTime , 0 )
137+ m .config .Node .LazyMode = false
138+
139+ // Create the notification channel
140+ m .txNotifyCh = make (chan struct {}, 1 )
141+
142+ ctx , cancel := context .WithCancel (context .Background ())
143+ defer cancel ()
144+
145+ var wg sync.WaitGroup
146+ wg .Add (1 )
147+ go func () {
148+ defer wg .Done ()
149+ blockTimer := time .NewTimer (blockTime )
150+ defer blockTimer .Stop ()
151+ m .normalAggregationLoop (ctx , blockTimer )
152+ }()
153+
154+ // Wait for the first block to be published by the timer
155+ select {
156+ case <- pubMock .calls :
157+ // Block was published by timer, which is expected
158+ case <- time .After (blockTime * 2 ):
159+ require .Fail ("Block was not published by timer" )
160+ }
161+
162+ // Reset the publish mock to track new calls
163+ pubMock .reset ()
164+
165+ // Send a transaction notification
166+ m .NotifyNewTransactions ()
167+
168+ // In normal mode, the notification should not trigger an immediate block
169+ select {
170+ case <- pubMock .calls :
171+ // If we enable the optional enhancement to reset the timer, this might happen
172+ // But with the current implementation, this should not happen
173+ require .Fail ("Block was published immediately after notification in normal mode" )
174+ case <- time .After (blockTime / 2 ):
175+ // This is expected - no immediate block
176+ }
177+
178+ // Wait for the next regular block
179+ select {
180+ case <- pubMock .calls :
181+ // Block was published by timer, which is expected
182+ case <- time .After (blockTime * 2 ):
183+ require .Fail ("Block was not published by timer after notification" )
184+ }
185+
186+ cancel ()
187+ wg .Wait ()
188+ }
0 commit comments