2021-06-23 07:24:50 +00:00
|
|
|
//go:build !wasm
|
2021-10-28 08:53:04 +00:00
|
|
|
// +build !wasm
|
2021-06-23 07:24:50 +00:00
|
|
|
|
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-27 04:47:25 +00:00
|
|
|
qt "github.com/frankban/quicktest"
|
2021-06-23 07:24:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-12 03:47:47 +00:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/internal/testutil"
|
|
|
|
"github.com/anacrolix/torrent/storage"
|
2021-06-23 07:24:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltPieceCompletionClosedWhenClientClosed(t *testing.T) {
|
|
|
|
cfg := TestingConfig(t)
|
|
|
|
pc, err := storage.NewBoltPieceCompletion(cfg.DataDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
ci := storage.NewFileWithCompletion(cfg.DataDir, pc)
|
|
|
|
defer ci.Close()
|
|
|
|
cfg.DefaultStorage = ci
|
|
|
|
cl, err := NewClient(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
cl.Close()
|
|
|
|
// And again, https://github.com/anacrolix/torrent/issues/158
|
|
|
|
cl, err = NewClient(cfg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
cl.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIssue335(t *testing.T) {
|
|
|
|
dir, mi := testutil.GreetingTestTorrent()
|
2023-05-27 02:40:15 +00:00
|
|
|
defer func() {
|
|
|
|
err := os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("removing torrent dummy data dir: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-05-27 04:47:25 +00:00
|
|
|
logErr := func(f func() error, msg string) {
|
|
|
|
err := f()
|
|
|
|
t.Logf("%s: %v", msg, err)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 07:24:50 +00:00
|
|
|
cfg := TestingConfig(t)
|
|
|
|
cfg.Seed = false
|
|
|
|
cfg.Debug = true
|
|
|
|
cfg.DataDir = dir
|
|
|
|
comp, err := storage.NewBoltPieceCompletion(dir)
|
2023-05-27 04:47:25 +00:00
|
|
|
c := qt.New(t)
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
defer logErr(comp.Close, "closing bolt piece completion")
|
|
|
|
mmapStorage := storage.NewMMapWithCompletion(dir, comp)
|
|
|
|
defer logErr(mmapStorage.Close, "closing mmap storage")
|
|
|
|
cfg.DefaultStorage = mmapStorage
|
2021-06-23 07:24:50 +00:00
|
|
|
cl, err := NewClient(cfg)
|
2023-05-27 04:47:25 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
2023-05-27 05:05:23 +00:00
|
|
|
defer cl.Close()
|
2021-06-23 07:24:50 +00:00
|
|
|
tor, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
2023-05-27 04:47:25 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(new, qt.IsTrue)
|
|
|
|
c.Assert(cl.WaitAll(), qt.IsTrue)
|
2021-06-23 07:24:50 +00:00
|
|
|
tor.Drop()
|
|
|
|
_, new, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
|
2023-05-27 04:47:25 +00:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(new, qt.IsTrue)
|
|
|
|
c.Assert(cl.WaitAll(), qt.IsTrue)
|
2021-06-23 07:24:50 +00:00
|
|
|
}
|