From 40b99edbb72bb3a2679e68706403a580d52f3a1a Mon Sep 17 00:00:00 2001 From: Godefroy Ponsinet Date: Mon, 4 Mar 2019 13:54:38 +0100 Subject: [PATCH] fix(test): e2e_test and app mock context Signed-off-by: Godefroy Ponsinet --- core/api/client/jsonclient/logger.gen.go | 1 - core/api/client/logger.gen.go | 1 - core/network/config/config.go | 3 + core/network/driver.go | 14 +-- core/network/host/host.go | 2 +- core/network/host/notifee.go | 17 --- core/network/host/stream_wrapper.go | 10 +- core/network/options.go | 1 + core/network/protocol/ble/transport.go | 1 + core/network/protocol/mdns/discovery.go | 37 ++++-- core/node/network.go | 3 +- core/test/app_mock.go | 18 ++- core/test/e2e_test.go | 142 ++++++++++++----------- core/test/mainloop_test.go | 2 +- core/test/node_test.go | 5 +- core/test/scenario_test.go | 1 + 16 files changed, 133 insertions(+), 125 deletions(-) delete mode 100644 core/network/host/notifee.go diff --git a/core/api/client/jsonclient/logger.gen.go b/core/api/client/jsonclient/logger.gen.go index 5a498381b8..fa5cd7d5a0 100644 --- a/core/api/client/jsonclient/logger.gen.go +++ b/core/api/client/jsonclient/logger.gen.go @@ -1,5 +1,4 @@ // Code generated by berty.tech/core/.scripts/generate-logger.sh - package jsonclient import "go.uber.org/zap" diff --git a/core/api/client/logger.gen.go b/core/api/client/logger.gen.go index 57da3d740d..4ad92854b5 100644 --- a/core/api/client/logger.gen.go +++ b/core/api/client/logger.gen.go @@ -1,5 +1,4 @@ // Code generated by berty.tech/core/.scripts/generate-logger.sh - package client import "go.uber.org/zap" diff --git a/core/network/config/config.go b/core/network/config/config.go index 1993d3270c..9ce2150fbc 100644 --- a/core/network/config/config.go +++ b/core/network/config/config.go @@ -83,6 +83,9 @@ func (cfg *Config) NewNode(ctx context.Context) (*host.BertyHost, error) { // override conn manager cfg.Config.ConnManager = host.NewBertyConnMgr(ctx, 10, 20, time.Duration(60*1000)) + // override ping service + cfg.Config.DisablePing = true + h, err := cfg.Config.NewNode(ctx) if err != nil { return nil, err diff --git a/core/network/driver.go b/core/network/driver.go index 97c4c373d8..32f4a9682e 100644 --- a/core/network/driver.go +++ b/core/network/driver.go @@ -74,9 +74,9 @@ func (net *Network) ID(ctx context.Context) *metric.Peer { tracer := tracing.EnterFunc(ctx) defer tracer.Finish() // ctx = tracer.Context() - addrs := make([]string, len(net.host.Addrs())) - for i, addr := range net.host.Addrs() { - addrs[i] = addr.String() + addrs := []string{} + for _, addr := range net.host.Addrs() { + addrs = append(addrs, addr.String()) } return &metric.Peer{ @@ -336,9 +336,9 @@ func (net *Network) FindProvidersAndWait(ctx context.Context, id string, cache b return nil, err } - ctx, cancel := context.WithCancel(ctx) + ctx, cancel := context.WithTimeout(ctx, time.Second*3) + defer cancel() - t := time.Now() piChan := net.host.Routing.FindProvidersAsync(ctx, c, 10) piSlice := []pstore.PeerInfo{} @@ -348,11 +348,7 @@ func (net *Network) FindProvidersAndWait(ctx context.Context, id string, cache b if pi.ID != "" { piSlice = append(piSlice, pi) } - if time.Now().Sub(t) >= time.Second*3 && len(piSlice) >= 1 { - cancel() - } case <-ctx.Done(): - cancel() if len(piSlice) == 0 { return nil, errors.New("no providers found") } diff --git a/core/network/host/host.go b/core/network/host/host.go index 4ffb5a4e7a..05d5c98b7a 100644 --- a/core/network/host/host.go +++ b/core/network/host/host.go @@ -173,7 +173,7 @@ func (bh *BertyHost) newStream(ctx context.Context, p peer.ID, pid protocol.ID) s.SetProtocol(pid) lzcon := msmux.NewMSSelect(s, string(pid)) - return &streamWrapper{ + return &StreamWrapper{ Stream: s, rw: lzcon, }, nil diff --git a/core/network/host/notifee.go b/core/network/host/notifee.go deleted file mode 100644 index 7af91c7a27..0000000000 --- a/core/network/host/notifee.go +++ /dev/null @@ -1,17 +0,0 @@ -package host - -import ( - inet "github.com/libp2p/go-libp2p-net" - ma "github.com/multiformats/go-multiaddr" -) - -func (bh *BertyHost) Listen(net inet.Network, a ma.Multiaddr) {} -func (bh *BertyHost) ListenClose(net inet.Network, a ma.Multiaddr) {} -func (bh *BertyHost) OpenedStream(net inet.Network, s inet.Stream) {} -func (bh *BertyHost) ClosedStream(net inet.Network, s inet.Stream) {} - -func (bh *BertyHost) Connected(s inet.Network, c inet.Conn) { - -} - -func (bh *BertyHost) Disconnected(s inet.Network, c inet.Conn) {} diff --git a/core/network/host/stream_wrapper.go b/core/network/host/stream_wrapper.go index 240df64fea..086c7c2538 100644 --- a/core/network/host/stream_wrapper.go +++ b/core/network/host/stream_wrapper.go @@ -8,25 +8,25 @@ import ( msmux "github.com/multiformats/go-multistream" ) -type streamWrapper struct { +type StreamWrapper struct { inet.Stream rw io.ReadWriter } -func NewStreamWrapper(s inet.Stream, pid protocol.ID) inet.Stream { +func NewStreamWrapper(s inet.Stream, pid protocol.ID) *StreamWrapper { s.SetProtocol(pid) lzcon := msmux.NewMSSelect(s, string(pid)) - return &streamWrapper{ + return &StreamWrapper{ Stream: s, rw: lzcon, } } -func (s *streamWrapper) Read(b []byte) (int, error) { +func (s *StreamWrapper) Read(b []byte) (int, error) { return s.rw.Read(b) } -func (s *streamWrapper) Write(b []byte) (int, error) { +func (s *StreamWrapper) Write(b []byte) (int, error) { return s.rw.Write(b) } diff --git a/core/network/options.go b/core/network/options.go index bc6f514fde..fddd5fa072 100644 --- a/core/network/options.go +++ b/core/network/options.go @@ -38,6 +38,7 @@ func WithDefaultOptions() config.Option { libp2p.EnableAutoRelay(), ), EnableDefaultBootstrap(), + EnablePing(), EnableMDNS(), PrivateNetwork(config.DefaultSwarmKey), EnableDHT(), diff --git a/core/network/protocol/ble/transport.go b/core/network/protocol/ble/transport.go index 1fffc64046..028a6ac517 100644 --- a/core/network/protocol/ble/transport.go +++ b/core/network/protocol/ble/transport.go @@ -64,6 +64,7 @@ func AddToPeerStore(peerID string, rAddr string) { // created. It represents an entire tcp stack (though it might not necessarily be) func NewTransport(h host.Host) (*Transport, error) { // use deterministic id based on host peerID + logger().Debug("BLE: " + h.ID().String()) id := uuid.NewV5(uuid.UUID{}, h.ID().String()) srcMA, err := ma.NewMultiaddr(fmt.Sprintf("/ble/%s", id.String())) ret := &Transport{ diff --git a/core/network/protocol/mdns/discovery.go b/core/network/protocol/mdns/discovery.go index 9f78b42dfb..dc2f6af235 100644 --- a/core/network/protocol/mdns/discovery.go +++ b/core/network/protocol/mdns/discovery.go @@ -2,6 +2,7 @@ package mdns import ( "context" + "sync" "time" "berty.tech/core/pkg/tracing" @@ -17,6 +18,7 @@ type Discovery struct { host host.Host services map[string]service.Service notifees map[string]*notifee + mutex sync.Mutex } func NewDiscovery(ctx context.Context, host host.Host) (discovery.Discovery, error) { @@ -31,7 +33,7 @@ func (d *Discovery) Advertise(ctx context.Context, ns string, opts ...discovery. tracer := tracing.EnterFunc(ctx) defer tracer.Finish() - if err := d.wakeService(ctx, ns); err != nil { + if err := d.wakeService(ctx, ns, false); err != nil { return 0, err } time.Sleep(10 * time.Second) @@ -42,22 +44,17 @@ func (d *Discovery) FindPeers(ctx context.Context, ns string, opts ...discovery. tracer := tracing.EnterFunc(ctx) defer tracer.Finish() - if err := d.wakeService(ctx, ns); err != nil { + if err := d.wakeService(ctx, ns, true); err != nil { return nil, err } - _, ok := d.notifees[ns] - if !ok { - d.notifees[ns] = ¬ifee{ - piChan: make(chan pstore.PeerInfo, 1), - } - d.services[ns].RegisterNotifee(d.notifees[ns]) - } + return d.notifees[ns].piChan, nil } -func (d *Discovery) wakeService(ctx context.Context, ns string) error { +func (d *Discovery) wakeService(ctx context.Context, ns string, regiterNotifee bool) error { var err error + d.mutex.Lock() _, ok := d.services[ns] if ok { return nil @@ -68,15 +65,31 @@ func (d *Discovery) wakeService(ctx context.Context, ns string) error { return err } + _, ok = d.notifees[ns] + if !ok { + d.notifees[ns] = ¬ifee{ + piChan: make(chan pstore.PeerInfo, 1), + } + } + + if regiterNotifee && d.notifees[ns].registered == false { + d.services[ns].RegisterNotifee(d.notifees[ns]) + d.notifees[ns].registered = true + } + d.mutex.Unlock() + return nil } var _ service.Notifee = (*notifee)(nil) type notifee struct { - piChan chan pstore.PeerInfo + piChan chan pstore.PeerInfo + registered bool } func (n *notifee) HandlePeerFound(pi pstore.PeerInfo) { - n.piChan <- pi + if pi.ID != "" { + n.piChan <- pi + } } diff --git a/core/node/network.go b/core/node/network.go index 2ee7f18b1b..35880209eb 100644 --- a/core/node/network.go +++ b/core/node/network.go @@ -40,10 +40,11 @@ func (n *Node) UseNetworkDriver(ctx context.Context, driver network.Driver) erro // configure network n.networkDriver.OnEnvelopeHandler(n.HandleEnvelope) if err := n.networkDriver.Join(ctx, n.UserID()); err != nil { - logger().Warn("failed to join user channel", + logger().Error("failed to join user channel", zap.String("id", n.UserID()), zap.Error(err), ) + return err } // FIXME: subscribe to every owned device IDs diff --git a/core/test/app_mock.go b/core/test/app_mock.go index 44c860e020..91e32c80e9 100644 --- a/core/test/app_mock.go +++ b/core/test/app_mock.go @@ -63,7 +63,7 @@ func WithUnencryptedDb() AppMockOption { } } -func NewAppMock(device *entity.Device, networkDriver network.Driver, options ...AppMockOption) (*AppMock, error) { +func NewAppMock(ctx context.Context, device *entity.Device, networkDriver network.Driver, options ...AppMockOption) (*AppMock, error) { tmpFile, err := ioutil.TempFile("", "sqlite") if err != nil { return nil, err @@ -76,8 +76,10 @@ func NewAppMock(device *entity.Device, networkDriver network.Driver, options ... crypto: &keypair.InsecureCrypto{}, options: options, } + a.ctx, a.cancel = context.WithCancel(ctx) if err := a.Open(); err != nil { + a.cancel() return nil, err } @@ -121,8 +123,6 @@ func (a *AppMock) Open() error { } } - a.ctx, a.cancel = context.WithCancel(context.Background()) - if a.node, err = node.New( a.ctx, node.WithSQL(a.db), @@ -158,7 +158,7 @@ func (a *AppMock) Open() error { return nil } -func (a *AppMock) InitEventStream() error { +func (a *AppMock) InitEventStream(ctx context.Context) error { a.eventStream = make(chan *entity.Event, 100) stream, err := a.client.Node().EventStream(a.ctx, &nodeapi.EventStreamInput{}) if err != nil { @@ -172,10 +172,16 @@ func (a *AppMock) InitEventStream() error { return } if err != nil { - logger().Warn("failed to receive stream data", zap.Error(err)) + logger().Warn("failed to receive stream data", zap.String("app", fmt.Sprintf("%+v", a)), zap.Error(err)) + return + } + select { + default: + a.eventStream <- data + case <-ctx.Done(): + logger().Debug("event stream context done") return } - a.eventStream <- data } }() return nil diff --git a/core/test/e2e_test.go b/core/test/e2e_test.go index 025bbd5e56..b86e4a91da 100644 --- a/core/test/e2e_test.go +++ b/core/test/e2e_test.go @@ -24,7 +24,7 @@ func TestWithEnqueuer(t *testing.T) { var ( alice, bob, eve *AppMock err error - internalCtx = context.Background() + ctx = context.Background() ) defer func() { @@ -48,17 +48,17 @@ func TestWithEnqueuer(t *testing.T) { Convey("Initialize nodes", FailureHalts, func() { shouldIContinue(t) - alice, err = NewAppMock(&entity.Device{Name: "Alice's iPhone"}, mock.NewEnqueuer(context.Background())) + alice, err = NewAppMock(ctx, &entity.Device{Name: "Alice's iPhone"}, mock.NewEnqueuer(context.Background())) So(err, ShouldBeNil) - So(alice.InitEventStream(), ShouldBeNil) + So(alice.InitEventStream(ctx), ShouldBeNil) - bob, err = NewAppMock(&entity.Device{Name: "iPhone de Bob"}, mock.NewEnqueuer(context.Background())) + bob, err = NewAppMock(ctx, &entity.Device{Name: "iPhone de Bob"}, mock.NewEnqueuer(context.Background())) So(err, ShouldBeNil) - So(bob.InitEventStream(), ShouldBeNil) + So(bob.InitEventStream(ctx), ShouldBeNil) - eve, err = NewAppMock(&entity.Device{Name: "Eve"}, mock.NewEnqueuer(context.Background())) + eve, err = NewAppMock(ctx, &entity.Device{Name: "Eve"}, mock.NewEnqueuer(context.Background())) So(err, ShouldBeNil) - So(eve.InitEventStream(), ShouldBeNil) + So(eve.InitEventStream(ctx), ShouldBeNil) everythingWentFine() }) @@ -100,7 +100,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Alice should only know itself", FailureHalts, func() { shouldIContinue(t) - contacts, err := alice.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err := alice.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) // 'myself' is the only known contact @@ -120,7 +120,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Bob should only know itself", FailureHalts, func() { shouldIContinue(t) - contacts, err := bob.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err := bob.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) // 'myself' is the only known contact @@ -140,7 +140,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Eve should only know itself", FailureHalts, func() { shouldIContinue(t) - contacts, err := eve.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err := eve.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) // 'myself' is the only known contact @@ -163,7 +163,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Alice calls node.ContactRequest", FailureHalts, func() { shouldIContinue(t) - res, err := alice.client.Node().ContactRequest(internalCtx, &node.ContactRequestInput{ + res, err := alice.client.Node().ContactRequest(ctx, &node.ContactRequestInput{ ContactID: bob.node.UserID(), ContactOverrideDisplayName: "Bob from school", IntroText: "hello, I want to chat!", @@ -189,7 +189,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Alice has en entry in sql for Bob", FailureHalts, func() { shouldIContinue(t) - contacts, err := alice.client.ContactList(internalCtx, &node.ContactListInput{ + contacts, err := alice.client.ContactList(ctx, &node.ContactListInput{ Filter: &entity.Contact{Status: entity.Contact_IsRequested}, }) So(err, ShouldBeNil) @@ -207,7 +207,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Alice has a conversation with Bob", FailureHalts, func() { shouldIContinue(t) - conversations, err := alice.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err := alice.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) So(len(conversations[0].Members), ShouldEqual, 2) @@ -300,7 +300,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Bob has en entry in sql for Alice", FailureHalts, func() { shouldIContinue(t) - contacts, err := bob.client.ContactList(internalCtx, &node.ContactListInput{ + contacts, err := bob.client.ContactList(ctx, &node.ContactListInput{ Filter: &entity.Contact{DisplayName: "Alice"}, }) So(err, ShouldBeNil) @@ -318,7 +318,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Bob has a conversation with Alice", FailureHalts, func() { shouldIContinue(t) - conversations, err := alice.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err := alice.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) So(len(conversations[0].Members), ShouldEqual, 2) @@ -329,7 +329,7 @@ func TestWithEnqueuer(t *testing.T) { // Convey("Bob has a conversation with Alice", FailureHalts, func() { // shouldIContinue(T) - // _, err := bob.client.ConversationList(internalCtx, &node.ConversationListInput{}) + // _, err := bob.client.ConversationList(ctx, &node.ConversationListInput{}) // So(err, ShouldBeNil) // // So(len(conversations), ShouldEqual, 1) // // So(len(conversations[0].Members), ShouldEqual, 2) @@ -341,7 +341,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Bob calls node.ContactAcceptRequest", FailureHalts, func() { shouldIContinue(t) - res, err := bob.client.Node().ContactAcceptRequest(internalCtx, &node.ContactAcceptRequestInput{ + res, err := bob.client.Node().ContactAcceptRequest(ctx, &node.ContactAcceptRequestInput{ ContactID: alice.node.UserID(), }) So(err, ShouldBeNil) @@ -566,7 +566,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Alice has Bob as friend", FailureHalts, func() { shouldIContinue(t) - contacts, err := alice.client.ContactList(internalCtx, &node.ContactListInput{ + contacts, err := alice.client.ContactList(ctx, &node.ContactListInput{ Filter: &entity.Contact{Status: entity.Contact_IsFriend}, }) So(err, ShouldBeNil) @@ -586,7 +586,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Bob has Alice as friend", FailureHalts, func() { shouldIContinue(t) - contacts, err := bob.client.ContactList(internalCtx, &node.ContactListInput{ + contacts, err := bob.client.ContactList(ctx, &node.ContactListInput{ Filter: &entity.Contact{Status: entity.Contact_IsFriend}, }) So(err, ShouldBeNil) @@ -606,7 +606,7 @@ func TestWithEnqueuer(t *testing.T) { Convey("Eve has no friend", FailureHalts, func() { shouldIContinue(t) - contacts, err := eve.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err := eve.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) }) @@ -632,11 +632,11 @@ func TestWithEnqueuer(t *testing.T) { func TestAliasesFlow(t *testing.T) { var ( - alice, bob *AppMock - err error - internalCtx = context.Background() - res interface{} - envelope *entity.Envelope + alice, bob *AppMock + err error + ctx = context.Background() + res interface{} + envelope *entity.Envelope ) defer func() { @@ -656,14 +656,14 @@ func TestAliasesFlow(t *testing.T) { network := mock.NewSimple() aliceNetwork := network.Driver() - alice, err = NewAppMock(&entity.Device{Name: "Alice's iPhone"}, aliceNetwork, WithUnencryptedDb()) + alice, err = NewAppMock(ctx, &entity.Device{Name: "Alice's iPhone"}, aliceNetwork, WithUnencryptedDb()) So(err, ShouldBeNil) - So(alice.InitEventStream(), ShouldBeNil) + So(alice.InitEventStream(ctx), ShouldBeNil) bobNetwork := network.Driver() - bob, err = NewAppMock(&entity.Device{Name: "iPhone de Bob"}, bobNetwork, WithUnencryptedDb()) + bob, err = NewAppMock(ctx, &entity.Device{Name: "iPhone de Bob"}, bobNetwork, WithUnencryptedDb()) So(err, ShouldBeNil) - So(bob.InitEventStream(), ShouldBeNil) + So(bob.InitEventStream(ctx), ShouldBeNil) network.AddPeer(aliceNetwork) network.AddPeer(bobNetwork) @@ -673,16 +673,16 @@ func TestAliasesFlow(t *testing.T) { Convey("Alice adds Bob as a friend and init a conversation", FailureHalts, func() { - contacts, err := alice.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err := alice.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) // 'myself' is the only known contact - contacts, err = bob.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err = bob.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 1) // 'myself' is the only known contact shouldIContinue(t) - res, err = alice.client.Node().ContactRequest(internalCtx, &node.ContactRequestInput{ + res, err = alice.client.Node().ContactRequest(ctx, &node.ContactRequestInput{ ContactID: bob.node.UserID(), ContactOverrideDisplayName: "Bob from school", IntroText: "hello, I want to chat!", @@ -691,11 +691,11 @@ func TestAliasesFlow(t *testing.T) { So(err, ShouldBeNil) time.Sleep(timeBetweenSteps) - contacts, err = alice.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err = alice.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 2) // 'myself' is the only known contact - contacts, err = bob.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err = bob.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 2) // 'myself' is the only known contact @@ -703,7 +703,7 @@ func TestAliasesFlow(t *testing.T) { So(res, ShouldNotBeNil) time.Sleep(timeBetweenSteps) - res, err = bob.client.Node().ContactAcceptRequest(internalCtx, &node.ContactAcceptRequestInput{ + res, err = bob.client.Node().ContactAcceptRequest(ctx, &node.ContactAcceptRequestInput{ ContactID: alice.node.UserID(), }) @@ -711,15 +711,15 @@ func TestAliasesFlow(t *testing.T) { So(res, ShouldNotBeNil) time.Sleep(timeBetweenSteps) - contacts, err = alice.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err = alice.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 2) - contacts, err = bob.client.ContactList(internalCtx, &node.ContactListInput{}) + contacts, err = bob.client.ContactList(ctx, &node.ContactListInput{}) So(err, ShouldBeNil) So(len(contacts), ShouldEqual, 2) - _, err = bob.client.Node().ConversationCreate(internalCtx, &node.ConversationCreateInput{ + _, err = bob.client.Node().ConversationCreate(ctx, &node.ConversationCreateInput{ Title: "Alice & Bob", Topic: "hey!", Contacts: []*entity.Contact{ @@ -731,11 +731,11 @@ func TestAliasesFlow(t *testing.T) { time.Sleep(200 * time.Millisecond) - conversations, err := bob.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err := bob.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) - conversations, err = alice.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err = alice.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) @@ -762,7 +762,7 @@ func TestAliasesFlow(t *testing.T) { time.Sleep(timeBetweenSteps) - _, err = alice.client.Node().ConversationCreate(internalCtx, &node.ConversationCreateInput{ + _, err = alice.client.Node().ConversationCreate(ctx, &node.ConversationCreateInput{ Title: "Alice & Bob 2", Topic: "hey! oh!", Contacts: []*entity.Contact{ @@ -773,11 +773,11 @@ func TestAliasesFlow(t *testing.T) { So(err, ShouldBeNil) time.Sleep(timeBetweenSteps) - conversations, err := bob.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err := bob.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) - conversations, err = alice.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err = alice.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) @@ -793,7 +793,7 @@ func TestAliasesFlow(t *testing.T) { time.Sleep(timeBetweenSteps) - _, err = alice.client.Node().ConversationCreate(internalCtx, &node.ConversationCreateInput{ + _, err = alice.client.Node().ConversationCreate(ctx, &node.ConversationCreateInput{ Title: "Alice & Bob 3", Topic: "hey! oh! let's go", Contacts: []*entity.Contact{ @@ -804,11 +804,11 @@ func TestAliasesFlow(t *testing.T) { So(err, ShouldBeNil) time.Sleep(timeBetweenSteps) - conversations, err = bob.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err = bob.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) - conversations, err = alice.client.ConversationList(internalCtx, &node.ConversationListInput{}) + conversations, err = alice.client.ConversationList(ctx, &node.ConversationListInput{}) So(err, ShouldBeNil) So(len(conversations), ShouldEqual, 1) @@ -826,16 +826,16 @@ func TestAliasesFlow(t *testing.T) { } func setupP2PNetwork(ctx context.Context) (*p2pnet.Network, error) { - return p2pnet.New(ctx, p2pnet.WithDefaultOptions()) + return p2pnet.New(ctx, p2pnet.WithDefaultOptions(), p2pnet.DisableDefaultBootstrap()) } -func getBoostrap(n *p2pnet.Network) []string { +func getBootstrap(ctx context.Context, n *p2pnet.Network) []string { addrs := n.Addrs() bootstrap := make([]string, len(addrs)) for i, a := range addrs { if a.String() != "/p2p-circuit" { - bootstrap[i] = fmt.Sprintf("%s/ipfs/%s", a.String(), n.ID(context.Background()).ID) + bootstrap[i] = fmt.Sprintf("%s/ipfs/%s", a.String(), n.ID(ctx).ID) } } @@ -847,6 +847,8 @@ func TestWithSimpleNetwork(t *testing.T) { alice, bob, eve *AppMock err error ) + + ctx := context.Background() defer func() { if alice != nil { alice.Close() @@ -867,24 +869,24 @@ func TestWithSimpleNetwork(t *testing.T) { network := mock.NewSimple() aliceNetwork := network.Driver() - alice, err = NewAppMock(&entity.Device{Name: "Alice's iPhone"}, aliceNetwork) + alice, err = NewAppMock(ctx, &entity.Device{Name: "Alice's iPhone"}, aliceNetwork) So(err, ShouldBeNil) bobNetwork := network.Driver() - bob, err = NewAppMock(&entity.Device{Name: "iPhone de Bob"}, bobNetwork) + bob, err = NewAppMock(ctx, &entity.Device{Name: "iPhone de Bob"}, bobNetwork) So(err, ShouldBeNil) eveNetwork := network.Driver() - eve, err = NewAppMock(&entity.Device{Name: "Eve"}, eveNetwork) + eve, err = NewAppMock(ctx, &entity.Device{Name: "Eve"}, eveNetwork) So(err, ShouldBeNil) network.AddPeer(aliceNetwork) network.AddPeer(bobNetwork) network.AddPeer(eveNetwork) - So(alice.InitEventStream(), ShouldBeNil) - So(bob.InitEventStream(), ShouldBeNil) - So(eve.InitEventStream(), ShouldBeNil) + So(alice.InitEventStream(ctx), ShouldBeNil) + So(bob.InitEventStream(ctx), ShouldBeNil) + So(eve.InitEventStream(ctx), ShouldBeNil) everythingWentFine() }) @@ -901,36 +903,40 @@ func TestNodesWithP2PNetwork(t *testing.T) { everythingWentFine() - ctx := context.WithTimeout(context.Background(), time.Second*10) + ctx := context.Background() + ctx, _ = context.WithTimeout(ctx, time.Minute) Convey("End-to-end test (with p2p network)", t, FailureHalts, func() { - Convey("Initialize nodes", FailureHalts, func() { + Convey("setup networks", FailureHalts, func() { shouldIContinue(t) aliceNetwork, err = setupP2PNetwork(ctx) So(err, ShouldBeNil) - - bootstrap := getBoostrap(aliceNetwork) - bobNetwork, err = setupP2PNetwork(ctx) So(err, ShouldBeNil) eveNetwork, err = setupP2PNetwork(ctx) So(err, ShouldBeNil) - err = bobNetwork.Bootstrap(ctx, true, bootstrap...) + aliceBootstrap := getBootstrap(ctx, aliceNetwork) + bobBootstrap := getBootstrap(ctx, bobNetwork) + eveBootstrap := getBootstrap(ctx, eveNetwork) + + err = aliceNetwork.Bootstrap(ctx, true, append(bobBootstrap, eveBootstrap...)...) + So(err, ShouldBeNil) + err = bobNetwork.Bootstrap(ctx, true, append(aliceBootstrap, eveBootstrap...)...) So(err, ShouldBeNil) - err = eveNetwork.Bootstrap(ctx, true, bootstrap...) + err = eveNetwork.Bootstrap(ctx, true, append(aliceBootstrap, bobBootstrap...)...) So(err, ShouldBeNil) - bob, err = NewAppMock(&entity.Device{Name: "Bob"}, bobNetwork) + bob, err = NewAppMock(ctx, &entity.Device{Name: "Bob"}, bobNetwork) So(err, ShouldBeNil) - alice, err = NewAppMock(&entity.Device{Name: "Alice"}, aliceNetwork) + alice, err = NewAppMock(ctx, &entity.Device{Name: "Alice"}, aliceNetwork) So(err, ShouldBeNil) - eve, err = NewAppMock(&entity.Device{Name: "Eve"}, eveNetwork) + eve, err = NewAppMock(ctx, &entity.Device{Name: "Eve"}, eveNetwork) So(err, ShouldBeNil) - So(bob.InitEventStream(), ShouldBeNil) - So(alice.InitEventStream(), ShouldBeNil) - So(eve.InitEventStream(), ShouldBeNil) + So(bob.InitEventStream(ctx), ShouldBeNil) + So(alice.InitEventStream(ctx), ShouldBeNil) + So(eve.InitEventStream(ctx), ShouldBeNil) everythingWentFine() }) diff --git a/core/test/mainloop_test.go b/core/test/mainloop_test.go index 3311fdfc6d..c07e916d9c 100644 --- a/core/test/mainloop_test.go +++ b/core/test/mainloop_test.go @@ -13,7 +13,7 @@ import ( ) func setupNonAcknowledgedEventDestinations() (*AppMock, time.Time, time.Time, time.Time) { - n, err := NewAppMock(&entity.Device{Name: "Alice's iPhone"}, networkmock.NewEnqueuer(context.Background()), WithUnencryptedDb()) + n, err := NewAppMock(context.Background(), &entity.Device{Name: "Alice's iPhone"}, networkmock.NewEnqueuer(context.Background()), WithUnencryptedDb()) if err != nil { panic(err) diff --git a/core/test/node_test.go b/core/test/node_test.go index be929ff952..82836f4a49 100644 --- a/core/test/node_test.go +++ b/core/test/node_test.go @@ -41,10 +41,8 @@ func TestNodeHelpers(t *testing.T) { Convey("Testing Node", t, func() { Convey("Testing Node.EventStream", FailureContinues, func(c C) { t.Skip("see https://github.com/berty/berty/issues/252") - app, err := NewAppMock(&entity.Device{Name: "test phone"}, mock.NewEnqueuer(context.Background())) + app, err := NewAppMock(context.Background(), &entity.Device{Name: "test phone"}, mock.NewEnqueuer(context.Background())) So(err, ShouldBeNil) - defer app.Close() - So(app.node.EnqueueClientEvent(app.ctx, &entity.Event{}), ShouldBeNil) So(app.node.EnqueueClientEvent(app.ctx, &entity.Event{}), ShouldBeNil) @@ -140,6 +138,7 @@ func TestNodeHelpers(t *testing.T) { So(len(queue), ShouldEqual, 50) } + app.Close() // FIXME: send more messages than chan capacity // FIXME: remove subs (unsubscribe) }) diff --git a/core/test/scenario_test.go b/core/test/scenario_test.go index 1e12ec106d..5db411d32f 100644 --- a/core/test/scenario_test.go +++ b/core/test/scenario_test.go @@ -28,6 +28,7 @@ func scenario(t *testing.T, alice, bob, eve *AppMock) { }) Convey("Nodes should be empty when just initialized", FailureHalts, func() { + shouldIContinue(t) contacts, err := alice.client.ContactList(internalCtx, &node.ContactListInput{})