From ab48d4731cb242583c8e2bf70b2401e9910f1d60 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Mon, 11 Jun 2018 12:20:51 +1000
Subject: [PATCH] Include rate limiting and stats in
 BenchmarkConnectionMainReadLoop

---
 client.go          |  4 ++--
 connection_test.go | 13 +++++++------
 misc.go            |  3 +++
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/client.go b/client.go
index 8b8f9b4f..5f28b12d 100644
--- a/client.go
+++ b/client.go
@@ -192,12 +192,12 @@ func NewClient(cfg *Config) (cl *Client, err error) {
 		cl.Close()
 	}()
 	if cfg.UploadRateLimiter == nil {
-		cl.uploadLimit = rate.NewLimiter(rate.Inf, 0)
+		cl.uploadLimit = unlimited
 	} else {
 		cl.uploadLimit = cfg.UploadRateLimiter
 	}
 	if cfg.DownloadRateLimiter == nil {
-		cl.downloadLimit = rate.NewLimiter(rate.Inf, 0)
+		cl.downloadLimit = unlimited
 	} else {
 		cl.downloadLimit = cfg.DownloadRateLimiter
 	}
diff --git a/connection_test.go b/connection_test.go
index 6b4ccd6b..92af0681 100644
--- a/connection_test.go
+++ b/connection_test.go
@@ -2,6 +2,7 @@ package torrent
 
 import (
 	"io"
+	"net"
 	"sync"
 	"testing"
 	"time"
@@ -85,7 +86,9 @@ func (me *torrentStorage) WriteAt(b []byte, _ int64) (int, error) {
 }
 
 func BenchmarkConnectionMainReadLoop(b *testing.B) {
-	cl := &Client{}
+	cl := &Client{
+		downloadLimit: unlimited,
+	}
 	ts := &torrentStorage{}
 	t := &Torrent{
 		cl:                cl,
@@ -99,11 +102,9 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
 	}))
 	t.setChunkSize(defaultChunkSize)
 	t.pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
-	r, w := io.Pipe()
-	cn := &connection{
-		t: t,
-		r: r,
-	}
+	r, w := net.Pipe()
+	cn := cl.newConnection(r)
+	cn.setTorrent(t)
 	mrlErr := make(chan error)
 	cl.mu.Lock()
 	go func() {
diff --git a/misc.go b/misc.go
index 6fdb77c0..10df1244 100644
--- a/misc.go
+++ b/misc.go
@@ -7,6 +7,7 @@ import (
 	"github.com/anacrolix/missinggo"
 	"github.com/anacrolix/torrent/metainfo"
 	pp "github.com/anacrolix/torrent/peer_protocol"
+	"golang.org/x/time/rate"
 )
 
 type chunkSpec struct {
@@ -165,3 +166,5 @@ func min(as ...int64) int64 {
 	}
 	return ret
 }
+
+var unlimited = rate.NewLimiter(rate.Inf, 0)
-- 
2.51.0