From 49ace73516bae8dd0b2fef6347635ac4121c70dd Mon Sep 17 00:00:00 2001 From: JT Archie Date: Sat, 16 Mar 2024 07:23:34 -0600 Subject: [PATCH] add a test for multiple readers --- sqlite_zstd_suite_test.go | 91 ++++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 29 deletions(-) diff --git a/sqlite_zstd_suite_test.go b/sqlite_zstd_suite_test.go index 1973aa5..ad42d6b 100644 --- a/sqlite_zstd_suite_test.go +++ b/sqlite_zstd_suite_test.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "sync" "testing" sqlitezstd "github.com/jtarchie/sqlitezstd" @@ -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() @@ -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() + }) })