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