Use testing.T.Name instead of missinggo.GetTestName

This handles subtests correctly, and removes the need to specify when to export due to collisions with the old function.
This commit is contained in:
Matt Joiner 2020-10-10 10:59:17 +11:00
parent a9102abf1d
commit 3d7a95a65d
5 changed files with 21 additions and 30 deletions

View File

@ -465,7 +465,7 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
seeder, err := NewClient(cfg)
require.NoError(t, err)
defer seeder.Close()
defer testutil.ExportStatusWriter(seeder, "s")()
defer testutil.ExportStatusWriter(seeder, "s", t)()
seederTorrent, _, _ := seeder.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
seederTorrent.VerifyData()
leecherDataDir, err := ioutil.TempDir("", "")
@ -481,7 +481,7 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) {
leecher, err := NewClient(cfg)
require.NoError(t, err)
defer leecher.Close()
defer testutil.ExportStatusWriter(leecher, "l")()
defer testutil.ExportStatusWriter(leecher, "l", t)()
leecherGreeting, new, err := leecher.AddTorrentSpec(func() (ret *TorrentSpec) {
ret = TorrentSpecFromMetaInfo(mi)
ret.ChunkSize = 2
@ -620,7 +620,7 @@ func TestSetMaxEstablishedConn(t *testing.T) {
defer cl.Close()
tt, _ := cl.AddTorrentInfoHash(ih)
tt.SetMaxEstablishedConns(2)
defer testutil.ExportStatusWriter(cl, fmt.Sprintf("%d", i))()
defer testutil.ExportStatusWriter(cl, fmt.Sprintf("%d", i), t)()
tts = append(tts, tt)
}
addPeers := func() {
@ -700,7 +700,7 @@ func testSeederLeecherPair(t *testing.T, seeder func(*ClientConfig), leecher fun
server, err := NewClient(cfg)
require.NoError(t, err)
defer server.Close()
defer testutil.ExportStatusWriter(server, "s")()
defer testutil.ExportStatusWriter(server, "s", t)()
magnet1 := makeMagnet(t, server, cfg.DataDir, "test1")
// Extra torrents are added to test the seeder having to match incoming obfuscated headers
// against more than one torrent. See issue #114
@ -714,7 +714,7 @@ func testSeederLeecherPair(t *testing.T, seeder func(*ClientConfig), leecher fun
client, err := NewClient(cfg)
require.NoError(t, err)
defer client.Close()
defer testutil.ExportStatusWriter(client, "c")()
defer testutil.ExportStatusWriter(client, "c", t)()
tr, err := client.AddMagnet(magnet1)
require.NoError(t, err)
tr.AddClientPeer(server)

View File

@ -175,7 +175,7 @@ func TestDownloadOnDemand(t *testing.T) {
seeder, err := torrent.NewClient(cfg)
require.NoError(t, err)
defer seeder.Close()
defer testutil.ExportStatusWriter(seeder, "s")()
defer testutil.ExportStatusWriter(seeder, "s", t)()
// Just to mix things up, the seeder starts with the data, but the leecher
// starts with the metainfo.
seederTorrent, err := seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%s", layout.Metainfo.HashInfoBytes().HexString()))
@ -194,7 +194,7 @@ func TestDownloadOnDemand(t *testing.T) {
cfg.ListenPort = 0
leecher, err := torrent.NewClient(cfg)
require.NoError(t, err)
testutil.ExportStatusWriter(leecher, "l")()
testutil.ExportStatusWriter(leecher, "l", t)()
defer leecher.Close()
leecherTorrent, err := leecher.AddTorrent(layout.Metainfo)
require.NoError(t, err)

View File

@ -5,24 +5,24 @@ import (
"io"
"net/http"
"sync"
"testing"
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/missinggo"
)
type StatusWriter interface {
WriteStatus(io.Writer)
}
// The key is the route pattern. The value is nil when the resource is
// released.
// The key is the route pattern. The value is nil when the resource is released.
var (
mu sync.Mutex
sws = map[string]StatusWriter{}
)
func ExportStatusWriter(sw StatusWriter, path string) (release func()) {
pattern := fmt.Sprintf("/%s/%s", missinggo.GetTestName(), path)
func ExportStatusWriter(sw StatusWriter, path string, t *testing.T) (release func()) {
pattern := fmt.Sprintf("/%s/%s", t.Name(), path)
t.Logf("exporting status path %q", pattern)
release = func() {
mu.Lock()
defer mu.Unlock()

View File

@ -46,14 +46,14 @@ func testReceiveChunkStorageFailure(t *testing.T, seederFast bool) {
seederClient, err := torrent.NewClient(seederClientConfig)
require.NoError(t, err)
defer seederClient.Close()
defer testutil.ExportStatusWriter(seederClient, "s")()
defer testutil.ExportStatusWriter(seederClient, "s", t)()
leecherClientConfig := torrent.TestingConfig()
leecherClientConfig.Debug = true
justOneNetwork(leecherClientConfig)
leecherClient, err := torrent.NewClient(leecherClientConfig)
require.NoError(t, err)
defer leecherClient.Close()
defer testutil.ExportStatusWriter(leecherClient, "l")()
defer testutil.ExportStatusWriter(leecherClient, "l", t)()
info, err := metainfo.UnmarshalInfo()
require.NoError(t, err)
leecherStorage := diskFullStorage{

View File

@ -23,7 +23,6 @@ type testClientTransferParams struct {
Responsive bool
Readahead int64
SetReadahead bool
ExportClientStatus bool
LeecherStorage func(string) storage.ClientImplCloser
SeederStorage func(string) storage.ClientImplCloser
SeederUploadRateLimiter *rate.Limiter
@ -70,9 +69,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
if ps.ConfigureSeeder.Client != nil {
ps.ConfigureSeeder.Client(seeder)
}
if ps.ExportClientStatus {
defer testutil.ExportStatusWriter(seeder, "s")()
}
defer testutil.ExportStatusWriter(seeder, "s", t)()
seederTorrent, _, _ := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
// Run a Stats right after Closing the Client. This will trigger the Stats
// panic in #214 caused by RemoteAddr on Closed uTP sockets.
@ -104,9 +101,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
if ps.ConfigureLeecher.Client != nil {
ps.ConfigureLeecher.Client(leecher)
}
if ps.ExportClientStatus {
defer testutil.ExportStatusWriter(leecher, "l")()
}
defer testutil.ExportStatusWriter(leecher, "l", t)()
leecherTorrent, new, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
ret = torrent.TorrentSpecFromMetaInfo(mi)
ret.ChunkSize = 2
@ -193,7 +188,6 @@ type storageFactory func(string) storage.ClientImplCloser
func TestClientTransferDefault(t *testing.T) {
testClientTransfer(t, testClientTransferParams{
ExportClientStatus: true,
LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
Wrapper: fileCachePieceResourceStorage,
}),
@ -202,7 +196,6 @@ func TestClientTransferDefault(t *testing.T) {
func TestClientTransferDefaultNoMetadata(t *testing.T) {
testClientTransfer(t, testClientTransferParams{
ExportClientStatus: true,
LeecherStorage: newFileCacheClientStorageFactory(fileCacheClientStorageFactoryParams{
Wrapper: fileCachePieceResourceStorage,
}),
@ -217,7 +210,6 @@ func TestClientTransferRateLimitedUpload(t *testing.T) {
// chunks are 2 bytes in length. Then the smallest burst we can run
// with is 2. Time taken is (13-burst)/rate.
SeederUploadRateLimiter: rate.NewLimiter(11, 2),
ExportClientStatus: true,
})
require.True(t, time.Since(started) > time.Second)
}
@ -244,8 +236,7 @@ func testClientTransferSmallCache(t *testing.T, setReadahead bool, readahead int
SetReadahead: setReadahead,
// Can't readahead too far or the cache will thrash and drop data we
// thought we had.
Readahead: readahead,
ExportClientStatus: true,
Readahead: readahead,
// These tests don't work well with more than 1 connection to the seeder.
ConfigureLeecher: ConfigureClient{
@ -285,7 +276,7 @@ func TestClientTransferVarious(t *testing.T) {
// Seeder storage
for _, ss := range []struct {
name string
f func(string) storage.ClientImplCloser
f storageFactory
}{
{"File", storage.NewFile},
{"Mmap", storage.NewMMap},
@ -331,7 +322,7 @@ func TestSeedAfterDownloading(t *testing.T) {
seeder, err := torrent.NewClient(cfg)
require.NoError(t, err)
defer seeder.Close()
defer testutil.ExportStatusWriter(seeder, "s")()
defer testutil.ExportStatusWriter(seeder, "s", t)()
seederTorrent, ok, err := seeder.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
require.NoError(t, err)
assert.True(t, ok)
@ -345,7 +336,7 @@ func TestSeedAfterDownloading(t *testing.T) {
leecher, err := torrent.NewClient(cfg)
require.NoError(t, err)
defer leecher.Close()
defer testutil.ExportStatusWriter(leecher, "l")()
defer testutil.ExportStatusWriter(leecher, "l", t)()
cfg = torrent.TestingConfig()
cfg.Seed = false
@ -355,7 +346,7 @@ func TestSeedAfterDownloading(t *testing.T) {
leecherLeecher, _ := torrent.NewClient(cfg)
require.NoError(t, err)
defer leecherLeecher.Close()
defer testutil.ExportStatusWriter(leecherLeecher, "ll")()
defer testutil.ExportStatusWriter(leecherLeecher, "ll", t)()
leecherGreeting, ok, err := leecher.AddTorrentSpec(func() (ret *torrent.TorrentSpec) {
ret = torrent.TorrentSpecFromMetaInfo(mi)
ret.ChunkSize = 2