Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
art22m committed Jan 17, 2025
1 parent 3143ea4 commit 970b831
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions database/ydb/ydb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package ydb
import (
"context"
"fmt"
"log"
"strings"
"testing"
"time"

"github.com/dhui/dktest"
"github.com/docker/go-connections/nat"
"github.com/ydb-platform/ydb-go-sdk/v3"

"github.com/golang-migrate/migrate/v4"
Expand All @@ -18,8 +18,7 @@ import (
)

const (
host = "localhost"
port = "2136"
defaultPort = 2136
databaseName = "local"
)

Expand All @@ -31,16 +30,7 @@ var (
"MON_PORT": "8765",
},

PortBindings: nat.PortMap{
nat.Port("2136/tcp"): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: port,
},
},
},

Hostname: host,
PortRequired: true,
ReadyTimeout: 15 * time.Second,
ReadyFunc: isReady,
}
Expand All @@ -53,12 +43,18 @@ var (
}
)

func connectionString(options ...string) string {
func connectionString(host, port string, options ...string) string {
return fmt.Sprintf("ydb://%s:%s/%s?%s", host, port, databaseName, strings.Join(options, "&"))
}

func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
d, err := ydb.Open(ctx, fmt.Sprintf("grpc://%s:%s/%s", host, port, databaseName))
ip, port, err := c.Port(defaultPort)
if err != nil {
log.Println("port error:", err)
return false
}

d, err := ydb.Open(ctx, fmt.Sprintf("grpc://%s:%s/%s", ip, port, databaseName))
if err != nil {
return false
}
Expand All @@ -75,8 +71,13 @@ func isReady(ctx context.Context, c dktest.ContainerInfo) bool {

func Test(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(defaultPort)
if err != nil {
t.Fatal(err)
}

db := &YDB{}
d, err := db.Open(connectionString())
d, err := db.Open(connectionString(ip, port))
if err != nil {
t.Fatal(err)
}
Expand All @@ -94,8 +95,13 @@ func Test(t *testing.T) {

func TestMigrate(t *testing.T) {
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) {
ip, port, err := c.Port(defaultPort)
if err != nil {
t.Fatal(err)
}

db := &YDB{}
d, err := db.Open(connectionString())
d, err := db.Open(connectionString(ip, port))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 970b831

Please sign in to comment.