Skip to content

Commit

Permalink
Ensure test container termination (#1274)
Browse files Browse the repository at this point in the history
* Ensure test container termination

* make #1229 more resilient

* make #1229 more resilent
  • Loading branch information
jkaflik authored Apr 15, 2024
1 parent aa43b3f commit 6fdbf7d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 107 deletions.
14 changes: 6 additions & 8 deletions tests/issues/1127_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import (

func Test1127(t *testing.T) {
var (
conn, err = clickhouse_tests.GetConnection("issues", clickhouse.Settings{
"max_execution_time": 60,
"allow_experimental_object_type": true,
}, nil, &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
})
conn, err = clickhouse_tests.GetConnection("issues", nil, nil, nil)
)
require.NoError(t, err)

Expand All @@ -30,11 +25,14 @@ func Test1127(t *testing.T) {
fmt.Println("log info: ", log)
}))

rows, err := conn.Query(ctx, "select throwIf(number = 1e6) from system.numbers settings max_block_size = 100")
rows, err := conn.Query(ctx, "select number, throwIf(number = 1e6) from system.numbers settings max_block_size = 100")
require.NoError(t, err)

defer rows.Close()

var number uint64
var throwIf uint8
for rows.Next() {
require.NoError(t, rows.Scan(&number, &throwIf))
}

assert.Error(t, rows.Err())
Expand Down
4 changes: 3 additions & 1 deletion tests/issues/1229_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func Test1229(t *testing.T) {
defer wg.Done()
withTimeoutCtx, cancel := context.WithTimeout(ctx, queryTimeout)
defer cancel()
_, _ = conn.Query(withTimeoutCtx, selectQuery)
rows, err := conn.Query(withTimeoutCtx, selectQuery)
require.NoError(t, err)
require.NoError(t, rows.Close())
}()
}

Expand Down
35 changes: 3 additions & 32 deletions tests/issues/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,16 @@
package issues

import (
"context"
"fmt"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"math/rand"
"os"
"strconv"
"testing"
"time"

clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
)

const testSet string = "issues"

func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
fmt.Printf("using random seed %d for %s\n", seed, testSet)
rand.Seed(seed)
useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
}
var env clickhouse_tests.ClickHouseTestEnvironment
switch useDocker {
case true:
env, err = clickhouse_tests.CreateClickHouseTestEnvironment(testSet)
if err != nil {
panic(err)
}
defer env.Container.Terminate(context.Background()) //nolint
case false:
env, err = clickhouse_tests.GetExternalTestEnvironment(testSet)
if err != nil {
panic(err)
}
}
clickhouse_tests.SetTestEnvironment(testSet, env)
if err := clickhouse_tests.CreateDatabase(testSet); err != nil {
panic(err)
}
os.Exit(m.Run())
os.Exit(clickhouse_tests.Runtime(m, testSet))
}

func GetIssuesTestEnvironment() (clickhouse_tests.ClickHouseTestEnvironment, error) {
Expand Down
37 changes: 4 additions & 33 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,18 @@
package tests

import (
"context"
"crypto/tls"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"math/rand"
"os"
"strconv"
"testing"
"time"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
)

const testSet string = "native"

func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
fmt.Printf("using random seed %d for %s tests\n", seed, testSet)
rand.Seed(seed)
useDocker, err := strconv.ParseBool(GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
}
var env ClickHouseTestEnvironment
switch useDocker {
case true:
env, err = CreateClickHouseTestEnvironment(testSet)
if err != nil {
panic(err)
}
defer env.Container.Terminate(context.Background()) //nolint
case false:
env, err = GetExternalTestEnvironment(testSet)
if err != nil {
panic(err)
}
}
SetTestEnvironment(testSet, env)
if err := CreateDatabase(testSet); err != nil {
panic(err)
}
os.Exit(m.Run())
os.Exit(Runtime(m, testSet))
}

func GetNativeTestEnvironment() (ClickHouseTestEnvironment, error) {
Expand Down
37 changes: 4 additions & 33 deletions tests/std/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,20 @@
package std

import (
"context"
"crypto/tls"
"database/sql"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
"math/rand"
"net/url"
"os"
"strconv"
"testing"
"time"

"github.com/ClickHouse/clickhouse-go/v2"
clickhouse_tests "github.com/ClickHouse/clickhouse-go/v2/tests"
)

const testSet string = "std"

func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
fmt.Printf("using random seed %d for %s tests\n", seed, testSet)
rand.Seed(seed)
useDocker, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
}
var env clickhouse_tests.ClickHouseTestEnvironment
switch useDocker {
case true:
env, err = clickhouse_tests.CreateClickHouseTestEnvironment(testSet)
if err != nil {
panic(err)
}
defer env.Container.Terminate(context.Background()) //nolint
case false:
env, err = clickhouse_tests.GetExternalTestEnvironment(testSet)
if err != nil {
panic(err)
}
}
clickhouse_tests.SetTestEnvironment(testSet, env)
if err := clickhouse_tests.CreateDatabase(testSet); err != nil {
panic(err)
}
os.Exit(m.Run())
os.Exit(clickhouse_tests.Runtime(m, testSet))
}

func GetStdDSNConnection(protocol clickhouse.Protocol, secure bool, opts url.Values) (*sql.DB, error) {
Expand Down
37 changes: 37 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,40 @@ func optionsToDSN(o *clickhouse.Options) string {

return u.String()
}

func Runtime(m *testing.M, ts string) (exitCode int) {
seed := time.Now().UnixNano()
rand.Seed(seed)
fmt.Printf("using random seed %d for %s tests\n", seed, ts)

useDocker, err := strconv.ParseBool(GetEnv("CLICKHOUSE_USE_DOCKER", "true"))
if err != nil {
panic(err)
}

var env ClickHouseTestEnvironment
switch useDocker {
case true:
env, err = CreateClickHouseTestEnvironment(ts)
if err != nil {
panic(err)
}
defer func() {
if err := env.Container.Terminate(context.Background()); err != nil {
panic(err)
}
}() //nolint
case false:
env, err = GetExternalTestEnvironment(ts)
if err != nil {
panic(err)
}
}

SetTestEnvironment(ts, env)
if err := CreateDatabase(ts); err != nil {
panic(err)
}

return m.Run()
}

0 comments on commit 6fdbf7d

Please sign in to comment.