]> Sergey Matveev's repositories - btrtrc.git/blob - tests/webseed-partial-seed/herp_test.go
Don't starve unverified bytes limit on unrequestable pieces
[btrtrc.git] / tests / webseed-partial-seed / herp_test.go
1 package webseed_partial_seed
2
3 import (
4         "path/filepath"
5         "runtime"
6         "testing"
7
8         "github.com/anacrolix/torrent"
9         "github.com/anacrolix/torrent/internal/testutil"
10         qt "github.com/frankban/quicktest"
11 )
12
13 func testSrcDir() string {
14         _, b, _, _ := runtime.Caller(0)
15         return filepath.Dir(b)
16 }
17
18 func makeSeederClient(t *testing.T) *torrent.Client {
19         config := torrent.TestingConfig(t)
20         config.DisableIPv6 = true
21         config.ListenPort = 3030
22         config.Seed = true
23         config.Logger = config.Logger.WithNames("seeder")
24         config.MaxAllocPeerRequestDataPerConn = 1 << 20
25         c, err := torrent.NewClient(config)
26         assertOk(err)
27         return c
28 }
29
30 func makeLeecherClient(t *testing.T) *torrent.Client {
31         config := torrent.TestingConfig(t)
32         config.Debug = true
33         config.DisableIPv6 = true
34         config.Logger = config.Logger.WithNames("leecher")
35         config.DisableWebseeds = true
36         c, err := torrent.NewClient(config)
37         assertOk(err)
38         return c
39 }
40
41 func assertOk(err error) {
42         if err != nil {
43                 panic(err)
44         }
45 }
46
47 func downloadAll(t *torrent.Torrent) {
48         <-t.GotInfo()
49         t.DownloadAll()
50 }
51
52 func TestWebseedPartialSeed(t *testing.T) {
53         c := qt.New(t)
54         seederClient := makeSeederClient(t)
55         defer seederClient.Close()
56         testutil.ExportStatusWriter(seederClient, "seeder", t)
57         const infoHashHex = "a88fda5954e89178c372716a6a78b8180ed4dad3"
58         metainfoPath := filepath.Join(testSrcDir(), "test.img.torrent")
59         seederTorrent, err := seederClient.AddTorrentFromFile(metainfoPath)
60         assertOk(err)
61         leecherClient := makeLeecherClient(t)
62         defer leecherClient.Close()
63         testutil.ExportStatusWriter(leecherClient, "leecher", t)
64         leecherTorrent, _ := leecherClient.AddTorrentFromFile(metainfoPath)
65         // TODO: Check that leecher has pieces before seeder completes. Currently I do this manually by
66         // looking at the GOPPROF http endpoint with the exported status writer
67         // /TestWebseedPartialSeed/leecher.
68         go downloadAll(leecherTorrent)
69         peer := make([]torrent.PeerInfo, 1)
70         peer[0] = torrent.PeerInfo{
71                 Id:                 seederClient.PeerID(),
72                 Addr:               torrent.PeerRemoteAddr(seederClient.ListenAddrs()[0]),
73                 Source:             "",
74                 SupportsEncryption: false,
75                 PexPeerFlags:       0,
76                 Trusted:            false,
77         }
78         go leecherTorrent.AddPeers(peer)
79
80         seederTorrent.DownloadAll()
81         allDownloaded := leecherClient.WaitAll()
82         c.Assert(allDownloaded, qt.IsTrue)
83 }