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]>
103 lines
2.0 KiB
Go
103 lines
2.0 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package throttling
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
var _ net.Listener = (*MockListener)(nil)
|
|
|
|
type MockListener struct {
|
|
t *testing.T
|
|
OnAcceptF func() (net.Conn, error)
|
|
OnCloseF func() error
|
|
OnAddrF func() net.Addr
|
|
}
|
|
|
|
func (ml *MockListener) Accept() (net.Conn, error) {
|
|
if ml.OnAcceptF == nil {
|
|
require.FailNow(ml.t, "unexpectedly called Accept")
|
|
return nil, nil
|
|
}
|
|
return ml.OnAcceptF()
|
|
}
|
|
|
|
func (ml *MockListener) Close() error {
|
|
if ml.OnCloseF == nil {
|
|
require.FailNow(ml.t, "unexpectedly called Close")
|
|
return nil
|
|
}
|
|
return ml.OnCloseF()
|
|
}
|
|
|
|
func (ml *MockListener) Addr() net.Addr {
|
|
if ml.OnAddrF == nil {
|
|
require.FailNow(ml.t, "unexpectedly called Addr")
|
|
return nil
|
|
}
|
|
return ml.OnAddrF()
|
|
}
|
|
|
|
func TestInboundConnThrottlerClose(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
closed := false
|
|
l := &MockListener{
|
|
t: t,
|
|
OnCloseF: func() error {
|
|
closed = true
|
|
return nil
|
|
},
|
|
}
|
|
wrappedL := NewThrottledListener(l, 1)
|
|
require.NoError(wrappedL.Close())
|
|
require.True(closed)
|
|
|
|
select {
|
|
case <-wrappedL.(*throttledListener).ctx.Done():
|
|
default:
|
|
require.FailNow("should have closed context")
|
|
}
|
|
|
|
// Accept() should return an error because the context is cancelled
|
|
_, err := wrappedL.Accept()
|
|
require.ErrorIs(err, context.Canceled)
|
|
}
|
|
|
|
func TestInboundConnThrottlerAddr(t *testing.T) {
|
|
addrCalled := false
|
|
l := &MockListener{
|
|
t: t,
|
|
OnAddrF: func() net.Addr {
|
|
addrCalled = true
|
|
return nil
|
|
},
|
|
}
|
|
wrappedL := NewThrottledListener(l, 1)
|
|
_ = wrappedL.Addr()
|
|
require.True(t, addrCalled)
|
|
}
|
|
|
|
func TestInboundConnThrottlerAccept(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
acceptCalled := false
|
|
l := &MockListener{
|
|
t: t,
|
|
OnAcceptF: func() (net.Conn, error) {
|
|
acceptCalled = true
|
|
return nil, nil
|
|
},
|
|
}
|
|
wrappedL := NewThrottledListener(l, 1)
|
|
_, err := wrappedL.Accept()
|
|
require.NoError(err)
|
|
require.True(acceptCalled)
|
|
}
|