Skip to content

Commit

Permalink
Replace internal/tmproot with Testing.TB.TempDir
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Feb 22, 2021
1 parent 91454b6 commit 7f7c59d
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 117 deletions.
64 changes: 32 additions & 32 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestClientDefault(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
cl.Close()
}
Expand All @@ -41,7 +41,7 @@ func TestClientNilConfig(t *testing.T) {
}

func TestBoltPieceCompletionClosedWhenClientClosed(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
pc, err := storage.NewBoltPieceCompletion(cfg.DataDir)
require.NoError(t, err)
ci := storage.NewFileWithCompletion(cfg.DataDir, pc)
Expand All @@ -57,7 +57,7 @@ func TestBoltPieceCompletionClosedWhenClientClosed(t *testing.T) {
}

func TestAddDropTorrent(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
dir, mi := testutil.GreetingTestTorrent()
Expand Down Expand Up @@ -93,12 +93,12 @@ func TestTorrentInitialState(t *testing.T) {
dir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(dir)
cl := &Client{
config: TestingConfig(),
config: TestingConfig(t),
}
cl.initLogger()
tor := cl.newTorrent(
mi.HashInfoBytes(),
storage.NewFileWithCompletion(TestingTempDir.NewSub(), storage.NewMapPieceCompletion()),
storage.NewFileWithCompletion(t.TempDir(), storage.NewMapPieceCompletion()),
)
tor.setChunkSize(2)
tor.cl.lock()
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestReducedDialTimeout(t *testing.T) {
}

func TestAddDropManyTorrents(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
for i := range iter.N(1000) {
Expand All @@ -163,7 +163,7 @@ func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImpl {
}

func TestMergingTrackersByAddingSpecs(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
spec := TorrentSpec{}
Expand All @@ -181,7 +181,7 @@ func TestMergingTrackersByAddingSpecs(t *testing.T) {

// We read from a piece which is marked completed, but is missing data.
func TestCompletedPieceWrongSize(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DefaultStorage = badStorage{}
cl, err := NewClient(cfg)
require.NoError(t, err)
Expand All @@ -208,7 +208,7 @@ func TestCompletedPieceWrongSize(t *testing.T) {
}

func BenchmarkAddLargeTorrent(b *testing.B) {
cfg := TestingConfig()
cfg := TestingConfig(b)
cfg.DisableTCP = true
cfg.DisableUTP = true
cl, err := NewClient(cfg)
Expand All @@ -227,7 +227,7 @@ func BenchmarkAddLargeTorrent(b *testing.B) {
func TestResponsive(t *testing.T) {
seederDataDir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(seederDataDir)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Seed = true
cfg.DataDir = seederDataDir
seeder, err := NewClient(cfg)
Expand All @@ -238,7 +238,7 @@ func TestResponsive(t *testing.T) {
leecherDataDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
defer os.RemoveAll(leecherDataDir)
cfg = TestingConfig()
cfg = TestingConfig(t)
cfg.DataDir = leecherDataDir
leecher, err := NewClient(cfg)
require.Nil(t, err)
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestResponsive(t *testing.T) {
func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
seederDataDir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(seederDataDir)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Seed = true
cfg.DataDir = seederDataDir
seeder, err := NewClient(cfg)
Expand All @@ -281,7 +281,7 @@ func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
leecherDataDir, err := ioutil.TempDir("", "")
require.Nil(t, err)
defer os.RemoveAll(leecherDataDir)
cfg = TestingConfig()
cfg = TestingConfig(t)
cfg.DataDir = leecherDataDir
leecher, err := NewClient(cfg)
require.Nil(t, err)
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestTorrentDroppedDuringResponsiveRead(t *testing.T) {
func TestDhtInheritBlocklist(t *testing.T) {
ipl := iplist.New(nil)
require.NotNil(t, ipl)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.IPBlocklist = ipl
cfg.NoDHT = false
cl, err := NewClient(cfg)
Expand All @@ -331,7 +331,7 @@ func TestDhtInheritBlocklist(t *testing.T) {
// Check that stuff is merged in subsequent AddTorrentSpec for the same
// infohash.
func TestAddTorrentSpecMerging(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
dir, mi := testutil.GreetingTestTorrent()
Expand All @@ -351,7 +351,7 @@ func TestAddTorrentSpecMerging(t *testing.T) {
func TestTorrentDroppedBeforeGotInfo(t *testing.T) {
dir, mi := testutil.GreetingTestTorrent()
os.RemoveAll(dir)
cl, _ := NewClient(TestingConfig())
cl, _ := NewClient(TestingConfig(t))
defer cl.Close()
tt, _, _ := cl.AddTorrentSpec(&TorrentSpec{
InfoHash: mi.HashInfoBytes(),
Expand Down Expand Up @@ -395,7 +395,7 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf
require.NoError(t, greetingData.Piece(p).MarkComplete())
}
}
cfg := TestingConfig()
cfg := TestingConfig(t)
// TODO: Disable network option?
cfg.DisableTCP = true
cfg.DisableUTP = true
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) {
}

func TestAddMetainfoWithNodes(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.ListenHost = func(string) string { return "" }
cfg.NoDHT = false
cfg.DhtStartingNodes = func(string) dht.StartingNodesGetter { return func() ([]dht.Addr, error) { return nil, nil } }
Expand Down Expand Up @@ -461,7 +461,7 @@ type testDownloadCancelParams struct {
func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
greetingTempDir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(greetingTempDir)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Seed = true
cfg.DataDir = greetingTempDir
seeder, err := NewClient(cfg)
Expand Down Expand Up @@ -530,7 +530,7 @@ func TestTorrentDownloadAllThenCancel(t *testing.T) {

// Ensure that it's an error for a peer to send an invalid have message.
func TestPeerInvalidHave(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DropMutuallyCompletePeers = false
cl, err := NewClient(cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -561,9 +561,9 @@ func TestPeerInvalidHave(t *testing.T) {
func TestPieceCompletedInStorageButNotClient(t *testing.T) {
greetingTempDir, greetingMetainfo := testutil.GreetingTestTorrent()
defer os.RemoveAll(greetingTempDir)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DataDir = greetingTempDir
seeder, err := NewClient(TestingConfig())
seeder, err := NewClient(TestingConfig(t))
require.NoError(t, err)
seeder.AddTorrentSpec(&TorrentSpec{
InfoBytes: greetingMetainfo.InfoBytes,
Expand All @@ -573,7 +573,7 @@ func TestPieceCompletedInStorageButNotClient(t *testing.T) {
// Check that when the listen port is 0, all the protocols listened on have
// the same port, and it isn't zero.
func TestClientDynamicListenPortAllProtocols(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
port := cl.LocalPort()
Expand All @@ -585,7 +585,7 @@ func TestClientDynamicListenPortAllProtocols(t *testing.T) {
}

func TestClientDynamicListenTCPOnly(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DisableUTP = true
cfg.DisableTCP = false
cl, err := NewClient(cfg)
Expand All @@ -595,7 +595,7 @@ func TestClientDynamicListenTCPOnly(t *testing.T) {
}

func TestClientDynamicListenUTPOnly(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DisableTCP = true
cfg.DisableUTP = false
cl, err := NewClient(cfg)
Expand All @@ -616,7 +616,7 @@ func totalConns(tts []*Torrent) (ret int) {
func TestSetMaxEstablishedConn(t *testing.T) {
var tts []*Torrent
ih := testutil.GreetingMetaInfo().HashInfoBytes()
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DisableAcceptRateLimiting = true
cfg.DropDuplicatePeerIds = true
for i := range iter.N(3) {
Expand Down Expand Up @@ -697,7 +697,7 @@ func TestMultipleTorrentsWithEncryption(t *testing.T) {
// Test that the leecher can download a torrent in its entirety from the seeder. Note that the
// seeder config is done first.
func testSeederLeecherPair(t *testing.T, seeder func(*ClientConfig), leecher func(*ClientConfig)) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Seed = true
cfg.DataDir = filepath.Join(cfg.DataDir, "server")
os.Mkdir(cfg.DataDir, 0755)
Expand All @@ -713,7 +713,7 @@ func testSeederLeecherPair(t *testing.T, seeder func(*ClientConfig), leecher fun
for i := 0; i < 100; i++ {
makeMagnet(t, server, cfg.DataDir, fmt.Sprintf("test%d", i+2))
}
cfg = TestingConfig()
cfg = TestingConfig(t)
cfg.DataDir = filepath.Join(cfg.DataDir, "client")
leecher(cfg)
client, err := NewClient(cfg)
Expand Down Expand Up @@ -764,14 +764,14 @@ func TestClientAddressInUse(t *testing.T) {
if s != nil {
defer s.Close()
}
cfg := TestingConfig().SetListenAddr(":50007")
cfg := TestingConfig(t).SetListenAddr(":50007")
cl, err := NewClient(cfg)
require.Error(t, err)
require.Nil(t, cl)
}

func TestClientHasDhtServersWhenUtpDisabled(t *testing.T) {
cc := TestingConfig()
cc := TestingConfig(t)
cc.DisableUTP = true
cc.NoDHT = false
cl, err := NewClient(cc)
Expand All @@ -783,7 +783,7 @@ func TestClientHasDhtServersWhenUtpDisabled(t *testing.T) {
func TestIssue335(t *testing.T) {
dir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(dir)
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Seed = false
cfg.Debug = true
cfg.DataDir = dir
Expand All @@ -806,7 +806,7 @@ func TestIssue335(t *testing.T) {
}

func TestClientDisabledImplicitNetworksButDhtEnabled(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.DisableTCP = true
cfg.DisableUTP = true
cfg.NoDHT = false
Expand Down
56 changes: 0 additions & 56 deletions internal/tmproot/dir.go

This file was deleted.

2 changes: 1 addition & 1 deletion issue211_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestDropTorrentWithMmapStorageWhileHashing(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
// Ensure the data is present when the torrent is added, and not obtained
// over the network as the test runs.
cfg.DownloadRateLimiter = rate.NewLimiter(0, 0)
Expand Down
2 changes: 0 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ func init() {
}

func TestMain(m *testing.M) {
TestingTempDir.Init("torrent.test")
code := m.Run()
TestingTempDir.RemoveAll()
// select {}
os.Exit(code)
}
2 changes: 1 addition & 1 deletion peerconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// Have that would potentially alter it.
func TestSendBitfieldThenHave(t *testing.T) {
cl := Client{
config: TestingConfig(),
config: TestingConfig(t),
}
cl.initLogger()
c := cl.newConnection(nil, false, nil, "io.Pipe", "")
Expand Down
2 changes: 1 addition & 1 deletion pexconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestPexConnState(t *testing.T) {
cl := Client{
config: TestingConfig(),
config: TestingConfig(t),
}
cl.initLogger()
torrent := cl.newTorrent(metainfo.Hash{}, nil)
Expand Down
2 changes: 1 addition & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestReaderReadContext(t *testing.T) {
cl, err := NewClient(TestingConfig())
cl, err := NewClient(TestingConfig(t))
require.NoError(t, err)
defer cl.Close()
tt, err := cl.AddTorrent(testutil.GreetingMetaInfo())
Expand Down
5 changes: 0 additions & 5 deletions test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@ package test

import (
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/torrent"
)

func init() {
torrent.TestingTempDir.Init("torrent-test.test")
}
Loading

0 comments on commit 7f7c59d

Please sign in to comment.