|
| 1 | +package syncing |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/evstack/ev-node/block/internal/common" |
| 9 | + "github.com/rs/zerolog" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAsyncDARetriever_RequestRetrieval(t *testing.T) { |
| 15 | + logger := zerolog.Nop() |
| 16 | + mockRetriever := NewMockDARetriever(t) |
| 17 | + resultCh := make(chan common.DAHeightEvent, 10) |
| 18 | + |
| 19 | + asyncRetriever := NewAsyncDARetriever(mockRetriever, resultCh, logger) |
| 20 | + ctx, cancel := context.WithCancel(context.Background()) |
| 21 | + defer cancel() |
| 22 | + |
| 23 | + asyncRetriever.Start(ctx) |
| 24 | + defer asyncRetriever.Stop() |
| 25 | + |
| 26 | + // 1. Test successful retrieval |
| 27 | + height1 := uint64(100) |
| 28 | + mockRetriever.EXPECT().RetrieveFromDA(mock.Anything, height1).Return([]common.DAHeightEvent{{DaHeight: height1}}, nil).Once() |
| 29 | + |
| 30 | + asyncRetriever.RequestRetrieval(height1) |
| 31 | + |
| 32 | + select { |
| 33 | + case event := <-resultCh: |
| 34 | + assert.Equal(t, height1, event.DaHeight) |
| 35 | + case <-time.After(1 * time.Second): |
| 36 | + t.Fatal("timeout waiting for result") |
| 37 | + } |
| 38 | + |
| 39 | + // 2. Test deduplication (idempotency) |
| 40 | + // We'll block the retriever to simulate a slow request, then send multiple requests for the same height |
| 41 | + height2 := uint64(200) |
| 42 | + |
| 43 | + // Create a channel to signal when the mock is called |
| 44 | + calledCh := make(chan struct{}) |
| 45 | + // Create a channel to unblock the mock |
| 46 | + unblockCh := make(chan struct{}) |
| 47 | + |
| 48 | + mockRetriever.EXPECT().RetrieveFromDA(mock.Anything, height2).RunAndReturn(func(ctx context.Context, h uint64) ([]common.DAHeightEvent, error) { |
| 49 | + close(calledCh) |
| 50 | + <-unblockCh |
| 51 | + return []common.DAHeightEvent{{DaHeight: h}}, nil |
| 52 | + }).Once() // Should be called only once despite multiple requests |
| 53 | + |
| 54 | + // Send first request |
| 55 | + asyncRetriever.RequestRetrieval(height2) |
| 56 | + |
| 57 | + // Wait for the worker to pick it up |
| 58 | + select { |
| 59 | + case <-calledCh: |
| 60 | + case <-time.After(1 * time.Second): |
| 61 | + t.Fatal("timeout waiting for retriever call") |
| 62 | + } |
| 63 | + |
| 64 | + // Send duplicate requests while the first one is still in flight |
| 65 | + asyncRetriever.RequestRetrieval(height2) |
| 66 | + asyncRetriever.RequestRetrieval(height2) |
| 67 | + |
| 68 | + // Unblock the worker |
| 69 | + close(unblockCh) |
| 70 | + |
| 71 | + // We should receive exactly one result |
| 72 | + select { |
| 73 | + case event := <-resultCh: |
| 74 | + assert.Equal(t, height2, event.DaHeight) |
| 75 | + case <-time.After(1 * time.Second): |
| 76 | + t.Fatal("timeout waiting for result") |
| 77 | + } |
| 78 | + |
| 79 | + // Ensure no more results come through |
| 80 | + select { |
| 81 | + case <-resultCh: |
| 82 | + t.Fatal("received duplicate result") |
| 83 | + default: |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestAsyncDARetriever_WorkerPoolLimit(t *testing.T) { |
| 88 | + logger := zerolog.Nop() |
| 89 | + mockRetriever := NewMockDARetriever(t) |
| 90 | + resultCh := make(chan common.DAHeightEvent, 100) |
| 91 | + |
| 92 | + asyncRetriever := NewAsyncDARetriever(mockRetriever, resultCh, logger) |
| 93 | + ctx, cancel := context.WithCancel(context.Background()) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + asyncRetriever.Start(ctx) |
| 97 | + defer asyncRetriever.Stop() |
| 98 | + |
| 99 | + // We have 5 workers. We'll block them all. |
| 100 | + unblockCh := make(chan struct{}) |
| 101 | + |
| 102 | + // Expect 5 calls that block |
| 103 | + for i := 0; i < 5; i++ { |
| 104 | + h := uint64(1000 + i) |
| 105 | + mockRetriever.EXPECT().RetrieveFromDA(mock.Anything, h).RunAndReturn(func(ctx context.Context, h uint64) ([]common.DAHeightEvent, error) { |
| 106 | + <-unblockCh |
| 107 | + return []common.DAHeightEvent{{DaHeight: h}}, nil |
| 108 | + }).Once() |
| 109 | + asyncRetriever.RequestRetrieval(h) |
| 110 | + } |
| 111 | + |
| 112 | + // Give workers time to pick up tasks |
| 113 | + time.Sleep(100 * time.Millisecond) |
| 114 | + |
| 115 | + // Now send a 6th request. It should be queued but not processed yet. |
| 116 | + height6 := uint64(1005) |
| 117 | + processed6 := make(chan struct{}) |
| 118 | + mockRetriever.EXPECT().RetrieveFromDA(mock.Anything, height6).RunAndReturn(func(ctx context.Context, h uint64) ([]common.DAHeightEvent, error) { |
| 119 | + close(processed6) |
| 120 | + return []common.DAHeightEvent{{DaHeight: h}}, nil |
| 121 | + }).Once() |
| 122 | + |
| 123 | + asyncRetriever.RequestRetrieval(height6) |
| 124 | + |
| 125 | + // Ensure 6th request is NOT processed yet |
| 126 | + select { |
| 127 | + case <-processed6: |
| 128 | + t.Fatal("6th request processed too early") |
| 129 | + default: |
| 130 | + } |
| 131 | + |
| 132 | + // Unblock workers |
| 133 | + close(unblockCh) |
| 134 | + |
| 135 | + // Now 6th request should be processed |
| 136 | + select { |
| 137 | + case <-processed6: |
| 138 | + case <-time.After(1 * time.Second): |
| 139 | + t.Fatal("timeout waiting for 6th request") |
| 140 | + } |
| 141 | +} |
0 commit comments