mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-07 20:43:57 -05:00
* Ran gopls modernize to fix everything go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./... * Override rules_go provided dependency for golang.org/x/tools to v0.38.0. To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches. * Fix buildtag violations and ignore buildtag violations in external * Introduce modernize analyzer package. * Add modernize "any" analyzer. * Fix violations of any analyzer * Add modernize "appendclipped" analyzer. * Fix violations of appendclipped * Add modernize "bloop" analyzer. * Add modernize "fmtappendf" analyzer. * Add modernize "forvar" analyzer. * Add modernize "mapsloop" analyzer. * Add modernize "minmax" analyzer. * Fix violations of minmax analyzer * Add modernize "omitzero" analyzer. * Add modernize "rangeint" analyzer. * Fix violations of rangeint. * Add modernize "reflecttypefor" analyzer. * Fix violations of reflecttypefor analyzer. * Add modernize "slicescontains" analyzer. * Add modernize "slicessort" analyzer. * Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686. * Add modernize "stringscutprefix" analyzer. * Add modernize "stringsbuilder" analyzer. * Fix violations of stringsbuilder analyzer. * Add modernize "stringsseq" analyzer. * Add modernize "testingcontext" analyzer. * Add modernize "waitgroup" analyzer. * Changelog fragment * gofmt * gazelle * Add modernize "newexpr" analyzer. * Disable newexpr until go1.26 * Add more details in WORKSPACE on how to update the override * @nalepae feedback on min() * gofmt * Fix violations of forvar
182 lines
4.0 KiB
Go
182 lines
4.0 KiB
Go
package leakybucket
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Arbitrary start time.
|
|
var start = time.Date(1990, 1, 2, 0, 0, 0, 0, time.UTC).Round(0)
|
|
var elapsed atomic.Int64
|
|
|
|
// We provide atomic access to elapsed to avoid data races between multiple
|
|
// concurrent goroutines during the tests.
|
|
func getElapsed() time.Duration {
|
|
return time.Duration(elapsed.Load())
|
|
}
|
|
func setElapsed(v time.Duration) {
|
|
elapsed.Store(int64(v))
|
|
}
|
|
func addToElapsed(v time.Duration) {
|
|
elapsed.Add(int64(v))
|
|
}
|
|
|
|
func reset(t *testing.T, c *Collector) {
|
|
c.Reset()
|
|
setElapsed(0)
|
|
}
|
|
|
|
func TestNewLeakyBucket(t *testing.T) {
|
|
rate := 1.0
|
|
capacity := int64(5)
|
|
b := NewLeakyBucket(rate, capacity, time.Second)
|
|
|
|
if b.p != now() {
|
|
t.Fatal("Didn't initialize priority?!")
|
|
}
|
|
if b.rate != rate || b.Rate() != rate {
|
|
t.Fatal("Wrong rate?!")
|
|
}
|
|
if b.capacity != capacity || b.Capacity() != capacity {
|
|
t.Fatal("Wrong capacity?!")
|
|
}
|
|
}
|
|
|
|
type actionSet struct {
|
|
count int64
|
|
action string
|
|
value any
|
|
}
|
|
|
|
type testSet struct {
|
|
capacity int64
|
|
rate float64
|
|
set []actionSet
|
|
}
|
|
|
|
var oneAtaTime = testSet{
|
|
capacity: 5,
|
|
rate: 1.0,
|
|
set: []actionSet{
|
|
{},
|
|
{1, "add", 1},
|
|
{1, "time-set", time.Nanosecond},
|
|
{1, "till", time.Second - time.Nanosecond},
|
|
{1, "time-set", time.Second - time.Nanosecond},
|
|
{1, "till", time.Nanosecond},
|
|
{0, "time-set", time.Second},
|
|
{0, "till", time.Duration(0)},
|
|
|
|
// Add a couple.
|
|
{1, "add", 1},
|
|
{1, "time-add", time.Second / 2},
|
|
{1, "till", time.Second / 2},
|
|
{2, "add", 1},
|
|
{2, "time-add", time.Second/2 - time.Nanosecond},
|
|
|
|
// Monkey with the capacity and make sure Count()/TillEmpty() are
|
|
// adjusted as needed.
|
|
{2, "cap", 5 + 1},
|
|
{2, "till", time.Second + time.Nanosecond},
|
|
{2, "cap", 5 - 1},
|
|
{2, "till", time.Second + time.Nanosecond},
|
|
{1, "cap", 1},
|
|
{1, "till", time.Second},
|
|
{1, "cap", 4},
|
|
{1, "till", time.Second},
|
|
|
|
// Test the full cases.
|
|
{0, "time-add", time.Second * time.Duration(5)},
|
|
{1, "add", 1},
|
|
{2, "add", 1},
|
|
{3, "add", 1},
|
|
{4, "add", 1},
|
|
{4, "add", 1},
|
|
{4, "till", time.Second * 4},
|
|
},
|
|
}
|
|
|
|
var varied = testSet{
|
|
capacity: 1000,
|
|
rate: 60.0,
|
|
set: []actionSet{
|
|
{},
|
|
{100, "add", 100},
|
|
{100, "time-set", time.Nanosecond},
|
|
{1000, "add", 1000},
|
|
{1000, "add", 1},
|
|
{940, "time-set", time.Second},
|
|
},
|
|
}
|
|
|
|
func runTest(t *testing.T, test *testSet) {
|
|
setElapsed(0)
|
|
b := NewLeakyBucket(test.rate, test.capacity, time.Second)
|
|
|
|
for i, v := range test.set {
|
|
switch v.action {
|
|
case "add":
|
|
count := b.Count()
|
|
remaining := test.capacity - count
|
|
amount := int64(v.value.(int))
|
|
n := b.Add(amount)
|
|
if n < amount {
|
|
// The bucket should be full now.
|
|
if n < remaining {
|
|
t.Fatalf("Test %d: Bucket should have been filled by this Add()?!", i)
|
|
}
|
|
}
|
|
case "time-set":
|
|
setElapsed(v.value.(time.Duration))
|
|
case "cap":
|
|
b.ChangeCapacity(int64(v.value.(int)))
|
|
test.capacity = b.Capacity()
|
|
case "time-add":
|
|
addToElapsed(v.value.(time.Duration))
|
|
case "till":
|
|
dt := b.TillEmpty()
|
|
if dt != v.value.(time.Duration) {
|
|
t.Fatalf("Test %d: Bad TillEmpty(). Expected %v, got %v", i, v.value, dt)
|
|
}
|
|
case "debug":
|
|
fmt.Println("elapsed:", getElapsed())
|
|
fmt.Println("tillEmpty:", b.TillEmpty())
|
|
fmt.Println("count:", b.Count())
|
|
}
|
|
c := b.Count()
|
|
if c != v.count {
|
|
t.Fatalf("Test %d: Bad count. Expected %d, got %d", i, v.count, c)
|
|
}
|
|
if c > test.capacity {
|
|
t.Fatalf("Test %d: Went over capacity?!", i)
|
|
}
|
|
if b.Remaining() != test.capacity-v.count {
|
|
t.Fatalf("Test %d: Expected remaining value %d, got %d",
|
|
i, test.capacity-v.count, b.Remaining())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLeakyBucket(t *testing.T) {
|
|
tests := []testSet{
|
|
oneAtaTime,
|
|
varied,
|
|
}
|
|
|
|
for i, test := range tests {
|
|
fmt.Println("Running testSet:", i)
|
|
runTest(t, &test)
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Override what now() function the leakybucket library uses.
|
|
// This greatly increases testability!
|
|
now = func() time.Time { return start.Add(getElapsed()) }
|
|
|
|
os.Exit(m.Run())
|
|
}
|