github.com/anacrolix/torrent@v1.61.0/client-nowasm_test.go (about)

     1  //go:build !wasm
     2  // +build !wasm
     3  
     4  package torrent
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	qt "github.com/go-quicktest/qt"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/anacrolix/torrent/internal/testutil"
    14  	"github.com/anacrolix/torrent/storage"
    15  )
    16  
    17  func TestBoltPieceCompletionClosedWhenClientClosed(t *testing.T) {
    18  	cfg := TestingConfig(t)
    19  	pc, err := storage.NewBoltPieceCompletion(cfg.DataDir)
    20  	require.NoError(t, err)
    21  	ci := storage.NewFileWithCompletion(cfg.DataDir, pc)
    22  	defer ci.Close()
    23  	cfg.DefaultStorage = ci
    24  	cl, err := NewClient(cfg)
    25  	qt.Assert(t, qt.IsNil(err), qt.Commentf("%#v", err))
    26  	cl.Close()
    27  	// And again, https://github.com/anacrolix/torrent/issues/158
    28  	cl, err = NewClient(cfg)
    29  	require.NoError(t, err)
    30  	cl.Close()
    31  }
    32  
    33  func TestIssue335(t *testing.T) {
    34  	dir, mi := testutil.GreetingTestTorrent()
    35  	defer func() {
    36  		err := os.RemoveAll(dir)
    37  		if err != nil {
    38  			t.Fatalf("removing torrent dummy data dir: %v", err)
    39  		}
    40  	}()
    41  	logErr := func(f func() error, msg string) {
    42  		err := f()
    43  		t.Logf("%s: %v", msg, err)
    44  		if err != nil {
    45  			t.Fail()
    46  		}
    47  	}
    48  	cfg := TestingConfig(t)
    49  	cfg.Seed = false
    50  	cfg.Debug = true
    51  	cfg.DataDir = dir
    52  	comp, err := storage.NewBoltPieceCompletion(dir)
    53  	qt.Assert(t, qt.IsNil(err))
    54  	defer logErr(comp.Close, "closing bolt piece completion")
    55  	mmapStorage := storage.NewMMapWithCompletion(dir, comp)
    56  	defer mmapStorage.Close()
    57  	cfg.DefaultStorage = mmapStorage
    58  	cl, err := NewClient(cfg)
    59  	qt.Assert(t, qt.IsNil(err))
    60  	defer cl.Close()
    61  	tor, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
    62  	qt.Assert(t, qt.IsNil(err))
    63  	qt.Assert(t, qt.IsTrue(new))
    64  	qt.Assert(t, qt.IsTrue(cl.WaitAll()))
    65  	tor.Drop()
    66  	_, new, err = cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
    67  	qt.Assert(t, qt.IsNil(err))
    68  	qt.Assert(t, qt.IsTrue(new))
    69  	qt.Assert(t, qt.IsTrue(cl.WaitAll()))
    70  }