]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add tests inspired by issue #27
authorMatt Joiner <anacrolix@gmail.com>
Sun, 23 Aug 2015 02:50:32 +0000 (12:50 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 23 Aug 2015 02:50:32 +0000 (12:50 +1000)
client_test.go

index b52ea51079865119f7b65be13fef8a09387eb94d..47f1371594fb16a4fb2024b455c7ed899f25d5d7 100644 (file)
@@ -525,3 +525,55 @@ func TestDHTInheritBlocklist(t *testing.T) {
        defer cl.Close()
        require.Equal(t, ipl, cl.DHT().IPBlocklist())
 }
+
+// Check that stuff is merged in subsequent AddTorrentSpec for the same
+// infohash.
+func TestAddTorrentSpecMerging(t *testing.T) {
+       cl, err := NewClient(&TestingConfig)
+       require.NoError(t, err)
+       defer cl.Close()
+       dir, mi := testutil.GreetingTestTorrent()
+       defer os.RemoveAll(dir)
+       var ts TorrentSpec
+       missinggo.CopyExact(&ts.InfoHash, mi.Info.Hash)
+       tt, new, err := cl.AddTorrentSpec(&ts)
+       require.NoError(t, err)
+       require.True(t, new)
+       require.Nil(t, tt.Info())
+       _, new, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
+       require.NoError(t, err)
+       require.False(t, new)
+       require.NotNil(t, tt.Info())
+}
+
+// Check that torrent Info is obtained from the metainfo file cache.
+func TestAddTorrentMetainfoInCache(t *testing.T) {
+       cfg := TestingConfig
+       cfg.DisableMetainfoCache = false
+       cfg.ConfigDir, _ = ioutil.TempDir(os.TempDir(), "")
+       defer os.RemoveAll(cfg.ConfigDir)
+       cl, err := NewClient(&cfg)
+       require.NoError(t, err)
+       defer cl.Close()
+       dir, mi := testutil.GreetingTestTorrent()
+       defer os.RemoveAll(dir)
+       tt, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
+       require.NoError(t, err)
+       require.True(t, new)
+       require.NotNil(t, tt.Info())
+       _, err = os.Stat(filepath.Join(cfg.ConfigDir, "torrents", fmt.Sprintf("%x.torrent", mi.Info.Hash)))
+       require.NoError(t, err)
+       // Contains only the infohash.
+       var ts TorrentSpec
+       missinggo.CopyExact(&ts.InfoHash, mi.Info.Hash)
+       _, ok := cl.Torrent(ts.InfoHash)
+       require.True(t, ok)
+       tt.Drop()
+       _, ok = cl.Torrent(ts.InfoHash)
+       require.False(t, ok)
+       tt, new, err = cl.AddTorrentSpec(&ts)
+       require.NoError(t, err)
+       require.True(t, new)
+       // Obtained from the metainfo cache.
+       require.NotNil(t, tt.Info())
+}