|
| 1 | +package chanutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +const waitTime = time.Second * 5 |
| 14 | + |
| 15 | +// TestBatchWriter tests that the BatchWriter behaves as expected. |
| 16 | +func TestBatchWriter(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + rand.Seed(time.Now().UnixNano()) |
| 19 | + |
| 20 | + // waitForItems is a helper function that will wait for a given set of |
| 21 | + // items to appear in the db. |
| 22 | + waitForItems := func(db *mockItemsDB, items ...*item) { |
| 23 | + err := waitFor(func() bool { |
| 24 | + return db.hasItems(items...) |
| 25 | + }, waitTime) |
| 26 | + require.NoError(t, err) |
| 27 | + } |
| 28 | + |
| 29 | + t.Run("filters persisted after ticker", func(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + // Create a mock filters DB. |
| 33 | + db := newMockItemsDB() |
| 34 | + |
| 35 | + // Construct a new BatchWriter backed by the mock db. |
| 36 | + b := NewBatchWriter[*item](&BatchWriterConfig[*item]{ |
| 37 | + QueueBufferSize: 10, |
| 38 | + MaxBatch: 20, |
| 39 | + DBWritesTickerDuration: time.Millisecond * 500, |
| 40 | + PutItems: db.PutItems, |
| 41 | + }) |
| 42 | + b.Start() |
| 43 | + t.Cleanup(b.Stop) |
| 44 | + |
| 45 | + fs := genFilterSet(5) |
| 46 | + for _, f := range fs { |
| 47 | + b.AddItem(f) |
| 48 | + } |
| 49 | + waitForItems(db, fs...) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("write once threshold is reached", func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + // Create a mock filters DB. |
| 56 | + db := newMockItemsDB() |
| 57 | + |
| 58 | + // Construct a new BatchWriter backed by the mock db. |
| 59 | + // Make the DB writes ticker duration extra long so that we |
| 60 | + // can explicitly test that the batch gets persisted if the |
| 61 | + // MaxBatch threshold is reached. |
| 62 | + b := NewBatchWriter[*item](&BatchWriterConfig[*item]{ |
| 63 | + QueueBufferSize: 10, |
| 64 | + MaxBatch: 20, |
| 65 | + DBWritesTickerDuration: time.Hour, |
| 66 | + PutItems: db.PutItems, |
| 67 | + }) |
| 68 | + b.Start() |
| 69 | + t.Cleanup(b.Stop) |
| 70 | + |
| 71 | + // Generate 30 filters and add each one to the batch writer. |
| 72 | + fs := genFilterSet(30) |
| 73 | + for _, f := range fs { |
| 74 | + b.AddItem(f) |
| 75 | + } |
| 76 | + |
| 77 | + // Since the MaxBatch threshold has been reached, we expect the |
| 78 | + // first 20 filters to be persisted. |
| 79 | + waitForItems(db, fs[:20]...) |
| 80 | + |
| 81 | + // Since the last 10 filters don't reach the threshold and since |
| 82 | + // the ticker has definitely not ticked yet, we don't expect the |
| 83 | + // last 10 filters to be in the db yet. |
| 84 | + require.False(t, db.hasItems(fs[21:]...)) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("stress test", func(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + // Create a mock filters DB. |
| 91 | + db := newMockItemsDB() |
| 92 | + |
| 93 | + // Construct a new BatchWriter backed by the mock db. |
| 94 | + // Make the DB writes ticker duration extra long so that we |
| 95 | + // can explicitly test that the batch gets persisted if the |
| 96 | + // MaxBatch threshold is reached. |
| 97 | + b := NewBatchWriter[*item](&BatchWriterConfig[*item]{ |
| 98 | + QueueBufferSize: 5, |
| 99 | + MaxBatch: 5, |
| 100 | + DBWritesTickerDuration: time.Millisecond * 2, |
| 101 | + PutItems: db.PutItems, |
| 102 | + }) |
| 103 | + b.Start() |
| 104 | + t.Cleanup(b.Stop) |
| 105 | + |
| 106 | + // Generate lots of filters and add each to the batch writer. |
| 107 | + // Sleep for a bit between each filter to ensure that we |
| 108 | + // sometimes hit the timeout write and sometimes the threshold |
| 109 | + // write. |
| 110 | + fs := genFilterSet(1000) |
| 111 | + for _, f := range fs { |
| 112 | + b.AddItem(f) |
| 113 | + |
| 114 | + n := rand.Intn(3) |
| 115 | + time.Sleep(time.Duration(n) * time.Millisecond) |
| 116 | + } |
| 117 | + |
| 118 | + // Since the MaxBatch threshold has been reached, we expect the |
| 119 | + // first 20 filters to be persisted. |
| 120 | + waitForItems(db, fs...) |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +type item struct { |
| 125 | + i int |
| 126 | +} |
| 127 | + |
| 128 | +// mockItemsDB is a mock DB that holds a set of items. |
| 129 | +type mockItemsDB struct { |
| 130 | + items map[int]bool |
| 131 | + mu sync.Mutex |
| 132 | +} |
| 133 | + |
| 134 | +// newMockItemsDB constructs a new mockItemsDB. |
| 135 | +func newMockItemsDB() *mockItemsDB { |
| 136 | + return &mockItemsDB{ |
| 137 | + items: make(map[int]bool), |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// hasItems returns true if the db contains all the given items. |
| 142 | +func (m *mockItemsDB) hasItems(items ...*item) bool { |
| 143 | + m.mu.Lock() |
| 144 | + defer m.mu.Unlock() |
| 145 | + |
| 146 | + for _, i := range items { |
| 147 | + _, ok := m.items[i.i] |
| 148 | + if !ok { |
| 149 | + return false |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return true |
| 154 | +} |
| 155 | + |
| 156 | +// PutItems adds a set of items to the db. |
| 157 | +func (m *mockItemsDB) PutItems(items ...*item) error { |
| 158 | + m.mu.Lock() |
| 159 | + defer m.mu.Unlock() |
| 160 | + |
| 161 | + for _, i := range items { |
| 162 | + m.items[i.i] = true |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// genItemSet generates a set of numFilters items. |
| 169 | +func genFilterSet(numFilters int) []*item { |
| 170 | + res := make([]*item, numFilters) |
| 171 | + for i := 0; i < numFilters; i++ { |
| 172 | + res[i] = &item{i: i} |
| 173 | + } |
| 174 | + |
| 175 | + return res |
| 176 | +} |
| 177 | + |
| 178 | +// pollInterval is a constant specifying a 200 ms interval. |
| 179 | +const pollInterval = 200 * time.Millisecond |
| 180 | + |
| 181 | +// waitFor is a helper test function that will wait for a timeout period of |
| 182 | +// time until the passed predicate returns true. This function is helpful as |
| 183 | +// timing doesn't always line up well when running integration tests with |
| 184 | +// several running lnd nodes. This function gives callers a way to assert that |
| 185 | +// some property is upheld within a particular time frame. |
| 186 | +func waitFor(pred func() bool, timeout time.Duration) error { |
| 187 | + exitTimer := time.After(timeout) |
| 188 | + result := make(chan bool, 1) |
| 189 | + |
| 190 | + for { |
| 191 | + <-time.After(pollInterval) |
| 192 | + |
| 193 | + go func() { |
| 194 | + result <- pred() |
| 195 | + }() |
| 196 | + |
| 197 | + // Each time we call the pred(), we expect a result to be |
| 198 | + // returned otherwise it will timeout. |
| 199 | + select { |
| 200 | + case <-exitTimer: |
| 201 | + return fmt.Errorf("predicate not satisfied after " + |
| 202 | + "time out") |
| 203 | + |
| 204 | + case succeed := <-result: |
| 205 | + if succeed { |
| 206 | + return nil |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments