12 _ "github.com/anacrolix/envpprof"
13 "github.com/anacrolix/torrent/storage"
14 test_storage "github.com/anacrolix/torrent/storage/test"
15 qt "github.com/frankban/quicktest"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
20 func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
21 opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
22 pool, err := NewPool(opts)
23 qt.Assert(t, err, qt.IsNil)
24 // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
25 //t.Cleanup(func() { pool.Close() })
26 qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
27 prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
28 require.NoError(t, err)
29 t.Cleanup(func() { prov.Close() })
33 func TestTextBlobSize(t *testing.T) {
34 _, prov := newConnsAndProv(t, NewPoolOpts{})
35 a, _ := prov.NewInstance("a")
36 err := a.Put(bytes.NewBufferString("\x00hello"))
37 qt.Assert(t, err, qt.IsNil)
39 qt.Assert(t, err, qt.IsNil)
40 assert.EqualValues(t, 6, fi.Size())
43 func TestSimultaneousIncrementalBlob(t *testing.T) {
44 _, p := newConnsAndProv(t, NewPoolOpts{
47 a, err := p.NewInstance("a")
48 require.NoError(t, err)
49 const contents = "hello, world"
50 require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
52 require.NoError(t, err)
54 require.NoError(t, err)
58 doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
61 *b, *e = ioutil.ReadAll(rc)
62 require.NoError(t, *e, n)
63 assert.EqualValues(t, contents, *b)
66 go doRead(&b2, &e2, rc2, 2)
67 go doRead(&b1, &e1, rc1, 1)
71 func BenchmarkMarkComplete(b *testing.B) {
72 const pieceSize = test_storage.DefaultPieceSize
73 const capacity = test_storage.DefaultNumPieces * pieceSize / 2
74 runBench := func(b *testing.B, ci storage.ClientImpl) {
75 test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
78 for _, memory := range []bool{false, true} {
79 b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
80 b.Run("Direct", func(b *testing.B) {
81 var opts NewDirectStorageOpts
83 opts.Path = filepath.Join(b.TempDir(), "storage.db")
84 opts.Capacity = capacity
85 ci, err := NewDirectStorage(opts)
86 c.Assert(err, qt.IsNil)
90 b.Run("ResourcePieces", func(b *testing.B) {
91 for _, batchWrites := range []bool{false, true} {
92 b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
93 var opts NewPiecesStorageOpts
94 opts.Path = filepath.Join(b.TempDir(), "storage.db")
95 //b.Logf("storage db path: %q", dbPath)
96 opts.Capacity = capacity
98 opts.ProvOpts = func(opts *ProviderOpts) {
99 opts.BatchWrites = batchWrites
101 ci, err := NewPiecesStorage(opts)
102 c.Assert(err, qt.IsNil)