Skip to content

Commit 81e31db

Browse files
committed
fix flaky test with global session initializer
1 parent 7e1c4ab commit 81e31db

File tree

1 file changed

+77
-50
lines changed

1 file changed

+77
-50
lines changed

chdb/session_test.go

+77-50
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
11
package chdb
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"testing"
78
)
89

9-
// TestNewSession tests the creation of a new session.
10-
func TestNewSession(t *testing.T) {
11-
session, err := NewSession()
10+
var (
11+
session *Session
12+
)
13+
14+
func globalSetup() error {
15+
sess, err := NewSession()
1216
if err != nil {
13-
t.Fatalf("Failed to create new session: %s", err)
17+
return err
1418
}
15-
defer session.Cleanup()
19+
session = sess
20+
return nil
21+
}
22+
23+
func globalTeardown() {
24+
session.Cleanup()
25+
session.Close()
26+
}
27+
28+
func TestMain(m *testing.M) {
29+
if err := globalSetup(); err != nil {
30+
fmt.Println("Global setup failed:", err)
31+
os.Exit(1)
32+
}
33+
// Run all tests.
34+
exitCode := m.Run()
35+
36+
// Global teardown: clean up any resources here.
37+
globalTeardown()
38+
39+
// Exit with the code returned by m.Run().
40+
os.Exit(exitCode)
41+
}
42+
43+
// TestNewSession tests the creation of a new session.
44+
func TestNewSession(t *testing.T) {
1645

1746
// Check if the session directory exists
1847
if _, err := os.Stat(session.Path()); os.IsNotExist(err) {
@@ -25,59 +54,30 @@ func TestNewSession(t *testing.T) {
2554
}
2655
}
2756

28-
// TestSessionClose tests the Close method of the session.
29-
func TestSessionClose(t *testing.T) {
30-
session, _ := NewSession()
31-
defer session.Cleanup() // Cleanup in case Close fails
57+
// This test is currently flaky because of this: https://github.com/chdb-io/chdb/pull/299/commits/91b0aedd8c17e74a4bb213e885d89cc9a77c99ad
58+
func TestQuery(t *testing.T) {
3259

33-
// Close the session
34-
session.Close()
60+
// time.Sleep(time.Second * 5)
3561

36-
// Check if the session directory has been removed
37-
if _, err := os.Stat(session.Path()); !os.IsNotExist(err) {
38-
t.Errorf("Session directory should be removed after Close: %s", session.Path())
62+
_, err := session.Query("CREATE TABLE IF NOT EXISTS TestQuery (id UInt32) ENGINE = MergeTree() ORDER BY id;")
63+
if err != nil {
64+
t.Fatal(err)
3965
}
40-
}
4166

42-
// TestSessionCleanup tests the Cleanup method of the session.
43-
func TestSessionCleanup(t *testing.T) {
44-
session, _ := NewSession()
45-
46-
// Cleanup the session
47-
session.Cleanup()
67+
_, err = session.Query("INSERT INTO TestQuery VALUES (1), (2), (3);")
68+
if err != nil {
69+
t.Fatal(err)
70+
}
71+
ret, err := session.Query("SELECT * FROM TestQuery;")
72+
if err != nil {
73+
t.Fatalf("Query failed: %s", err)
74+
}
4875

49-
// Check if the session directory has been removed
50-
if _, err := os.Stat(session.Path()); !os.IsNotExist(err) {
51-
t.Errorf("Session directory should be removed after Cleanup: %s", session.Path())
76+
if string(ret.Buf()) != "1\n2\n3\n" {
77+
t.Fatalf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf()))
5278
}
5379
}
5480

55-
// This test is currently flaky because of this: https://github.com/chdb-io/chdb/pull/299/commits/91b0aedd8c17e74a4bb213e885d89cc9a77c99ad
56-
// func TestQuery(t *testing.T) {
57-
58-
// session, _ := NewSession()
59-
// defer session.Cleanup()
60-
// // time.Sleep(time.Second * 5)
61-
62-
// _, err := session.Query("CREATE TABLE IF NOT EXISTS TestQuery (id UInt32) ENGINE = MergeTree() ORDER BY id;")
63-
// if err != nil {
64-
// t.Fatal(err)
65-
// }
66-
67-
// _, err = session.Query("INSERT INTO TestQuery VALUES (1), (2), (3);")
68-
// if err != nil {
69-
// t.Fatal(err)
70-
// }
71-
// ret, err := session.Query("SELECT * FROM TestQuery;")
72-
// if err != nil {
73-
// t.Fatalf("Query failed: %s", err)
74-
// }
75-
76-
// if string(ret.Buf()) != "1\n2\n3\n" {
77-
// t.Fatalf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf()))
78-
// }
79-
// }
80-
8181
func TestSessionPathAndIsTemp(t *testing.T) {
8282
// Create a new session and check its Path and IsTemp
8383
session, _ := NewSession()
@@ -105,3 +105,30 @@ func TestSessionPathAndIsTemp(t *testing.T) {
105105
t.Errorf("Session should not be temporary")
106106
}
107107
}
108+
109+
// TestSessionClose tests the Close method of the session.
110+
func TestSessionClose(t *testing.T) {
111+
session, _ := NewSession()
112+
defer session.Cleanup() // Cleanup in case Close fails
113+
114+
// Close the session
115+
session.Close()
116+
117+
// Check if the session directory has been removed
118+
if _, err := os.Stat(session.Path()); !os.IsNotExist(err) {
119+
t.Errorf("Session directory should be removed after Close: %s", session.Path())
120+
}
121+
}
122+
123+
// TestSessionCleanup tests the Cleanup method of the session.
124+
func TestSessionCleanup(t *testing.T) {
125+
session, _ := NewSession()
126+
127+
// Cleanup the session
128+
session.Cleanup()
129+
130+
// Check if the session directory has been removed
131+
if _, err := os.Stat(session.Path()); !os.IsNotExist(err) {
132+
t.Errorf("Session directory should be removed after Cleanup: %s", session.Path())
133+
}
134+
}

0 commit comments

Comments
 (0)