Test connecting two nodes

This commit is contained in:
Łukasz Magiera 2019-07-09 19:03:36 +02:00
parent 4ac8eba59e
commit 0a9ef94da5
7 changed files with 92 additions and 18 deletions

View File

@ -36,7 +36,7 @@ type API interface {
NetPeers(context.Context) ([]peer.AddrInfo, error) // TODO: check serialization
NetConnect(context.Context, peer.AddrInfo) error
NetAddrsListen(context.Context) (MultiaddrSlice, error)
NetAddrsListen(context.Context) (peer.AddrInfo, error)
// // ping
// Struct

View File

@ -14,7 +14,7 @@ type Struct struct {
NetPeers func(context.Context) ([]peer.AddrInfo, error)
NetConnect func(context.Context, peer.AddrInfo) error
NetAddrsListen func(context.Context) (MultiaddrSlice, error)
NetAddrsListen func(context.Context) (peer.AddrInfo, error)
}
}
@ -26,7 +26,7 @@ func (c *Struct) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return c.Internal.NetConnect(ctx, p)
}
func (c *Struct) NetAddrsListen(ctx context.Context) (MultiaddrSlice, error) {
func (c *Struct) NetAddrsListen(ctx context.Context) (peer.AddrInfo, error) {
return c.Internal.NetAddrsListen(ctx)
}

View File

@ -2,6 +2,7 @@ package test
import (
"context"
"strings"
"testing"
"github.com/filecoin-project/go-lotus/api"
@ -22,13 +23,16 @@ func TestApis(t *testing.T, b APIBuilder) {
}
t.Run("version", ts.testVersion)
t.Run("id", ts.testID)
t.Run("testConnectTwo", ts.testConnectTwo)
}
func (ts *testSuite) testVersion(t *testing.T) {
ctx := context.Background()
fc := ts.makeNodes(t, 1)[0]
api := ts.makeNodes(t, 1)[0]
v, err := fc.Version(ctx)
v, err := api.Version(ctx)
if err != nil {
t.Fatal(err)
}
@ -36,3 +40,62 @@ func (ts *testSuite) testVersion(t *testing.T) {
t.Error("Version didn't work properly")
}
}
func (ts *testSuite) testID(t *testing.T) {
ctx := context.Background()
api := ts.makeNodes(t, 1)[0]
id, err := api.ID(ctx)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(id.Pretty(), "Qm") {
t.Error("expected identity to be Qm..")
}
}
func (ts *testSuite) testConnectTwo(t *testing.T) {
ctx := context.Background()
apis := ts.makeNodes(t, 2)
p, err := apis[0].NetPeers(ctx)
if err != nil {
t.Fatal(err)
}
if len(p) != 0 {
t.Error("Node 0 has a peer")
}
p, err = apis[1].NetPeers(ctx)
if err != nil {
t.Fatal(err)
}
if len(p) != 0 {
t.Error("Node 1 has a peer")
}
addrs, err := apis[1].NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := apis[0].NetConnect(ctx, addrs); err != nil {
t.Fatal(err)
}
p, err = apis[0].NetPeers(ctx)
if err != nil {
t.Fatal(err)
}
if len(p) != 1 {
t.Error("Node 0 doesn't have 1 peer")
}
p, err = apis[1].NetPeers(ctx)
if err != nil {
t.Fatal(err)
}
if len(p) != 1 {
t.Error("Node 0 doesn't have 1 peer")
}
}

View File

@ -53,8 +53,8 @@ var netListen = &cli.Command{
return err
}
for _, peer := range addrs {
fmt.Println(peer)
for _, peer := range addrs.Addrs {
fmt.Printf("%s/p2p/%s\n", peer, addrs.ID)
}
return nil
},

View File

@ -45,8 +45,11 @@ func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return a.Host.Connect(ctx, p)
}
func (a *API) NetAddrsListen(context.Context) (api.MultiaddrSlice, error) {
return a.Host.Addrs(), nil
func (a *API) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
return peer.AddrInfo{
ID: a.Host.ID(),
Addrs: a.Host.Addrs(),
}, nil
}
var _ api.API = &API{}

View File

@ -108,7 +108,8 @@ func Override(typ, constructor interface{}) Option {
var defConf = config.Default()
var defaults = []Option{
func defaults() []Option {
return []Option{
Override(new(helpers.MetricsCtx), context.Background),
randomIdentity(),
@ -121,6 +122,7 @@ var defaults = []Option{
Override(new(*chain.ChainStore), chain.NewChainStore),
}
}
// Online sets up basic libp2p node
func Online() Option {
@ -202,7 +204,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
}
// apply module options in the right order
if err := Options(Options(defaults...), Options(opts...))(&settings); err != nil {
if err := Options(Options(defaults()...), Options(opts...))(&settings); err != nil {
return nil, err
}
@ -224,6 +226,8 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
fx.Options(settings.invokes...),
fx.Extract(resAPI),
fx.NopLogger,
)
// TODO: we probably should have a 'firewall' for Closing signal

View File

@ -30,6 +30,10 @@ func builder(t *testing.T, n int) []api.API {
}
}
if err := mn.LinkAll(); err != nil {
t.Fatal(err)
}
return out
}