Skip to content

Commit

Permalink
add a test for multiple readers
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Mar 16, 2024
1 parent d69bd3d commit 49ace73
Showing 1 changed file with 62 additions and 29 deletions.
91 changes: 62 additions & 29 deletions sqlite_zstd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sync"
"testing"

sqlitezstd "github.com/jtarchie/sqlitezstd"
Expand All @@ -19,46 +20,52 @@ func TestSqliteZstd(t *testing.T) {
RunSpecs(t, "SqliteZstd Suite")
}

var _ = Describe("SqliteZSTD", func() {
BeforeEach(func() {
err := sqlitezstd.Init()
Expect(err).ToNot(HaveOccurred())
})
func createDatabase() string {
buildPath, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())

It("can read from a compressed sqlite db", func() {
buildPath, err := os.MkdirTemp("", "")
Expect(err).ToNot(HaveOccurred())
dbPath := filepath.Join(buildPath, "test.sqlite")

dbPath := filepath.Join(buildPath, "test.sqlite")
client, err := sql.Open("sqlite3", dbPath)
Expect(err).ToNot(HaveOccurred())

client, err := sql.Open("sqlite3", dbPath)
Expect(err).ToNot(HaveOccurred())
_, err = client.Exec(`
CREATE TABLE entries (
id INTEGER PRIMARY KEY
);
`)
Expect(err).ToNot(HaveOccurred())

_, err = client.Exec(`
CREATE TABLE entries (
id INTEGER PRIMARY KEY
);
`)
for id := 1; id <= 1000; id++ {
_, err = client.Exec("INSERT INTO entries (id) VALUES (?)", id)
Expect(err).ToNot(HaveOccurred())
}

for id := 1; id <= 1000; id++ {
_, err = client.Exec("INSERT INTO entries (id) VALUES (?)", id)
Expect(err).ToNot(HaveOccurred())
}
zstPath := dbPath + ".zst"

command := exec.Command(
"go", "run", "github.com/SaveTheRbtz/zstd-seekable-format-go/cmd/zstdseek",
"-f", dbPath,
"-o", zstPath,
)

zstPath := dbPath + ".zst"
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))

command := exec.Command(
"go", "run", "github.com/SaveTheRbtz/zstd-seekable-format-go/cmd/zstdseek",
"-f", dbPath,
"-o", zstPath,
)
return zstPath
}

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
var _ = Describe("SqliteZSTD", func() {
BeforeEach(func() {
err := sqlitezstd.Init()
Expect(err).ToNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
})

It("can read from a compressed sqlite db", func() {
zstPath := createDatabase()

client, err = sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()

Expand All @@ -70,4 +77,30 @@ var _ = Describe("SqliteZSTD", func() {
Expect(err).ToNot(HaveOccurred())
Expect(count).To(BeEquivalentTo(1000))
})

It("can handle multiple readers", func() {
zstPath := createDatabase()

waiter := &sync.WaitGroup{}

for i := 0; i < 5; i++ {
waiter.Add(1)

go func() {
defer waiter.Done()
defer GinkgoRecover()

client, err := sql.Open("sqlite3", fmt.Sprintf("%s?vfs=zstd&mode=ro&immutable=true&synchronous=off", zstPath))
Expect(err).ToNot(HaveOccurred())
defer client.Close()

for i := 0; i < 1_000; i++ {
row := client.QueryRow("SELECT * FROM entries ORDER BY RANDOM() LIMIT 1;")
Expect(row.Err()).ToNot(HaveOccurred())
}
}()
}

waiter.Wait()
})
})

0 comments on commit 49ace73

Please sign in to comment.