Skip to content

Commit

Permalink
Reduce flakiness in ClickHouse tests (#1902)
Browse files Browse the repository at this point in the history
* Add retry

Add retry using performCRUDWithRetry.

* Merge performCRUD and performCRUDWithRetry

* Update retry

Update retry to use backoff package.

* Run linter
  • Loading branch information
rafiramadhana authored Nov 13, 2023
1 parent 83bc893 commit ea35ff1
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions modules/clickhouse/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

ch "github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/cenkalti/backoff/v4"
"github.com/stretchr/testify/assert"

"github.com/testcontainers/testcontainers-go"
Expand Down Expand Up @@ -218,22 +219,31 @@ func TestClickHouseWithConfigFile(t *testing.T) {
}

func performCRUD(conn driver.Conn) ([]Test, error) {
err := conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;")
if err != nil {
return nil, err
}
var (
err error
rows []Test
)

err = conn.Exec(context.Background(), "INSERT INTO test_table (id) VALUES (1);")
if err != nil {
return nil, err
}
err = backoff.Retry(func() error {
err = conn.Exec(context.Background(), "create table if not exists test_table (id UInt64) engine = MergeTree PRIMARY KEY (id) ORDER BY (id) SETTINGS index_granularity = 8192;")
if err != nil {
return err
}

rows, err := getAllRows(conn)
if err != nil {
return nil, err
}
err = conn.Exec(context.Background(), "INSERT INTO test_table (id) VALUES (1);")
if err != nil {
return err
}

rows, err = getAllRows(conn)
if err != nil {
return err
}

return nil
}, backoff.NewExponentialBackOff())

return rows, nil
return rows, err
}

func getAllRows(conn driver.Conn) ([]Test, error) {
Expand Down

0 comments on commit ea35ff1

Please sign in to comment.