Skip to content

Commit

Permalink
test: fix flaky crud tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
oleg-jukovec committed May 17, 2023
1 parent a8635cc commit eb7ffce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
45 changes: 36 additions & 9 deletions crud/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions crud/testdata/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit eb7ffce

Please sign in to comment.