mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
Two packages were named for the same word and meant different things. network/ dialed peers, exchanged keys, throttled and tracked them; nets/ tracked which chains share a validator set and when they have all bootstrapped. One letter apart, and neither name said which was which. The peer layer is mesh — the word this estate already uses for it, in lux-rs/node's own mesh.rs. That frees network for the thing that is actually a network: a set of chains sharing a validator set. A network holds chains; a chain belongs to one network. github.com/luxfi/p2p stays what it is, the gossip library the VMs use. Three concerns, three names, none of them borrowed from another. Co-authored-by: Hanzo Dev <[email protected]>
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package throttling
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Test that the DialThrottler returned by NewDialThrottler works
|
|
func TestDialThrottler(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
startTime := time.Now()
|
|
// Allows 5 per second
|
|
throttler := NewDialThrottler(5)
|
|
// Use all 5
|
|
for i := 0; i < 5; i++ {
|
|
acquiredChan := make(chan error)
|
|
// Should return immediately because < 5 taken this second
|
|
go func() {
|
|
acquiredChan <- throttler.Acquire(context.Background())
|
|
}()
|
|
|
|
select {
|
|
case <-time.After(10 * time.Millisecond):
|
|
require.FailNow("should have acquired immediately")
|
|
case err := <-acquiredChan:
|
|
require.NoError(err)
|
|
}
|
|
close(acquiredChan)
|
|
}
|
|
|
|
acquiredChan := make(chan error)
|
|
go func() {
|
|
// Should block because 5 already taken within last second
|
|
acquiredChan <- throttler.Acquire(context.Background())
|
|
}()
|
|
|
|
select {
|
|
case <-time.After(25 * time.Millisecond):
|
|
case <-acquiredChan:
|
|
require.FailNow("should not have been able to acquire immediately")
|
|
}
|
|
|
|
// Wait until the 6th Acquire() has returned. The time at which
|
|
// that returns should be no more than 1s after the time at which
|
|
// the first Acquire() returned.
|
|
require.NoError(<-acquiredChan)
|
|
// Use 1.05 seconds instead of 1 second to give some "wiggle room"
|
|
// so test doesn't flake
|
|
require.LessOrEqual(time.Since(startTime), 1050*time.Millisecond)
|
|
}
|
|
|
|
// Test that Acquire honors its specification about its context being canceled
|
|
func TestDialThrottlerCancel(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
// Allows 5 per second
|
|
throttler := NewDialThrottler(5)
|
|
// Use all 5
|
|
for i := 0; i < 5; i++ {
|
|
acquiredChan := make(chan error)
|
|
// Should return immediately because < 5 taken this second
|
|
go func() {
|
|
acquiredChan <- throttler.Acquire(context.Background())
|
|
}()
|
|
|
|
select {
|
|
case <-time.After(10 * time.Millisecond):
|
|
require.FailNow("should have acquired immediately")
|
|
case err := <-acquiredChan:
|
|
require.NoError(err)
|
|
}
|
|
}
|
|
|
|
acquiredChan := make(chan error)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
// Cancel the 6th acquire
|
|
cancel()
|
|
|
|
go func() {
|
|
acquiredChan <- throttler.Acquire(ctx)
|
|
}()
|
|
|
|
select {
|
|
case err := <-acquiredChan:
|
|
require.ErrorIs(err, context.Canceled)
|
|
case <-time.After(10 * time.Millisecond):
|
|
require.FailNow("Acquire should have returned immediately upon context cancellation")
|
|
}
|
|
}
|
|
|
|
// Test that the Throttler return by NewNoThrottler never blocks on Acquire()
|
|
func TestNoDialThrottler(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
throttler := NewNoDialThrottler()
|
|
for i := 0; i < 250; i++ {
|
|
startTime := time.Now()
|
|
require.NoError(throttler.Acquire(context.Background())) // Should always immediately return
|
|
require.WithinDuration(time.Now(), startTime, 25*time.Millisecond)
|
|
}
|
|
}
|