Replace internal/tmproot with Testing.TB.TempDir

This commit is contained in:
Matt Joiner 2021-02-22 14:30:09 +11:00
parent 91454b65fa
commit 7f7c59d52e
13 changed files with 52 additions and 117 deletions

View File

@ -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()
}
@ -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)
@ -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()
@ -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()
@ -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) {
@ -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{}
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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)
@ -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()
@ -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(),
@ -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
@ -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 } }
@ -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)
@ -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)
@ -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,
@ -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()
@ -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)
@ -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)
@ -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) {
@ -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)
@ -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)
@ -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)
@ -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
@ -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

View File

@ -1,56 +0,0 @@
package tmproot
import (
"io/ioutil"
"os"
"sync"
)
type Dir struct {
mu sync.Mutex
path string
inited bool
}
func (me *Dir) init(prefix string) bool {
if me.inited {
return false
}
var err error
me.path, err = ioutil.TempDir("", prefix)
if err != nil {
panic(err)
}
me.inited = true
return true
}
func (me *Dir) Init(prefix string) {
me.mu.Lock()
defer me.mu.Unlock()
if me.inited {
panic("already inited")
}
me.init(prefix)
}
func (me *Dir) lazyDefaultInit() {
me.mu.Lock()
defer me.mu.Unlock()
me.init("")
}
func (me *Dir) NewSub() string {
me.lazyDefaultInit()
ret, err := ioutil.TempDir(me.path, "")
if err != nil {
panic(err)
}
return ret
}
func (me *Dir) RemoveAll() error {
me.lazyDefaultInit()
return os.RemoveAll(me.path)
}

View File

@ -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)

View File

@ -13,9 +13,7 @@ func init() {
}
func TestMain(m *testing.M) {
TestingTempDir.Init("torrent.test")
code := m.Run()
TestingTempDir.RemoveAll()
// select {}
os.Exit(code)
}

View File

@ -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", "")

View File

@ -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)

View File

@ -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())

View File

@ -2,9 +2,4 @@ package test
import (
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/torrent"
)
func init() {
torrent.TestingTempDir.Init("torrent-test.test")
}

View File

@ -36,7 +36,7 @@ func TestReceiveChunkStorageFailure(t *testing.T) {
func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
seederDataDir, metainfo := testutil.GreetingTestTorrent()
defer os.RemoveAll(seederDataDir)
seederClientConfig := torrent.TestingConfig()
seederClientConfig := torrent.TestingConfig(t)
seederClientConfig.Debug = true
justOneNetwork(seederClientConfig)
seederClientStorage := storage.NewMMap(seederDataDir)
@ -49,7 +49,7 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
require.NoError(t, err)
defer seederClient.Close()
defer testutil.ExportStatusWriter(seederClient, "s", t)()
leecherClientConfig := torrent.TestingConfig()
leecherClientConfig := torrent.TestingConfig(t)
leecherClientConfig.Debug = true
justOneNetwork(leecherClientConfig)
leecherClient, err := torrent.NewClient(leecherClientConfig)

View File

@ -62,7 +62,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
greetingTempDir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(greetingTempDir)
// Create seeder and a Torrent.
cfg := torrent.TestingConfig()
cfg := torrent.TestingConfig(t)
cfg.Seed = true
// Some test instances don't like this being on, even when there's no cache involved.
cfg.DropMutuallyCompletePeers = false
@ -96,7 +96,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
leecherDataDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(leecherDataDir)
cfg = torrent.TestingConfig()
cfg = torrent.TestingConfig(t)
// See the seeder client config comment.
cfg.DropMutuallyCompletePeers = false
if ps.LeecherStorage == nil {
@ -388,7 +388,7 @@ func TestSeedAfterDownloading(t *testing.T) {
greetingTempDir, mi := testutil.GreetingTestTorrent()
defer os.RemoveAll(greetingTempDir)
cfg := torrent.TestingConfig()
cfg := torrent.TestingConfig(t)
cfg.Seed = true
cfg.DataDir = greetingTempDir
seeder, err := torrent.NewClient(cfg)
@ -400,7 +400,7 @@ func TestSeedAfterDownloading(t *testing.T) {
assert.True(t, ok)
seederTorrent.VerifyData()
cfg = torrent.TestingConfig()
cfg = torrent.TestingConfig(t)
cfg.Seed = true
cfg.DataDir, err = ioutil.TempDir("", "")
require.NoError(t, err)
@ -410,7 +410,7 @@ func TestSeedAfterDownloading(t *testing.T) {
defer leecher.Close()
defer testutil.ExportStatusWriter(leecher, "l", t)()
cfg = torrent.TestingConfig()
cfg = torrent.TestingConfig(t)
cfg.Seed = false
cfg.DataDir, err = ioutil.TempDir("", "")
require.NoError(t, err)

View File

@ -25,7 +25,7 @@ func TestUnixConns(t *testing.T) {
},
Client: func(cl *torrent.Client) {
cl.AddDialer(torrent.NetDialer{Network: "unix"})
l, err := net.Listen("unix", filepath.Join(torrent.TestingTempDir.NewSub(), "socket"))
l, err := net.Listen("unix", filepath.Join(t.TempDir(), "socket"))
if err != nil {
panic(err)
}

View File

@ -1,16 +1,14 @@
package torrent
import (
"github.com/anacrolix/torrent/internal/tmproot"
"testing"
)
var TestingTempDir tmproot.Dir
func TestingConfig() *ClientConfig {
func TestingConfig(t testing.TB) *ClientConfig {
cfg := NewDefaultClientConfig()
cfg.ListenHost = LoopbackListenHost
cfg.NoDHT = true
cfg.DataDir = TestingTempDir.NewSub()
cfg.DataDir = t.TempDir()
cfg.DisableTrackers = true
cfg.NoDefaultPortForwarding = true
cfg.DisableAcceptRateLimiting = true

View File

@ -81,7 +81,7 @@ func BenchmarkUpdatePiecePriorities(b *testing.B) {
numPieces = 13410
pieceLength = 256 << 10
)
cl := &Client{config: TestingConfig()}
cl := &Client{config: TestingConfig(b)}
cl.initLogger()
t := cl.newTorrent(metainfo.Hash{}, nil)
require.NoError(b, t.setInfo(&metainfo.Info{
@ -132,7 +132,7 @@ func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *ClientConfig) {
}
func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
ci := storage.NewFile(cfg.DataDir)
defer ci.Close()
cfg.DefaultStorage = ci
@ -140,7 +140,7 @@ func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
}
func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
ci := storage.NewMMap(cfg.DataDir)
defer ci.Close()
cfg.DefaultStorage = ci
@ -150,7 +150,7 @@ func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
func TestPieceHashFailed(t *testing.T) {
mi := testutil.GreetingMetaInfo()
cl := new(Client)
cl.config = TestingConfig()
cl.config = TestingConfig(t)
cl.initLogger()
tt := cl.newTorrent(mi.HashInfoBytes(), badStorage{})
tt.setChunkSize(2)
@ -166,7 +166,7 @@ func TestPieceHashFailed(t *testing.T) {
// Check the behaviour of Torrent.Metainfo when metadata is not completed.
func TestTorrentMetainfoIncompleteMetadata(t *testing.T) {
cfg := TestingConfig()
cfg := TestingConfig(t)
cfg.Debug = true
cl, err := NewClient(cfg)
require.NoError(t, err)