Browse Source

test infra: make memory network test buffers configurable (#6275)

pull/6281/head
Sam Kleinman 4 years ago
committed by GitHub
parent
commit
2ceb816721
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 28 deletions
  1. +1
    -1
      blockchain/v0/reactor_test.go
  2. +1
    -1
      consensus/reactor_test.go
  3. +1
    -1
      evidence/reactor_test.go
  4. +1
    -1
      mempool/reactor_test.go
  5. +17
    -3
      p2p/p2ptest/network.go
  6. +5
    -5
      p2p/router_test.go
  7. +15
    -15
      p2p/transport_memory.go
  8. +1
    -1
      p2p/transport_memory_test.go

+ 1
- 1
blockchain/v0/reactor_test.go View File

@ -51,7 +51,7 @@ func setup(
rts := &reactorTestSuite{ rts := &reactorTestSuite{
logger: log.TestingLogger().With("module", "blockchain", "testCase", t.Name()), logger: log.TestingLogger().With("module", "blockchain", "testCase", t.Name()),
network: p2ptest.MakeNetwork(t, numNodes),
network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
nodes: make([]p2p.NodeID, 0, numNodes), nodes: make([]p2p.NodeID, 0, numNodes),
reactors: make(map[p2p.NodeID]*Reactor, numNodes), reactors: make(map[p2p.NodeID]*Reactor, numNodes),
app: make(map[p2p.NodeID]proxy.AppConns, numNodes), app: make(map[p2p.NodeID]proxy.AppConns, numNodes),


+ 1
- 1
consensus/reactor_test.go View File

@ -50,7 +50,7 @@ func setup(t *testing.T, numNodes int, states []*State, size int) *reactorTestSu
t.Helper() t.Helper()
rts := &reactorTestSuite{ rts := &reactorTestSuite{
network: p2ptest.MakeNetwork(t, numNodes),
network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
states: make(map[p2p.NodeID]*State), states: make(map[p2p.NodeID]*State),
reactors: make(map[p2p.NodeID]*Reactor, numNodes), reactors: make(map[p2p.NodeID]*Reactor, numNodes),
subs: make(map[p2p.NodeID]types.Subscription, numNodes), subs: make(map[p2p.NodeID]types.Subscription, numNodes),


+ 1
- 1
evidence/reactor_test.go View File

@ -55,7 +55,7 @@ func setup(t *testing.T, stateStores []sm.Store, chBuf uint) *reactorTestSuite {
rts := &reactorTestSuite{ rts := &reactorTestSuite{
numStateStores: numStateStores, numStateStores: numStateStores,
logger: log.TestingLogger().With("testCase", t.Name()), logger: log.TestingLogger().With("testCase", t.Name()),
network: p2ptest.MakeNetwork(t, numStateStores),
network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numStateStores}),
reactors: make(map[p2p.NodeID]*evidence.Reactor, numStateStores), reactors: make(map[p2p.NodeID]*evidence.Reactor, numStateStores),
pools: make(map[p2p.NodeID]*evidence.Pool, numStateStores), pools: make(map[p2p.NodeID]*evidence.Pool, numStateStores),
peerUpdates: make(map[p2p.NodeID]*p2p.PeerUpdates, numStateStores), peerUpdates: make(map[p2p.NodeID]*p2p.PeerUpdates, numStateStores),


+ 1
- 1
mempool/reactor_test.go View File

@ -39,7 +39,7 @@ func setup(t *testing.T, cfg *cfg.MempoolConfig, numNodes int, chBuf uint) *reac
rts := &reactorTestSuite{ rts := &reactorTestSuite{
logger: log.TestingLogger().With("testCase", t.Name()), logger: log.TestingLogger().With("testCase", t.Name()),
network: p2ptest.MakeNetwork(t, numNodes),
network: p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: numNodes}),
reactors: make(map[p2p.NodeID]*Reactor, numNodes), reactors: make(map[p2p.NodeID]*Reactor, numNodes),
mempoolChnnels: make(map[p2p.NodeID]*p2p.Channel, numNodes), mempoolChnnels: make(map[p2p.NodeID]*p2p.Channel, numNodes),
mempools: make(map[p2p.NodeID]*CListMempool, numNodes), mempools: make(map[p2p.NodeID]*CListMempool, numNodes),


+ 17
- 3
p2p/p2ptest/network.go View File

@ -25,17 +25,31 @@ type Network struct {
memoryNetwork *p2p.MemoryNetwork memoryNetwork *p2p.MemoryNetwork
} }
// NetworkOptions is an argument structure to parameterize the
// MakeNetwork function.
type NetworkOptions struct {
NumNodes int
BufferSize int
}
func (opts *NetworkOptions) setDefaults() {
if opts.BufferSize == 0 {
opts.BufferSize = 1
}
}
// MakeNetwork creates a test network with the given number of nodes and // MakeNetwork creates a test network with the given number of nodes and
// connects them to each other. // connects them to each other.
func MakeNetwork(t *testing.T, nodes int) *Network {
func MakeNetwork(t *testing.T, opts NetworkOptions) *Network {
opts.setDefaults()
logger := log.TestingLogger() logger := log.TestingLogger()
network := &Network{ network := &Network{
Nodes: map[p2p.NodeID]*Node{}, Nodes: map[p2p.NodeID]*Node{},
logger: logger, logger: logger,
memoryNetwork: p2p.NewMemoryNetwork(logger),
memoryNetwork: p2p.NewMemoryNetwork(logger, opts.BufferSize),
} }
for i := 0; i < nodes; i++ {
for i := 0; i < opts.NumNodes; i++ {
node := network.MakeNode(t) node := network.MakeNode(t)
network.Nodes[node.NodeID] = node network.Nodes[node.NodeID] = node
} }


+ 5
- 5
p2p/router_test.go View File

@ -44,7 +44,7 @@ func TestRouter_Network(t *testing.T) {
t.Cleanup(leaktest.Check(t)) t.Cleanup(leaktest.Check(t))
// Create a test network and open a channel where all peers run echoReactor. // Create a test network and open a channel where all peers run echoReactor.
network := p2ptest.MakeNetwork(t, 8)
network := p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: 8})
network.Start(t) network.Start(t)
local := network.RandomNode() local := network.RandomNode()
@ -143,7 +143,7 @@ func TestRouter_Channel_SendReceive(t *testing.T) {
t.Cleanup(leaktest.Check(t)) t.Cleanup(leaktest.Check(t))
// Create a test network and open a channel on all nodes. // Create a test network and open a channel on all nodes.
network := p2ptest.MakeNetwork(t, 3)
network := p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: 3})
network.Start(t) network.Start(t)
ids := network.NodeIDs() ids := network.NodeIDs()
@ -201,7 +201,7 @@ func TestRouter_Channel_Broadcast(t *testing.T) {
t.Cleanup(leaktest.Check(t)) t.Cleanup(leaktest.Check(t))
// Create a test network and open a channel on all nodes. // Create a test network and open a channel on all nodes.
network := p2ptest.MakeNetwork(t, 4)
network := p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: 4})
network.Start(t) network.Start(t)
ids := network.NodeIDs() ids := network.NodeIDs()
@ -228,7 +228,7 @@ func TestRouter_Channel_Wrapper(t *testing.T) {
t.Cleanup(leaktest.Check(t)) t.Cleanup(leaktest.Check(t))
// Create a test network and open a channel on all nodes. // Create a test network and open a channel on all nodes.
network := p2ptest.MakeNetwork(t, 2)
network := p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: 2})
network.Start(t) network.Start(t)
ids := network.NodeIDs() ids := network.NodeIDs()
@ -286,7 +286,7 @@ func TestRouter_Channel_Error(t *testing.T) {
t.Cleanup(leaktest.Check(t)) t.Cleanup(leaktest.Check(t))
// Create a test network and open a channel on all nodes. // Create a test network and open a channel on all nodes.
network := p2ptest.MakeNetwork(t, 3)
network := p2ptest.MakeNetwork(t, p2ptest.NetworkOptions{NumNodes: 3})
network.Start(t) network.Start(t)
ids := network.NodeIDs() ids := network.NodeIDs()


+ 15
- 15
p2p/transport_memory.go View File

@ -15,9 +15,6 @@ import (
const ( const (
MemoryProtocol Protocol = "memory" MemoryProtocol Protocol = "memory"
// bufferSize is the channel buffer size of MemoryConnection.
bufferSize = 1
) )
// MemoryNetwork is an in-memory "network" that uses buffered Go channels to // MemoryNetwork is an in-memory "network" that uses buffered Go channels to
@ -30,11 +27,13 @@ type MemoryNetwork struct {
mtx sync.RWMutex mtx sync.RWMutex
transports map[NodeID]*MemoryTransport transports map[NodeID]*MemoryTransport
bufferSize int
} }
// NewMemoryNetwork creates a new in-memory network. // NewMemoryNetwork creates a new in-memory network.
func NewMemoryNetwork(logger log.Logger) *MemoryNetwork {
func NewMemoryNetwork(logger log.Logger, bufferSize int) *MemoryNetwork {
return &MemoryNetwork{ return &MemoryNetwork{
bufferSize: bufferSize,
logger: logger, logger: logger,
transports: map[NodeID]*MemoryTransport{}, transports: map[NodeID]*MemoryTransport{},
} }
@ -89,9 +88,10 @@ func (n *MemoryNetwork) Size() int {
// New transports are allocated with MemoryNetwork.CreateTransport(). To contact // New transports are allocated with MemoryNetwork.CreateTransport(). To contact
// a different endpoint, both transports must be in the same MemoryNetwork. // a different endpoint, both transports must be in the same MemoryNetwork.
type MemoryTransport struct { type MemoryTransport struct {
logger log.Logger
network *MemoryNetwork
nodeID NodeID
logger log.Logger
network *MemoryNetwork
nodeID NodeID
bufferSize int
acceptCh chan *MemoryConnection acceptCh chan *MemoryConnection
closeCh chan struct{} closeCh chan struct{}
@ -102,12 +102,12 @@ type MemoryTransport struct {
// MemoryNetwork, use MemoryNetwork.CreateTransport() instead. // MemoryNetwork, use MemoryNetwork.CreateTransport() instead.
func newMemoryTransport(network *MemoryNetwork, nodeID NodeID) *MemoryTransport { func newMemoryTransport(network *MemoryNetwork, nodeID NodeID) *MemoryTransport {
return &MemoryTransport{ return &MemoryTransport{
logger: network.logger.With("local", nodeID),
network: network,
nodeID: nodeID,
acceptCh: make(chan *MemoryConnection),
closeCh: make(chan struct{}),
logger: network.logger.With("local", nodeID),
network: network,
nodeID: nodeID,
bufferSize: network.bufferSize,
acceptCh: make(chan *MemoryConnection),
closeCh: make(chan struct{}),
} }
} }
@ -164,8 +164,8 @@ func (t *MemoryTransport) Dial(ctx context.Context, endpoint Endpoint) (Connecti
return nil, fmt.Errorf("unknown peer %q", nodeID) return nil, fmt.Errorf("unknown peer %q", nodeID)
} }
inCh := make(chan memoryMessage, bufferSize)
outCh := make(chan memoryMessage, bufferSize)
inCh := make(chan memoryMessage, t.bufferSize)
outCh := make(chan memoryMessage, t.bufferSize)
closer := tmsync.NewCloser() closer := tmsync.NewCloser()
outConn := newMemoryConnection(t.logger, t.nodeID, peer.nodeID, inCh, outCh, closer) outConn := newMemoryConnection(t.logger, t.nodeID, peer.nodeID, inCh, outCh, closer)


+ 1
- 1
p2p/transport_memory_test.go View File

@ -17,7 +17,7 @@ func init() {
testTransports["memory"] = func(t *testing.T) p2p.Transport { testTransports["memory"] = func(t *testing.T) p2p.Transport {
if network == nil { if network == nil {
network = p2p.NewMemoryNetwork(log.TestingLogger())
network = p2p.NewMemoryNetwork(log.TestingLogger(), 1)
} }
i := byte(network.Size()) i := byte(network.Size())
nodeID, err := p2p.NewNodeID(hex.EncodeToString(bytes.Repeat([]byte{i<<4 + i}, 20))) nodeID, err := p2p.NewNodeID(hex.EncodeToString(bytes.Repeat([]byte{i<<4 + i}, 20)))


Loading…
Cancel
Save