Skip to content

Commit

Permalink
usm: postgres classification: Reduced 5 seconds per test, 1m30s in to…
Browse files Browse the repository at this point in the history
…tal (#15070)

Improved the regex for which we are using to detect if the server is up and running, by that
we can spare the 'wait 5 seconds' in GetPGHandle
  • Loading branch information
guyarb authored Jan 16, 2023
1 parent 227069d commit f844650
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/network/protocols/postgres/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func RunPostgresServer(t *testing.T, serverAddr string, serverPort string) {

dir, _ := testutil.CurDir()

protocolsUtils.RunDockerServer(t, "postgres", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(".*database system is ready to accept connections"))
protocolsUtils.RunDockerServer(t, "postgres", dir+"/testdata/docker-compose.yml", env, regexp.MustCompile(".*\\[1].*database system is ready to accept connections"))
}
14 changes: 6 additions & 8 deletions pkg/network/protocols/postgres/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ import (
"context"
"database/sql"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)

// Define a table schema, so that Bun can generates queries for it.
// DummyTable defines a table schema, so that `bun` module can generate queries for it.
type DummyTable struct {
bun.BaseModel `bun:"table:dummy,alias:d"`

ID int64 `bun:",pk,autoincrement"`
Foo string
}

var dummyModel *DummyTable = &DummyTable{ID: 1, Foo: "bar"}
var dummyModel = &DummyTable{ID: 1, Foo: "bar"}

// GetPGHandle returns a handle on the test Postgres DB. This does not initiate
// a connection
func GetPGHandle(t *testing.T, serverAddr string) *sql.DB {
t.Helper()

time.Sleep(5 * time.Second)
pg := sql.OpenDB(pgdriver.NewConnector(
pgdriver.WithNetwork("tcp"),
pgdriver.WithAddr(serverAddr),
Expand All @@ -46,7 +44,7 @@ func GetPGHandle(t *testing.T, serverAddr string) *sql.DB {
}

// ConnectAndGetDB initiates a connection to the database, get a handle on the
// test db, and register cleanup handlers for the test. Finally it saves the db
// test db, and register cleanup handlers for the test. Finally, it saves the db
// handle and task context in the provided extras map.
func ConnectAndGetDB(t *testing.T, serverAddr string, extras map[string]interface{}) {
t.Helper()
Expand Down Expand Up @@ -132,9 +130,9 @@ func RunUpdateQuery(t *testing.T, extras map[string]interface{}) {
t.Helper()
db, ctx := getCtx(extras)

new := *dummyModel
new.Foo = "baz"
newModel := *dummyModel
newModel.Foo = "baz"

_, err := db.NewUpdate().Model(&new).WherePK().Exec(ctx)
_, err := db.NewUpdate().Model(&newModel).WherePK().Exec(ctx)
require.NoError(t, err)
}

0 comments on commit f844650

Please sign in to comment.