]> Sergey Matveev's repositories - btrtrc.git/blobdiff - storage/sqlite/sqlite-storage_test.go
Drop anacrolix/squirrel cache blob usage
[btrtrc.git] / storage / sqlite / sqlite-storage_test.go
index 633d4358b4ebefe2b1fbc9fe3b2ecb10395e5314..00ab449b92efda2696f0a6a57aec2ecd5439b80d 100644 (file)
+//go:build cgo
+// +build cgo
+
 package sqliteStorage
 
 import (
-       "bytes"
        "errors"
        "fmt"
-       "io"
-       "io/ioutil"
+       _ "github.com/anacrolix/envpprof"
+       "github.com/anacrolix/squirrel"
+       "github.com/dustin/go-humanize"
+       qt "github.com/frankban/quicktest"
        "path/filepath"
-       "sync"
        "testing"
 
-       _ "github.com/anacrolix/envpprof"
        "github.com/anacrolix/torrent/storage"
        test_storage "github.com/anacrolix/torrent/storage/test"
-       "github.com/dustin/go-humanize"
-       qt "github.com/frankban/quicktest"
-       "github.com/stretchr/testify/assert"
-       "github.com/stretchr/testify/require"
+       "github.com/anacrolix/torrent/test"
 )
 
-func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
-       opts.Path = filepath.Join(t.TempDir(), "sqlite3.db")
-       pool, err := NewPool(opts)
-       qt.Assert(t, err, qt.IsNil)
-       // sqlitex.Pool.Close doesn't like being called more than once. Let it slide for now.
-       //t.Cleanup(func() { pool.Close() })
-       qt.Assert(t, initPoolDatabase(pool, InitDbOpts{}), qt.IsNil)
-       prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
-       require.NoError(t, err)
-       t.Cleanup(func() { prov.Close() })
-       return pool, prov
-}
-
-func TestTextBlobSize(t *testing.T) {
-       _, prov := newConnsAndProv(t, NewPoolOpts{})
-       a, _ := prov.NewInstance("a")
-       err := a.Put(bytes.NewBufferString("\x00hello"))
-       qt.Assert(t, err, qt.IsNil)
-       fi, err := a.Stat()
-       qt.Assert(t, err, qt.IsNil)
-       assert.EqualValues(t, 6, fi.Size())
-}
-
-func TestSimultaneousIncrementalBlob(t *testing.T) {
-       _, p := newConnsAndProv(t, NewPoolOpts{
-               NumConns: 3,
+func TestLeecherStorage(t *testing.T) {
+       test.TestLeecherStorage(t, test.LeecherStorageTestCase{
+               "SqliteDirect",
+               func(s string) storage.ClientImplCloser {
+                       path := filepath.Join(s, "sqlite3.db")
+                       var opts NewDirectStorageOpts
+                       opts.Path = path
+                       cl, err := NewDirectStorage(opts)
+                       if err != nil {
+                               panic(err)
+                       }
+                       return cl
+               },
+               0,
        })
-       a, err := p.NewInstance("a")
-       require.NoError(t, err)
-       const contents = "hello, world"
-       require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
-       rc1, err := a.Get()
-       require.NoError(t, err)
-       rc2, err := a.Get()
-       require.NoError(t, err)
-       var b1, b2 []byte
-       var e1, e2 error
-       var wg sync.WaitGroup
-       doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
-               defer wg.Done()
-               defer rc.Close()
-               *b, *e = ioutil.ReadAll(rc)
-               require.NoError(t, *e, n)
-               assert.EqualValues(t, contents, *b)
-       }
-       wg.Add(2)
-       go doRead(&b2, &e2, rc2, 2)
-       go doRead(&b1, &e1, rc1, 1)
-       wg.Wait()
 }
 
 func BenchmarkMarkComplete(b *testing.B) {
        const pieceSize = test_storage.DefaultPieceSize
-       const capacity = test_storage.DefaultNumPieces * pieceSize / 2
+       const noTriggers = false
+       var capacity int64 = test_storage.DefaultNumPieces * pieceSize / 2
+       if noTriggers {
+               // Since we won't push out old pieces, we have to mark them incomplete manually.
+               capacity = 0
+       }
        runBench := func(b *testing.B, ci storage.ClientImpl) {
                test_storage.BenchmarkPieceMarkComplete(b, ci, pieceSize, test_storage.DefaultNumPieces, capacity)
        }
        c := qt.New(b)
+       b.Run("CustomDirect", func(b *testing.B) {
+               var opts squirrel.NewCacheOpts
+               opts.Capacity = capacity
+               opts.NoTriggers = noTriggers
+               benchOpts := func(b *testing.B) {
+                       opts.Path = filepath.Join(b.TempDir(), "storage.db")
+                       ci, err := NewDirectStorage(opts)
+                       c.Assert(err, qt.IsNil)
+                       defer ci.Close()
+                       runBench(b, ci)
+               }
+               b.Run("Default", benchOpts)
+       })
        for _, memory := range []bool{false, true} {
                b.Run(fmt.Sprintf("Memory=%v", memory), func(b *testing.B) {
                        b.Run("Direct", func(b *testing.B) {
                                var opts NewDirectStorageOpts
                                opts.Memory = memory
-                               opts.Path = filepath.Join(b.TempDir(), "storage.db")
                                opts.Capacity = capacity
+                               opts.NoTriggers = noTriggers
                                directBench := func(b *testing.B) {
+                                       opts.Path = filepath.Join(b.TempDir(), "storage.db")
                                        ci, err := NewDirectStorage(opts)
-                                       if errors.Is(err, UnexpectedJournalMode) {
+                                       var ujm squirrel.ErrUnexpectedJournalMode
+                                       if errors.As(err, &ujm) {
                                                b.Skipf("setting journal mode %q: %v", opts.SetJournalMode, err)
                                        }
                                        c.Assert(err, qt.IsNil)
                                        defer ci.Close()
                                        runBench(b, ci)
                                }
-                               for _, journalMode := range []string{"", "wal", "off", "delete", "memory"} {
+                               for _, journalMode := range []string{"", "wal", "off", "truncate", "delete", "persist", "memory"} {
                                        opts.SetJournalMode = journalMode
                                        b.Run("JournalMode="+journalMode, func(b *testing.B) {
-                                               for _, mmapSize := range []int64{-1, 0, 1 << 24, 1 << 25, 1 << 26} {
+                                               for _, mmapSize := range []int64{-1} {
                                                        if memory && mmapSize >= 0 {
                                                                continue
                                                        }
@@ -115,24 +100,6 @@ func BenchmarkMarkComplete(b *testing.B) {
                                        })
                                }
                        })
-                       b.Run("ResourcePieces", func(b *testing.B) {
-                               for _, batchWrites := range []bool{false, true} {
-                                       b.Run(fmt.Sprintf("BatchWrites=%v", batchWrites), func(b *testing.B) {
-                                               var opts NewPiecesStorageOpts
-                                               opts.Path = filepath.Join(b.TempDir(), "storage.db")
-                                               //b.Logf("storage db path: %q", dbPath)
-                                               opts.Capacity = capacity
-                                               opts.Memory = memory
-                                               opts.ProvOpts = func(opts *ProviderOpts) {
-                                                       opts.BatchWrites = batchWrites
-                                               }
-                                               ci, err := NewPiecesStorage(opts)
-                                               c.Assert(err, qt.IsNil)
-                                               defer ci.Close()
-                                               runBench(b, ci)
-                                       })
-                               }
-                       })
                })
        }
 }