@@ -19,6 +19,7 @@ package tests
1919
2020import (
2121 "testing"
22+ "time"
2223
2324 "github.com/ClickHouse/clickhouse-go/v2"
2425 "github.com/stretchr/testify/require"
@@ -51,3 +52,73 @@ func TestBatchContextCancellation(t *testing.T) {
5152 // assert if connection is properly released after context cancellation
5253 require .NoError (t , conn .Exec (context .Background (), "SELECT 1" ))
5354}
55+
56+ func TestBatchCloseConnectionReleased (t * testing.T ) {
57+ te , err := GetTestEnvironment (testSet )
58+ require .NoError (t , err )
59+ opts := ClientOptionsFromEnv (te , clickhouse.Settings {}, false )
60+ opts .MaxOpenConns = 1
61+ conn , err := GetConnectionWithOptions (& opts )
62+ require .NoError (t , err )
63+
64+ b , err := conn .PrepareBatch (context .Background (), "INSERT INTO function null('x UInt64')" )
65+ require .NoError (t , err )
66+ for i := 0 ; i < 100 ; i ++ {
67+ require .NoError (t , b .Append (i ))
68+ }
69+
70+ err = b .Close ()
71+ require .NoError (t , err )
72+
73+ // assert if connection is properly released after close called
74+ require .NoError (t , conn .Exec (context .Background (), "SELECT 1" ))
75+ }
76+
77+ func TestBatchSendConnectionReleased (t * testing.T ) {
78+ te , err := GetTestEnvironment (testSet )
79+ require .NoError (t , err )
80+ opts := ClientOptionsFromEnv (te , clickhouse.Settings {}, false )
81+ opts .MaxOpenConns = 1
82+ conn , err := GetConnectionWithOptions (& opts )
83+ require .NoError (t , err )
84+
85+ b , err := conn .PrepareBatch (context .Background (), "INSERT INTO function null('x UInt64')" )
86+ require .NoError (t , err )
87+ for i := 0 ; i < 100 ; i ++ {
88+ require .NoError (t , b .Append (i ))
89+ }
90+
91+ err = b .Send ()
92+ require .NoError (t , err )
93+
94+ // Close should be deferred after the batch is opened
95+ // Validate that it can be called after Send
96+ err = b .Close ()
97+ require .NoError (t , err )
98+
99+ // assert if connection is properly released after Send called
100+ require .NoError (t , conn .Exec (context .Background (), "SELECT 1" ))
101+ }
102+
103+ // This test validates that connections are blocked if a batch is not properly
104+ // cleaned up. This isn't required behavior, but this test confirms it happens.
105+ func TestBatchCloseConnectionHold (t * testing.T ) {
106+ te , err := GetTestEnvironment (testSet )
107+ require .NoError (t , err )
108+ opts := ClientOptionsFromEnv (te , clickhouse.Settings {}, false )
109+ opts .MaxOpenConns = 1
110+ opts .DialTimeout = 2 * time .Second // Lower timeout for faster acquire error
111+ conn , err := GetConnectionWithOptions (& opts )
112+ require .NoError (t , err )
113+
114+ b , err := conn .PrepareBatch (context .Background (), "INSERT INTO function null('x UInt64')" )
115+ require .NoError (t , err )
116+ for i := 0 ; i < 100 ; i ++ {
117+ require .NoError (t , b .Append (i ))
118+ }
119+
120+ // batch.Close() should be called here
121+
122+ // assert if connection is blocked if close is not called.
123+ require .ErrorIs (t , conn .Exec (context .Background (), "SELECT 1" ), clickhouse .ErrAcquireConnTimeout )
124+ }
0 commit comments