Skip to content

Commit bd29b39

Browse files
committed
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
1 parent 75bff3f commit bd29b39

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

crud/tarantool_test.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ var object = crud.MapObject{
104104
"name": "bla",
105105
}
106106

107+
func connect(t testing.TB) *tarantool.Connection {
108+
for i := 0; i < 10; i++ {
109+
conn, err := tarantool.Connect(server, opts)
110+
if err != nil {
111+
t.Fatalf("Failed to connect: %s", err)
112+
}
113+
114+
ret := struct {
115+
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
116+
Result bool
117+
}{}
118+
err = conn.Do(tarantool.NewCall17Request("is_ready")).GetTyped(&ret)
119+
if err != nil {
120+
t.Fatalf("Failed to check is_ready: %s", err)
121+
}
122+
123+
if ret.Result {
124+
return conn
125+
}
126+
127+
time.Sleep(time.Second)
128+
}
129+
130+
t.Fatalf("Failed to wait for a ready state connect.")
131+
return nil
132+
}
133+
107134
var testProcessDataCases = []struct {
108135
name string
109136
expectedRespLen int
@@ -454,7 +481,7 @@ func testCrudRequestCheck(t *testing.T, req tarantool.Request,
454481
}
455482

456483
func TestCrudGenerateData(t *testing.T) {
457-
conn := test_helpers.ConnectWithValidation(t, server, opts)
484+
conn := connect(t)
458485
defer conn.Close()
459486

460487
for _, testCase := range testGenerateDataCases {
@@ -477,7 +504,7 @@ func TestCrudGenerateData(t *testing.T) {
477504
}
478505

479506
func TestCrudProcessData(t *testing.T) {
480-
conn := test_helpers.ConnectWithValidation(t, server, opts)
507+
conn := connect(t)
481508
defer conn.Close()
482509

483510
for _, testCase := range testProcessDataCases {
@@ -527,7 +554,7 @@ func TestUnflattenRows(t *testing.T) {
527554
tpls []interface{}
528555
)
529556

530-
conn := test_helpers.ConnectWithValidation(t, server, opts)
557+
conn := connect(t)
531558
defer conn.Close()
532559

533560
// Do `replace`.
@@ -586,7 +613,7 @@ func TestUnflattenRows(t *testing.T) {
586613
}
587614

588615
func TestResultWithErr(t *testing.T) {
589-
conn := test_helpers.ConnectWithValidation(t, server, opts)
616+
conn := connect(t)
590617
defer conn.Close()
591618

592619
for _, testCase := range testResultWithErrCases {
@@ -601,7 +628,7 @@ func TestResultWithErr(t *testing.T) {
601628
}
602629

603630
func TestBoolResult(t *testing.T) {
604-
conn := test_helpers.ConnectWithValidation(t, server, opts)
631+
conn := connect(t)
605632
defer conn.Close()
606633

607634
req := crud.MakeTruncateRequest(spaceName).Opts(baseOpts)
@@ -624,7 +651,7 @@ func TestBoolResult(t *testing.T) {
624651
}
625652

626653
func TestNumberResult(t *testing.T) {
627-
conn := test_helpers.ConnectWithValidation(t, server, opts)
654+
conn := connect(t)
628655
defer conn.Close()
629656

630657
req := crud.MakeCountRequest(spaceName).Opts(countOpts)
@@ -665,7 +692,7 @@ func TestBaseResult(t *testing.T) {
665692
},
666693
}
667694

668-
conn := test_helpers.ConnectWithValidation(t, server, opts)
695+
conn := connect(t)
669696
defer conn.Close()
670697

671698
req := crud.MakeSelectRequest(spaceName).Opts(selectOpts)
@@ -708,7 +735,7 @@ func TestManyResult(t *testing.T) {
708735
},
709736
}
710737

711-
conn := test_helpers.ConnectWithValidation(t, server, opts)
738+
conn := connect(t)
712739
defer conn.Close()
713740

714741
req := crud.MakeReplaceManyRequest(spaceName).Tuples(tuples).Opts(opManyOpts)
@@ -733,7 +760,7 @@ func TestManyResult(t *testing.T) {
733760
}
734761

735762
func TestStorageInfoResult(t *testing.T) {
736-
conn := test_helpers.ConnectWithValidation(t, server, opts)
763+
conn := connect(t)
737764
defer conn.Close()
738765

739766
req := crud.MakeStorageInfoRequest().Opts(baseOpts)

crud/testdata/config.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ s:create_index('bucket_id', {
5959
unique = false,
6060
})
6161

62+
local function is_ready_false()
63+
return false
64+
end
65+
66+
local function is_ready_true()
67+
return true
68+
end
69+
70+
rawset(_G, 'is_ready', is_ready_false)
71+
6272
-- Setup vshard.
6373
_G.vshard = vshard
6474
box.once('guest', function()
@@ -93,7 +103,5 @@ box.schema.user.grant('test', 'execute', 'universe', nil, { if_not_exists = true
93103
box.schema.user.grant('test', 'create,read,write,drop,alter', 'space', nil, { if_not_exists = true })
94104
box.schema.user.grant('test', 'create', 'sequence', nil, { if_not_exists = true })
95105

96-
-- Set listen only when every other thing is configured.
97-
box.cfg{
98-
listen = os.getenv("TEST_TNT_LISTEN"),
99-
}
106+
-- Set is_ready = is_ready_true only when every other thing is configured.
107+
rawset(_G, 'is_ready', is_ready_true)

0 commit comments

Comments
 (0)