peer manager: Disable in tests

This commit is contained in:
Łukasz Magiera 2019-10-23 13:02:00 +02:00
parent fc7c7ddd97
commit 1d1f468c98
5 changed files with 62 additions and 33 deletions

View File

@ -211,6 +211,7 @@ func (tu *syncTestUtil) addSourceNode(gen int) {
node.Online(),
node.Repo(sourceRepo),
node.MockHost(tu.mn),
node.Test(),
node.Override(new(modules.Genesis), modules.LoadGenesis(genesis)),
)
@ -242,6 +243,7 @@ func (tu *syncTestUtil) addClientNode() int {
node.Online(),
node.Repo(repo.NewMemory(nil)),
node.MockHost(tu.mn),
node.Test(),
node.Override(new(modules.Genesis), modules.LoadGenesis(tu.genesis)),
)

View File

@ -3,7 +3,6 @@ package node
import (
"context"
"errors"
"reflect"
"time"
blockstore "github.com/ipfs/go-ipfs-blockstore"
@ -124,26 +123,6 @@ type Settings struct {
Config bool // Config option applied
}
// Override option changes constructor for a given type
func Override(typ, constructor interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = fx.Invoke(constructor)
return nil
}
if c, ok := typ.(special); ok {
s.modules[c] = fx.Provide(constructor)
return nil
}
ctor := as(constructor, typ)
rt := reflect.TypeOf(typ).Elem()
s.modules[rt] = fx.Provide(ctor)
return nil
}
}
var defConf = config.Default()
func defaults() []Option {
@ -399,15 +378,9 @@ func New(ctx context.Context, opts ...Option) (StopFunc, error) {
// In-memory / testing
func randomIdentity() Option {
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512)
if err != nil {
return Error(err)
}
func Test() Option {
return Options(
Override(new(ci.PrivKey), sk),
Override(new(ci.PubKey), pk),
Override(new(peer.ID), peer.IDFromPublicKey),
Unset(RunPeerMgrKey),
Unset(new(*peermgr.PeerMgr)),
)
}

View File

@ -3,6 +3,7 @@ package hello
import (
"context"
"fmt"
"go.uber.org/fx"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
@ -40,13 +41,23 @@ type Service struct {
pmgr *peermgr.PeerMgr
}
func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pmgr *peermgr.PeerMgr) *Service {
type MaybePeerMgr struct {
fx.In
Mgr *peermgr.PeerMgr `optional:"true"`
}
func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pmgr MaybePeerMgr) *Service {
if pmgr.Mgr == nil {
log.Warn("running without peer manager")
}
return &Service{
newStream: h.NewStream,
cs: cs,
syncer: syncer,
pmgr: pmgr,
pmgr: pmgr.Mgr,
}
}
@ -77,8 +88,10 @@ func (hs *Service) HandleStream(s inet.Stream) {
log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer())
hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts)
if hs.pmgr != nil {
hs.pmgr.AddFilecoinPeer(s.Conn().RemotePeer())
}
}
func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
s, err := hs.newStream(ctx, pid, ProtocolID)

View File

@ -91,6 +91,7 @@ func testStorageNode(ctx context.Context, t *testing.T, waddr address.Address, a
node.StorageMiner(&minerapi),
node.Online(),
node.Repo(r),
node.Test(),
node.Override(new(*sectorbuilder.SectorBuilderConfig), modules.SectorBuilderConfig(secbpath)),
node.Override(new(api.FullNode), tnd),
@ -133,6 +134,7 @@ func builder(t *testing.T, nFull int, storage []int) ([]test.TestNode, []test.Te
node.Online(),
node.Repo(repo.NewMemory(nil)),
node.MockHost(mn),
node.Test(),
node.Override(new(*miner.Miner), miner.NewTestMiner(mineBlock)),

View File

@ -1,6 +1,7 @@
package node
import (
"go.uber.org/fx"
"reflect"
)
@ -38,6 +39,44 @@ func ApplyIf(check func(s *Settings) bool, opts ...Option) Option {
}
}
// Override option changes constructor for a given type
func Override(typ, constructor interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = fx.Invoke(constructor)
return nil
}
if c, ok := typ.(special); ok {
s.modules[c] = fx.Provide(constructor)
return nil
}
ctor := as(constructor, typ)
rt := reflect.TypeOf(typ).Elem()
s.modules[rt] = fx.Provide(ctor)
return nil
}
}
func Unset(typ interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = nil
return nil
}
if c, ok := typ.(special); ok {
delete(s.modules, c)
return nil
}
rt := reflect.TypeOf(typ).Elem()
delete(s.modules, rt)
return nil
}
}
// from go-ipfs
// as casts input constructor to a given interface (if a value is given, it
// wraps it into a constructor).