]> Sergey Matveev's repositories - btrtrc.git/blob - storage/sqlite/sqlite-storage_test.go
Performance fiddling on sqlite storage
[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         "crawshaw.io/sqlite"
13         "crawshaw.io/sqlite/sqlitex"
14         _ "github.com/anacrolix/envpprof"
15         "github.com/stretchr/testify/assert"
16         "github.com/stretchr/testify/require"
17 )
18
19 func TestSimultaneousIncrementalBlob(t *testing.T) {
20         const poolSize = 10
21         pool, err := sqlitex.Open(
22                 // We don't do this in memory, because it seems to have some locking issues with updating
23                 // last_used.
24                 fmt.Sprintf("file:%s", filepath.Join(t.TempDir(), "sqlite3.db")),
25                 // We can't disable WAL in this test because then we can't open 2 blobs simultaneously for read.
26                 sqlite.OpenFlagsDefault, /* &^sqlite.SQLITE_OPEN_WAL */
27                 poolSize)
28         require.NoError(t, err)
29         defer pool.Close()
30         p, err := NewProviderPool(pool, poolSize, true)
31         require.NoError(t, err)
32         a, err := p.NewInstance("a")
33         require.NoError(t, err)
34         const contents = "hello, world"
35         require.NoError(t, a.Put(bytes.NewReader([]byte("hello, world"))))
36         rc1, err := a.Get()
37         require.NoError(t, err)
38         rc2, err := a.Get()
39         require.NoError(t, err)
40         var b1, b2 []byte
41         var e1, e2 error
42         var wg sync.WaitGroup
43         doRead := func(b *[]byte, e *error, rc io.ReadCloser, n int) {
44                 defer wg.Done()
45                 defer rc.Close()
46                 *b, *e = ioutil.ReadAll(rc)
47                 require.NoError(t, *e, n)
48                 assert.EqualValues(t, contents, *b)
49         }
50         wg.Add(2)
51         go doRead(&b2, &e2, rc2, 2)
52         go doRead(&b1, &e1, rc1, 1)
53         wg.Wait()
54 }