From f8446501ce7e1f51bcc41d2bf48afe74b1e5188a Mon Sep 17 00:00:00 2001 From: Guy Arbitman Date: Mon, 16 Jan 2023 05:38:20 +0200 Subject: [PATCH] usm: postgres classification: Reduced 5 seconds per test, 1m30s in total (#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 --- pkg/network/protocols/postgres/server.go | 2 +- pkg/network/protocols/postgres/utils.go | 14 ++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/network/protocols/postgres/server.go b/pkg/network/protocols/postgres/server.go index 21085ab71f8fd..adf2b20b66acf 100644 --- a/pkg/network/protocols/postgres/server.go +++ b/pkg/network/protocols/postgres/server.go @@ -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")) } diff --git a/pkg/network/protocols/postgres/utils.go b/pkg/network/protocols/postgres/utils.go index 2c9da806c10ab..ec189bcb80da6 100644 --- a/pkg/network/protocols/postgres/utils.go +++ b/pkg/network/protocols/postgres/utils.go @@ -9,7 +9,6 @@ import ( "context" "database/sql" "testing" - "time" "github.com/stretchr/testify/require" "github.com/uptrace/bun" @@ -17,7 +16,7 @@ import ( "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"` @@ -25,14 +24,13 @@ type DummyTable struct { 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), @@ -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() @@ -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) }