Skip to content

Commit fddf780

Browse files
author
tac0turtle
committed
move normal aggregation to aggration file
1 parent 86c62bd commit fddf780

2 files changed

Lines changed: 63 additions & 59 deletions

File tree

block/aggregation.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package block
22

33
import (
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+
}

block/lazy_aggregation_test.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -342,62 +342,3 @@ func TestEmptyBlockCreation(t *testing.T) {
342342
require.NotNil(capturedCtx, "Context should have been captured by mock publish function")
343343
require.Equal(ctx, capturedCtx, "Context should match the one passed to produceBlock")
344344
}
345-
346-
// TestNormalAggregationLoop_TxNotification tests that transaction notifications are handled in normal mode
347-
func TestNormalAggregationLoop_TxNotification(t *testing.T) {
348-
require := require.New(t)
349-
350-
blockTime := 100 * time.Millisecond
351-
m, pubMock := setupTestManager(t, blockTime, 0)
352-
m.config.Node.LazyMode = false
353-
354-
// Create the notification channel
355-
m.txNotifyCh = make(chan struct{}, 1)
356-
357-
ctx, cancel := context.WithCancel(context.Background())
358-
defer cancel()
359-
360-
var wg sync.WaitGroup
361-
wg.Add(1)
362-
go func() {
363-
defer wg.Done()
364-
blockTimer := time.NewTimer(blockTime)
365-
defer blockTimer.Stop()
366-
m.normalAggregationLoop(ctx, blockTimer)
367-
}()
368-
369-
// Wait for the first block to be published by the timer
370-
select {
371-
case <-pubMock.calls:
372-
// Block was published by timer, which is expected
373-
case <-time.After(blockTime * 2):
374-
require.Fail("Block was not published by timer")
375-
}
376-
377-
// Reset the publish mock to track new calls
378-
pubMock.reset()
379-
380-
// Send a transaction notification
381-
m.NotifyNewTransactions()
382-
383-
// In normal mode, the notification should not trigger an immediate block
384-
select {
385-
case <-pubMock.calls:
386-
// If we enable the optional enhancement to reset the timer, this might happen
387-
// But with the current implementation, this should not happen
388-
require.Fail("Block was published immediately after notification in normal mode")
389-
case <-time.After(blockTime / 2):
390-
// This is expected - no immediate block
391-
}
392-
393-
// Wait for the next regular block
394-
select {
395-
case <-pubMock.calls:
396-
// Block was published by timer, which is expected
397-
case <-time.After(blockTime * 2):
398-
require.Fail("Block was not published by timer after notification")
399-
}
400-
401-
cancel()
402-
wg.Wait()
403-
}

0 commit comments

Comments
 (0)