From eb7ffce38cb156964b788fadaf6bed88ca3222ee Mon Sep 17 00:00:00 2001 From: Oleg Jukovec Date: Sun, 14 May 2023 01:02:21 +0300 Subject: [PATCH] test: fix flaky crud tests An instance was listening on a testing port until the configuration was complete. At the end of the configuration, the port was reopened. As a result, we saw connection loss in tests. Closes #288 --- crud/tarantool_test.go | 45 ++++++++++++++++++++++++++++++++-------- crud/testdata/config.lua | 16 ++++++++++---- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/crud/tarantool_test.go b/crud/tarantool_test.go index 71f7d62b1..4d7e7cd0e 100644 --- a/crud/tarantool_test.go +++ b/crud/tarantool_test.go @@ -104,6 +104,33 @@ var object = crud.MapObject{ "name": "bla", } +func connect(t testing.TB) *tarantool.Connection { + for i := 0; i < 10; i++ { + conn, err := tarantool.Connect(server, opts) + if err != nil { + t.Fatalf("Failed to connect: %s", err) + } + + ret := struct { + _msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused + Result bool + }{} + err = conn.Do(tarantool.NewCall17Request("is_ready")).GetTyped(&ret) + if err != nil { + t.Fatalf("Failed to check is_ready: %s", err) + } + + if ret.Result { + return conn + } + + time.Sleep(time.Second) + } + + t.Fatalf("Failed to wait for a ready state connect.") + return nil +} + var testProcessDataCases = []struct { name string expectedRespLen int @@ -454,7 +481,7 @@ func testCrudRequestCheck(t *testing.T, req tarantool.Request, } func TestCrudGenerateData(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() for _, testCase := range testGenerateDataCases { @@ -477,7 +504,7 @@ func TestCrudGenerateData(t *testing.T) { } func TestCrudProcessData(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() for _, testCase := range testProcessDataCases { @@ -527,7 +554,7 @@ func TestUnflattenRows(t *testing.T) { tpls []interface{} ) - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() // Do `replace`. @@ -586,7 +613,7 @@ func TestUnflattenRows(t *testing.T) { } func TestResultWithErr(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() for _, testCase := range testResultWithErrCases { @@ -601,7 +628,7 @@ func TestResultWithErr(t *testing.T) { } func TestBoolResult(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() req := crud.MakeTruncateRequest(spaceName).Opts(baseOpts) @@ -624,7 +651,7 @@ func TestBoolResult(t *testing.T) { } func TestNumberResult(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() req := crud.MakeCountRequest(spaceName).Opts(countOpts) @@ -665,7 +692,7 @@ func TestBaseResult(t *testing.T) { }, } - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() req := crud.MakeSelectRequest(spaceName).Opts(selectOpts) @@ -708,7 +735,7 @@ func TestManyResult(t *testing.T) { }, } - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() req := crud.MakeReplaceManyRequest(spaceName).Tuples(tuples).Opts(opManyOpts) @@ -733,7 +760,7 @@ func TestManyResult(t *testing.T) { } func TestStorageInfoResult(t *testing.T) { - conn := test_helpers.ConnectWithValidation(t, server, opts) + conn := connect(t) defer conn.Close() req := crud.MakeStorageInfoRequest().Opts(baseOpts) diff --git a/crud/testdata/config.lua b/crud/testdata/config.lua index 9f8b2d5db..4f4db077f 100644 --- a/crud/testdata/config.lua +++ b/crud/testdata/config.lua @@ -59,6 +59,16 @@ s:create_index('bucket_id', { unique = false, }) +local function is_ready_false() + return false +end + +local function is_ready_true() + return true +end + +rawset(_G, 'is_ready', is_ready_false) + -- Setup vshard. _G.vshard = vshard box.once('guest', function() @@ -93,7 +103,5 @@ box.schema.user.grant('test', 'execute', 'universe', nil, { if_not_exists = true box.schema.user.grant('test', 'create,read,write,drop,alter', 'space', nil, { if_not_exists = true }) box.schema.user.grant('test', 'create', 'sequence', nil, { if_not_exists = true }) --- Set listen only when every other thing is configured. -box.cfg{ - listen = os.getenv("TEST_TNT_LISTEN"), -} +-- Set is_ready = is_ready_true only when every other thing is configured. +rawset(_G, 'is_ready', is_ready_true)