-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslot_selector_test.go
More file actions
318 lines (273 loc) · 9.09 KB
/
slot_selector_test.go
File metadata and controls
318 lines (273 loc) · 9.09 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package dasmon
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSlotSelector_EdgeCases(t *testing.T) {
tests := []struct {
name string
strategy SlotSelectionStrategy
earliest uint64
latest uint64
want uint64
}{
{
name: "uniform - empty range (equal slots)",
strategy: SlotSelectionUniform,
earliest: 100,
latest: 100,
want: 100,
},
{
name: "uniform - single slot range",
strategy: SlotSelectionUniform,
earliest: 100,
latest: 101,
want: 100,
},
{
name: "recent - empty range (equal slots)",
strategy: SlotSelectionRecent,
earliest: 100,
latest: 100,
want: 100,
},
{
name: "recent - single slot range",
strategy: SlotSelectionRecent,
earliest: 100,
latest: 101,
want: 100,
},
{
name: "historical - empty range (equal slots)",
strategy: SlotSelectionHistorical,
earliest: 100,
latest: 100,
want: 100,
},
{
name: "historical - single slot range",
strategy: SlotSelectionHistorical,
earliest: 100,
latest: 101,
want: 100,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
selector, err := ParseSlotSelector(tt.strategy)
require.NoError(t, err)
result := selector.SelectSlot(Range[uint64]{Start: tt.earliest, End: tt.latest})
assert.Equal(t, tt.want, result, "expected slot %d, got %d", tt.want, result)
})
}
}
func TestSlotSelector_ValidRange(t *testing.T) {
strategies := []SlotSelectionStrategy{
SlotSelectionUniform,
SlotSelectionRecent,
SlotSelectionHistorical,
}
testCases := []struct {
name string
earliest uint64
latest uint64
}{
{"small range", 100, 110},
{"medium range", 1000, 2000},
{"large range", 10000, 20000},
{"very large range", 0, 100000},
}
for _, strategy := range strategies {
for _, tc := range testCases {
t.Run(string(strategy)+"_"+tc.name, func(t *testing.T) {
selector, err := ParseSlotSelector(strategy)
require.NoError(t, err)
// Run multiple iterations to ensure all results are in valid range
for i := 0; i < 1000; i++ {
result := selector.SelectSlot(Range[uint64]{Start: tc.earliest, End: tc.latest})
assert.GreaterOrEqual(t, result, tc.earliest,
"result %d should be >= earliest %d", result, tc.earliest)
assert.Less(t, result, tc.latest,
"result %d should be < latest %d", result, tc.latest)
}
})
}
}
}
func TestSlotSelector_UniformDistribution(t *testing.T) {
selector, err := ParseSlotSelector(SlotSelectionUniform)
require.NoError(t, err)
earliest := uint64(1000)
latest := uint64(2000)
slotRange := int(latest - earliest)
// Number of samples for statistical test
numSamples := 100000
// Create buckets for histogram
numBuckets := 10
bucketSize := slotRange / numBuckets
buckets := make([]int, numBuckets)
// Collect samples
for i := 0; i < numSamples; i++ {
result := selector.SelectSlot(Range[uint64]{Start: earliest, End: latest})
bucketIdx := int(result-earliest) / bucketSize
if bucketIdx >= numBuckets {
bucketIdx = numBuckets - 1
}
buckets[bucketIdx]++
}
// Expected count per bucket for uniform distribution
expectedPerBucket := float64(numSamples) / float64(numBuckets)
// Allow 10% deviation from expected count (this is a reasonable tolerance for randomness)
tolerance := expectedPerBucket * 0.10
// Check each bucket is within tolerance
for i, count := range buckets {
diff := math.Abs(float64(count) - expectedPerBucket)
assert.Less(t, diff, tolerance,
"bucket %d has count %d, expected ~%.0f (±%.0f)",
i, count, expectedPerBucket, tolerance)
}
// Chi-squared test for goodness of fit (optional, more rigorous)
chiSquared := 0.0
for _, count := range buckets {
diff := float64(count) - expectedPerBucket
chiSquared += (diff * diff) / expectedPerBucket
}
// For 10 buckets (9 degrees of freedom), chi-squared critical value at 95% confidence is ~16.92
// We use a more lenient threshold of 20 to account for randomness
assert.Less(t, chiSquared, 20.0,
"chi-squared value %.2f indicates non-uniform distribution", chiSquared)
}
func TestSlotSelector_RecentBias(t *testing.T) {
selector, err := ParseSlotSelector(SlotSelectionRecent)
require.NoError(t, err)
earliest := uint64(1000)
latest := uint64(2000)
slotRange := int(latest - earliest)
numSamples := 10000
// Divide range into two halves: recent (upper half) and historical (lower half)
midpoint := earliest + uint64(slotRange/2)
recentCount := 0
historicalCount := 0
// Collect samples
for i := 0; i < numSamples; i++ {
result := selector.SelectSlot(Range[uint64]{Start: earliest, End: latest})
if result >= midpoint {
recentCount++
} else {
historicalCount++
}
}
// Recent strategy should have significantly more samples in the recent half
// We expect at least 60% of samples in the recent half due to exponential bias
minRecentPercentage := 0.60
actualRecentPercentage := float64(recentCount) / float64(numSamples)
assert.Greater(t, actualRecentPercentage, minRecentPercentage,
"recent strategy should bias toward recent slots: got %.2f%% recent, expected >%.0f%%",
actualRecentPercentage*100, minRecentPercentage*100)
t.Logf("Recent strategy: %.2f%% recent, %.2f%% historical (expected >%.0f%% recent)",
actualRecentPercentage*100,
float64(historicalCount)/float64(numSamples)*100,
minRecentPercentage*100)
}
func TestSlotSelector_HistoricalBias(t *testing.T) {
selector, err := ParseSlotSelector(SlotSelectionHistorical)
require.NoError(t, err)
earliest := uint64(1000)
latest := uint64(2000)
slotRange := int(latest - earliest)
numSamples := 10000
// Divide range into two halves: recent (upper half) and historical (lower half)
midpoint := earliest + uint64(slotRange/2)
recentCount := 0
historicalCount := 0
// Collect samples
for i := 0; i < numSamples; i++ {
result := selector.SelectSlot(Range[uint64]{Start: earliest, End: latest})
if result >= midpoint {
recentCount++
} else {
historicalCount++
}
}
// Historical strategy should have significantly more samples in the historical half
// We expect at least 60% of samples in the historical half due to exponential bias
minHistoricalPercentage := 0.60
actualHistoricalPercentage := float64(historicalCount) / float64(numSamples)
assert.Greater(t, actualHistoricalPercentage, minHistoricalPercentage,
"historical strategy should bias toward historical slots: got %.2f%% historical, expected >%.0f%%",
actualHistoricalPercentage*100, minHistoricalPercentage*100)
t.Logf("Historical strategy: %.2f%% historical, %.2f%% recent (expected >%.0f%% historical)",
actualHistoricalPercentage*100,
float64(recentCount)/float64(numSamples)*100,
minHistoricalPercentage*100)
}
func TestSlotSelector_StrategyDifferences(t *testing.T) {
// This test verifies that different strategies produce different distributions
earliest := uint64(1000)
latest := uint64(2000)
slotRange := int(latest - earliest)
numSamples := 10000
midpoint := earliest + uint64(slotRange/2)
strategies := []struct {
name string
strategy SlotSelectionStrategy
}{
{"uniform", SlotSelectionUniform},
{"recent", SlotSelectionRecent},
{"historical", SlotSelectionHistorical},
}
type distribution struct {
recentPct float64
historicalPct float64
}
distributions := make(map[string]distribution)
// Collect distributions for each strategy
for _, s := range strategies {
selector, err := ParseSlotSelector(s.strategy)
require.NoError(t, err)
recentCount := 0
historicalCount := 0
for i := 0; i < numSamples; i++ {
result := selector.SelectSlot(Range[uint64]{Start: earliest, End: latest})
if result >= midpoint {
recentCount++
} else {
historicalCount++
}
}
distributions[s.name] = distribution{
recentPct: float64(recentCount) / float64(numSamples),
historicalPct: float64(historicalCount) / float64(numSamples),
}
}
// Verify distributions are different
uniform := distributions["uniform"]
recent := distributions["recent"]
historical := distributions["historical"]
// Uniform should be close to 50/50
assert.InDelta(t, 0.5, uniform.recentPct, 0.05,
"uniform distribution should be ~50%% recent")
// Recent should be biased toward recent
assert.Greater(t, recent.recentPct, uniform.recentPct,
"recent strategy should have more recent samples than uniform")
// Historical should be biased toward historical
assert.Greater(t, historical.historicalPct, uniform.historicalPct,
"historical strategy should have more historical samples than uniform")
t.Logf("Distribution comparison:")
t.Logf(" Uniform: %.2f%% recent, %.2f%% historical",
uniform.recentPct*100, uniform.historicalPct*100)
t.Logf(" Recent: %.2f%% recent, %.2f%% historical",
recent.recentPct*100, recent.historicalPct*100)
t.Logf(" Historical: %.2f%% recent, %.2f%% historical",
historical.recentPct*100, historical.historicalPct*100)
}
func TestSlotSelector_UnknownStrategy(t *testing.T) {
// Test that an unknown strategy returns an error
selector, err := ParseSlotSelector("unknown")
assert.Error(t, err)
assert.Nil(t, selector)
}