-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathblock_scheduler_test.go
More file actions
272 lines (214 loc) · 8.43 KB
/
block_scheduler_test.go
File metadata and controls
272 lines (214 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex_test
import (
"sync"
"testing"
"time"
"github.com/ava-labs/simplex"
"github.com/ava-labs/simplex/testutil"
"github.com/stretchr/testify/require"
)
const (
defaultMaxDeps = uint64(1000)
defaultWaitDuration = 500 * time.Millisecond
)
func TestBlockVerificationScheduler(t *testing.T) {
t.Run("Schedules immediately when no dependencies", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
wg := sync.WaitGroup{}
wg.Add(1)
task := func() simplex.Digest {
defer wg.Done()
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task, 0, nil, nil))
wg.Wait()
})
t.Run("Defers until prevBlock is satisfied (manual ExecuteBlockDependents)", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
prev := makeDigest(t)
done := make(chan struct{}, 1)
task := func() simplex.Digest {
done <- struct{}{}
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task, 0, &prev, nil))
// Should not run until we satisfy the dependency.
waitNoReceive(t, done)
bvs.ExecuteBlockDependents(prev)
waitReceive(t, done)
})
t.Run("Defers until all emptyRounds are satisfied", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
done := make(chan struct{}, 1)
task := func() simplex.Digest {
done <- struct{}{}
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task, 0, nil, []uint64{11, 22}))
// Removing one is not enough.
bvs.ExecuteEmptyRoundDependents(11)
waitNoReceive(t, done)
// Removing the last one triggers scheduling.
bvs.ExecuteEmptyRoundDependents(22)
waitReceive(t, done)
})
t.Run("Defers until both prevBlock and all emptyRounds are satisfied", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
prev := makeDigest(t)
done := make(chan struct{}, 1)
task := func() simplex.Digest {
done <- struct{}{}
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task, 0, &prev, []uint64{1, 2, 3}))
// Satisfy rounds first — still blocked by prevBlock.
bvs.ExecuteEmptyRoundDependents(1)
bvs.ExecuteEmptyRoundDependents(2)
bvs.ExecuteEmptyRoundDependents(3)
waitNoReceive(t, done)
// Now satisfy the prevBlock and it should schedule.
bvs.ExecuteBlockDependents(prev)
waitReceive(t, done)
})
t.Run("Defers until both prevBlock and all emptyRounds are satisfied swapped order", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
prev := makeDigest(t)
done := make(chan struct{}, 1)
task := func() simplex.Digest {
done <- struct{}{}
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task, 0, &prev, []uint64{1, 2, 3}))
// first satisfy prev block dep
bvs.ExecuteBlockDependents(prev)
waitNoReceive(t, done)
// next satisfy the empty rounds
bvs.ExecuteEmptyRoundDependents(1)
bvs.ExecuteEmptyRoundDependents(2)
bvs.ExecuteEmptyRoundDependents(3)
waitReceive(t, done)
})
t.Run("Chained scheduling via onTaskFinished (A finishes -> B unblocked)", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
// Task A produces digest 'Aout'
Aout := makeDigest(t)
aRan := make(chan struct{}, 1)
taskA := func() simplex.Digest {
aRan <- struct{}{}
return Aout
}
// Task B depends on A's output digest
bRan := make(chan struct{}, 1)
taskB := func() simplex.Digest {
bRan <- struct{}{}
return makeDigest(t)
}
// Schedule B with dependency; it should not run yet.
require.NoError(t, bvs.ScheduleTaskWithDependencies(taskB, 0, &Aout, nil))
waitNoReceive(t, bRan)
// Schedule A with no dependencies; when it finishes, the scheduler's
// onTaskFinished should call ExecuteBlockDependents(Aout), unblocking B.
require.NoError(t, bvs.ScheduleTaskWithDependencies(taskA, 0, nil, nil))
waitReceive(t, aRan)
waitReceive(t, bRan)
})
t.Run("Max pending limit enforced (dependencies + queued)", func(t *testing.T) {
// Set max to 1 so a single pending item trips the limit.
const max = uint64(1)
limitedScheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
noLogger := testutil.MakeLogger(t)
noLogger.Silence() // we silence because CI fails when we get WARN logs but this is expected in this test
bvs := simplex.NewBlockVerificationScheduler(noLogger, max, limitedScheduler)
defer bvs.Close()
prev := makeDigest(t)
// First: add one task with a dependency (it will sit in ds.dependencies)
require.NoError(t, bvs.ScheduleTaskWithDependencies(func() simplex.Digest {
return makeDigest(t)
}, 0, &prev, nil))
// Second: trying to add another should exceed the limit.
err := bvs.ScheduleTaskWithDependencies(func() simplex.Digest {
return makeDigest(t)
}, 0, &prev, nil)
require.ErrorIs(t, err, simplex.ErrTooManyPendingVerifications)
})
t.Run("Multiple unrelated dependency resolutions don't trigger others", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
prev1 := makeDigest(t)
prev2 := makeDigest(t)
done1 := make(chan struct{}, 1)
task1 := func() simplex.Digest {
done1 <- struct{}{}
return makeDigest(t)
}
done2 := make(chan struct{}, 1)
task2 := func() simplex.Digest {
done2 <- struct{}{}
return makeDigest(t)
}
require.NoError(t, bvs.ScheduleTaskWithDependencies(task1, 0, &prev1, []uint64{7}))
require.NoError(t, bvs.ScheduleTaskWithDependencies(task2, 0, &prev2, []uint64{8}))
// Resolve only part of task1's deps; neither should run.
bvs.ExecuteEmptyRoundDependents(7)
waitNoReceive(t, done1)
waitNoReceive(t, done2)
// Resolve prev2 but not its round; still shouldn't run.
bvs.ExecuteBlockDependents(prev2)
waitNoReceive(t, done2)
// Finish task1 by resolving prev1; it should run now.
bvs.ExecuteBlockDependents(prev1)
waitReceive(t, done1)
// Finally resolve task2's round; it should run now.
bvs.ExecuteEmptyRoundDependents(8)
waitReceive(t, done2)
})
t.Run("RemoveOldTasks removes tasks with blockSeq <= finalized seq", func(t *testing.T) {
scheduler := simplex.NewScheduler(testutil.MakeLogger(t), defaultMaxDeps)
bvs := simplex.NewBlockVerificationScheduler(testutil.MakeLogger(t), defaultMaxDeps, scheduler)
defer bvs.Close()
// Both tasks depend on round 10 being cleared, so neither should run yet.
const depRound uint64 = 10
oldRan := make(chan struct{}, 1)
newRan := make(chan struct{}, 1)
taskOld := func() simplex.Digest {
oldRan <- struct{}{}
return makeDigest(t)
}
taskNew := func() simplex.Digest {
newRan <- struct{}{}
return makeDigest(t)
}
// Block sequences: old=5, new=8
// RemoveOldTasks(seq) should drop tasks with blockSeq <= seq.
const oldSeq uint64 = 5
const newSeq uint64 = 8
require.NoError(t, bvs.ScheduleTaskWithDependencies(taskOld, oldSeq, nil, []uint64{depRound}))
require.NoError(t, bvs.ScheduleTaskWithDependencies(taskNew, newSeq, nil, []uint64{depRound}))
// Nothing should run yet.
waitNoReceive(t, oldRan)
waitNoReceive(t, newRan)
// Finalize up to seq=6 — this should remove the old task (seq=5) but keep the new one (seq=8).
bvs.RemoveOldTasks(6)
// Now resolve the dependency round. Only the "new" task should execute.
bvs.ExecuteEmptyRoundDependents(depRound)
// Old was removed, so it must not run.
waitNoReceive(t, oldRan)
// New remains and should be scheduled.
waitReceive(t, newRan)
})
}