From 599a51c02a664271ba40acd9704e27e4be57e7ce Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 30 Dec 2023 11:34:28 +0000
Subject: [PATCH] Run the integration test as a Go test, too

---
 integrationtest/integrationtest.go      | 48 +++++++++++++++----------
 integrationtest/integrationtest_test.go | 11 ++++++
 2 files changed, 40 insertions(+), 19 deletions(-)
 create mode 100644 integrationtest/integrationtest_test.go

diff --git a/integrationtest/integrationtest.go b/integrationtest/integrationtest.go
index b06f480..2027c1b 100644
--- a/integrationtest/integrationtest.go
+++ b/integrationtest/integrationtest.go
@@ -204,36 +204,41 @@ func makeQuery(uri string) error {
 	return nil
 }
 
-func main() {
-	flag.Parse()
-
+func runIntegrationTest(profile bool) error {
 	dir, err := os.MkdirTemp("", "")
 	if err != nil {
-		log.Fatal(err)
+		return err
 	}
 	defer os.RemoveAll(dir)
 
+	// When loading the database with test data, lower the max
+	// file size so as to ensure a few finalized Parquet files are
+	// created and we can query them right away.
+	writer.MaxRecordsPerFile = 10000
 	log.Printf("generating random records")
-	genRandomRecords(dir, 2*writer.MaxRecordsPerFile)
+	genRandomRecords(dir, 10*writer.MaxRecordsPerFile)
 
 	log.Printf("starting benchmark")
-	err = runBench(func(uri string) error {
-
-		f, err := os.Create(*cpuprofile)
-		if err != nil {
-			log.Fatal(err)
+	return runBench(func(uri string) error {
+		if profile {
+			f, err := os.Create(*cpuprofile)
+			if err != nil {
+				return err
+			}
+			pprof.StartCPUProfile(f)
 		}
-		pprof.StartCPUProfile(f)
 
 		if err := loadDir(uri, dir); err != nil {
 			return err
 		}
 
-		// Stop collecting profiles.
-		pprof.StopCPUProfile()
-		runtime.GC()
-		dumpMemProfile("heap", *heapprofile)
-		dumpMemProfile("allocs", *allocprofile)
+		if profile {
+			// Stop collecting profiles.
+			pprof.StopCPUProfile()
+			runtime.GC()
+			dumpMemProfile("heap", *heapprofile)
+			dumpMemProfile("allocs", *allocprofile)
+		}
 
 		// Run validation query.
 		if err := makeQuery(uri); err != nil {
@@ -243,9 +248,6 @@ func main() {
 
 		return nil
 	})
-	if err != nil {
-		log.Fatal(err)
-	}
 }
 
 func dumpMemProfile(kind, filename string) {
@@ -256,3 +258,11 @@ func dumpMemProfile(kind, filename string) {
 	defer f.Close()
 	pprof.Lookup(kind).WriteTo(f, 0)
 }
+
+func main() {
+	flag.Parse()
+
+	if err := runIntegrationTest(true); err != nil {
+		log.Fatal(err)
+	}
+}
diff --git a/integrationtest/integrationtest_test.go b/integrationtest/integrationtest_test.go
new file mode 100644
index 0000000..3501546
--- /dev/null
+++ b/integrationtest/integrationtest_test.go
@@ -0,0 +1,11 @@
+package main
+
+import (
+	"testing"
+)
+
+func TestIntegration(t *testing.T) {
+	if err := runIntegrationTest(false); err != nil {
+		t.Fatal(err)
+	}
+}
-- 
GitLab