]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Rework lots of option handling
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
1 package sqliteStorage
2
3 import (
4         "bytes"
5         "fmt"
6         "io"
7         "io/ioutil"
8         "path/filepath"
9         "sync"
10         "testing"
11
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"
18 )
19
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() })
30         return pool, prov
31 }
32
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)
38         fi, err := a.Stat()
39         qt.Assert(t, err, qt.IsNil)
40         assert.EqualValues(t, 6, fi.Size())
41 }
42
43 func TestSimultaneousIncrementalBlob(t *testing.T) {
44         _, p := newConnsAndProv(t, NewPoolOpts{
45                 NumConns: 3,
46         })
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"))))
51         rc1, err := a.Get()
52         require.NoError(t, err)
53         rc2, err := a.Get()
54         require.NoError(t, err)
55         var b1, b2 []byte
56         var e1, e2 error
57         var wg sync.WaitGroup
58         doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
59                 defer wg.Done()
60                 defer rc.Close()
61                 *b, *e = ioutil.ReadAll(rc)
62                 require.NoError(t, *e, n)
63                 assert.EqualValues(t, contents, *b)
64         }
65         wg.Add(2)
66         go doRead(&b2, &e2, rc2, 2)
67         go doRead(&b1, &e1, rc1, 1)
68         wg.Wait()
69 }
70
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)
76         }
77         c := qt.New(b)
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
82                                 opts.Memory = memory
83                                 opts.Path = filepath.Join(b.TempDir(), "storage.db")
84                                 opts.Capacity = capacity
85                                 ci, err := NewDirectStorage(opts)
86                                 c.Assert(err, qt.IsNil)
87                                 defer ci.Close()
88                                 runBench(b, ci)
89                         })
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
97                                                 opts.Memory = memory
98                                                 opts.ProvOpts = func(opts *ProviderOpts) {
99                                                         opts.BatchWrites = batchWrites
100                                                 }
101                                                 ci, err := NewPiecesStorage(opts)
102                                                 c.Assert(err, qt.IsNil)
103                                                 defer ci.Close()
104                                                 runBench(b, ci)
105                                         })
106                                 }
107                         })
108                 })
109         }
110 }