Skip to content

Commit 44917a0

Browse files
elhimovoleg-jukovec
authored andcommitted
test: fix flaky test
Helper method that performs initial assigning of master/replica roles and is widely used in ConnectionPool tests was adjusted to wait for the roles to be applied successfully. Prior to this patch it doesn't, so sometimes subsequent test code might work unexpectedly (the problem was caught with TestConnectionHandlerOpenUpdateClose) Closes #452
1 parent 325040d commit 44917a0

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

test_helpers/pool_helper.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,50 @@ func SetInstanceRO(ctx context.Context, dialer tarantool.Dialer, connOpts tarant
212212
return err
213213
}
214214

215+
checkRole := func(conn *tarantool.Connection, isReplica bool) string {
216+
data, err := conn.Do(tarantool.NewCallRequest("box.info")).Get()
217+
switch {
218+
case err != nil:
219+
return fmt.Sprintf("failed to get box.info: %s", err)
220+
case len(data) < 1:
221+
return "box.info is empty"
222+
}
223+
224+
boxInfo, ok := data[0].(map[interface{}]interface{})
225+
if !ok {
226+
return "unexpected type in box.info response"
227+
}
228+
229+
status, statusFound := boxInfo["status"]
230+
readonly, readonlyFound := boxInfo["ro"]
231+
switch {
232+
case !statusFound:
233+
return "box.info.status is missing"
234+
case status != "running":
235+
return fmt.Sprintf("box.info.status='%s' (waiting for 'running')", status)
236+
case !readonlyFound:
237+
return "box.info.ro is missing"
238+
case readonly != isReplica:
239+
return fmt.Sprintf("box.info.ro='%v' (waiting for '%v')", readonly, isReplica)
240+
default:
241+
return ""
242+
}
243+
}
244+
245+
problem := "not checked yet"
246+
247+
// Wait for the role to be applied.
248+
for len(problem) != 0 {
249+
select {
250+
case <-time.After(10 * time.Millisecond):
251+
case <-ctx.Done():
252+
return fmt.Errorf("%w: failed to apply role, the last problem: %s",
253+
ctx.Err(), problem)
254+
}
255+
256+
problem = checkRole(conn, isReplica)
257+
}
258+
215259
return nil
216260
}
217261

0 commit comments

Comments
 (0)