Add concurrent query test
@@ -658,6 +658,45 @@ func Test_WriteOnQueryInMemDatabase(t *testing.T) {
}
}
+func Test_ConcurrentQueriesInMemory(t *testing.T) {
+ db := mustCreateInMemoryDatabase()
+ defer db.Close()
+
+ r, err := db.ExecuteStringStmt(`CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)`)
+ if err != nil {
+ t.Fatalf("failed to create table: %s", err.Error())
+ }
+ if exp, got := `[{}]`, asJSON(r); exp != got {
+ t.Fatalf("unexpected results for query\nexp: %s\ngot: %s", exp, got)
+ }
+
+ for i := 0; i < 5000; i++ {
+ r, err = db.ExecuteStringStmt(`INSERT INTO foo(name) VALUES("fiona")`)
+ if err != nil {
+ t.Fatalf("failed to insert record: %s", err.Error())
+ }
+ if exp, got := fmt.Sprintf(`[{"last_insert_id":%d,"rows_affected":1}]`, i+1), asJSON(r); exp != got {
+ t.Fatalf("unexpected results for query\nexp: %s\ngot: %s", exp, got)
+ }
+ }
+
+ var wg sync.WaitGroup
+ for i := 0; i < 32; i++ {
+ go func() {
+ wg.Add(1)
+ defer wg.Done()
+ ro, err := db.QueryStringStmt(`SELECT COUNT(*) FROM foo`)
+ if err != nil {
+ t.Fatalf("failed to query table: %s", err.Error())
+ }
+ if exp, got := `[{"columns":["COUNT(*)"],"types":[""],"values":[[5000]]}]`, asJSON(ro); exp != got {
+ t.Fatalf("unexpected results for query\nexp: %s\ngot: %s", exp, got)
+ }
+ }()
+ }
+ wg.Wait()
+}
+
func Test_SimpleParameterizedStatements(t *testing.T) {
db, path := mustCreateDatabase()
defer db.Close()
