Test both resource and file backends to piece storage

This commit is contained in:
Matt Joiner 2016-05-16 20:11:00 +10:00
parent 52e285ea86
commit 9b5d3cac03
1 changed files with 48 additions and 28 deletions

View File

@ -239,9 +239,18 @@ func TestAddDropManyTorrents(t *testing.T) {
func TestClientTransferDefault(t *testing.T) {
testClientTransfer(t, testClientTransferParams{
ExportClientStatus: true,
LeecherFileCachePieceStorageFactory: fileCachePieceResourceStorage,
})
}
func fileCachePieceResourceStorage(fc *filecache.Cache) storage.I {
return storage.NewPiecePerResource(fc.AsResourceProvider())
}
func fileCachePieceFileStorage(fc *filecache.Cache) storage.I {
return storage.NewPieceFileStorage(fc.AsFileStore())
}
func TestClientTransferSmallCache(t *testing.T) {
testClientTransfer(t, testClientTransferParams{
SetLeecherStorageCapacity: true,
@ -253,10 +262,15 @@ func TestClientTransferSmallCache(t *testing.T) {
// thought we had.
Readahead: 0,
ExportClientStatus: true,
LeecherFileCachePieceStorageFactory: fileCachePieceResourceStorage,
})
}
func TestClientTransferVarious(t *testing.T) {
for _, lsf := range []func(*filecache.Cache) storage.I{
fileCachePieceFileStorage,
fileCachePieceResourceStorage,
} {
for _, ss := range []func(string) storage.I{
storage.NewFile,
storage.NewMMap,
@ -265,6 +279,7 @@ func TestClientTransferVarious(t *testing.T) {
testClientTransfer(t, testClientTransferParams{
Responsive: responsive,
SeederStorage: ss,
LeecherFileCachePieceStorageFactory: lsf,
})
for _, readahead := range []int64{-1, 0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14, 15, 20} {
testClientTransfer(t, testClientTransferParams{
@ -272,10 +287,12 @@ func TestClientTransferVarious(t *testing.T) {
Responsive: responsive,
SetReadahead: true,
Readahead: readahead,
LeecherFileCachePieceStorageFactory: lsf,
})
}
}
}
}
}
type testClientTransferParams struct {
@ -285,6 +302,7 @@ type testClientTransferParams struct {
ExportClientStatus bool
SetLeecherStorageCapacity bool
LeecherStorageCapacity int64
LeecherFileCachePieceStorageFactory func(*filecache.Cache) storage.I
SeederStorage func(string) storage.I
}
@ -315,7 +333,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) {
if ps.SetLeecherStorageCapacity {
fc.SetCapacity(ps.LeecherStorageCapacity)
}
cfg.DefaultStorage = storage.NewPieceFileStorage(fc.AsFileStore())
cfg.DefaultStorage = ps.LeecherFileCachePieceStorageFactory(fc)
leecher, err := NewClient(&cfg)
require.NoError(t, err)
defer leecher.Close()
@ -678,7 +696,7 @@ func writeTorrentData(ts storage.Torrent, info *metainfo.InfoEx, b []byte) {
}
}
func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool) {
func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf func(*filecache.Cache) storage.I) {
fileCacheDir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(fileCacheDir)
@ -686,7 +704,7 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool) {
require.NoError(t, err)
greetingDataTempDir, greetingMetainfo := testutil.GreetingTestTorrent()
defer os.RemoveAll(greetingDataTempDir)
filePieceStore := storage.NewPieceFileStorage(fileCache.AsFileStore())
filePieceStore := csf(fileCache)
greetingData, err := filePieceStore.OpenTorrent(&greetingMetainfo.Info)
require.NoError(t, err)
writeTorrentData(greetingData, &greetingMetainfo.Info, []byte(testutil.GreetingFileContents))
@ -722,11 +740,13 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool) {
}
func TestAddTorrentPiecesAlreadyCompleted(t *testing.T) {
testAddTorrentPriorPieceCompletion(t, true)
testAddTorrentPriorPieceCompletion(t, true, fileCachePieceFileStorage)
testAddTorrentPriorPieceCompletion(t, true, fileCachePieceResourceStorage)
}
func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) {
testAddTorrentPriorPieceCompletion(t, false)
testAddTorrentPriorPieceCompletion(t, false, fileCachePieceFileStorage)
testAddTorrentPriorPieceCompletion(t, false, fileCachePieceResourceStorage)
}
func TestAddMetainfoWithNodes(t *testing.T) {