diff --git a/dial_sync_test.go b/dial_sync_test.go index db480bd5..9b23028d 100644 --- a/dial_sync_test.go +++ b/dial_sync_test.go @@ -241,7 +241,7 @@ func TestDialSelf(t *testing.T) { defer cancel() self := peer.ID("ABC") - s := NewSwarm(self, nil, nil) + s := NewSwarm(self, nil) defer s.Close() // this should fail diff --git a/swarm.go b/swarm.go index 391343cb..2b8f7056 100644 --- a/swarm.go +++ b/swarm.go @@ -50,6 +50,13 @@ func WithConnectionGater(gater connmgr.ConnectionGater) Option { } } +// WithMetrics sets a metrics reporter +func WithMetrics(reporter metrics.Reporter) Option { + return func(s *Swarm) { + s.bwc = reporter + } +} + // Swarm is a connection muxer, allowing connections to other peers to // be opened and closed, while still using the same Chan for all // communication. The Chan sends/receives Messages, which note the @@ -107,12 +114,11 @@ type Swarm struct { } // NewSwarm constructs a Swarm. -func NewSwarm(local peer.ID, peers peerstore.Peerstore, bwc metrics.Reporter, opts ...Option) *Swarm { +func NewSwarm(local peer.ID, peers peerstore.Peerstore, opts ...Option) *Swarm { ctx, cancel := context.WithCancel(context.Background()) s := &Swarm{ local: local, peers: peers, - bwc: bwc, ctx: ctx, ctxCancel: cancel, } diff --git a/testing/testing.go b/testing/testing.go index ff84252b..b0d4c218 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -113,11 +113,11 @@ func GenSwarm(t *testing.T, opts ...Option) *swarm.Swarm { ps.AddPrivKey(p.ID, p.PrivKey) t.Cleanup(func() { ps.Close() }) - var swarmOpts []swarm.Option + swarmOpts := []swarm.Option{swarm.WithMetrics(metrics.NewBandwidthCounter())} if cfg.connectionGater != nil { swarmOpts = append(swarmOpts, swarm.WithConnectionGater(cfg.connectionGater)) } - s := swarm.NewSwarm(p.ID, ps, metrics.NewBandwidthCounter(), swarmOpts...) + s := swarm.NewSwarm(p.ID, ps, swarmOpts...) upgrader := GenUpgrader(s) upgrader.ConnGater = cfg.connectionGater