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]>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package throttling
|
|
|
|
import (
|
|
"context"
|
|
"github.com/luxfi/metric"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/log"
|
|
)
|
|
|
|
func TestBandwidthThrottler(t *testing.T) {
|
|
require := require.New(t)
|
|
// Assert initial state
|
|
config := BandwidthThrottlerConfig{
|
|
RefillRate: 8,
|
|
MaxBurstSize: 10,
|
|
}
|
|
throttlerIntf, err := newBandwidthThrottler(log.NoLog{}, metric.NewNoOp().Registry(), config)
|
|
require.NoError(err)
|
|
require.IsType(&bandwidthThrottlerImpl{}, throttlerIntf)
|
|
throttler := throttlerIntf.(*bandwidthThrottlerImpl)
|
|
require.NotNil(throttler.log)
|
|
require.NotNil(throttler.limiters)
|
|
require.Equal(config.RefillRate, throttler.RefillRate)
|
|
require.Equal(config.MaxBurstSize, throttler.MaxBurstSize)
|
|
require.Empty(throttler.limiters)
|
|
|
|
// Add a node
|
|
nodeID1 := ids.GenerateTestNodeID()
|
|
throttler.AddNode(nodeID1)
|
|
require.Len(throttler.limiters, 1)
|
|
|
|
// Remove the node
|
|
throttler.RemoveNode(nodeID1)
|
|
require.Empty(throttler.limiters)
|
|
|
|
// Add the node back
|
|
throttler.AddNode(nodeID1)
|
|
require.Len(throttler.limiters, 1)
|
|
|
|
// Should be able to acquire 8
|
|
throttler.Acquire(context.Background(), 8, nodeID1)
|
|
|
|
// Make several goroutines that acquire bytes.
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(int(config.MaxBurstSize) + 5)
|
|
for i := uint64(0); i < config.MaxBurstSize+5; i++ {
|
|
go func() {
|
|
throttler.Acquire(context.Background(), 1, nodeID1)
|
|
wg.Done()
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|