Skip to content

Commit 92cbe14

Browse files
committed
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 803ceed commit 92cbe14

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
@@ -214,6 +214,50 @@ func SetInstanceRO(ctx context.Context, dialer tarantool.Dialer, connOpts tarant
214214
return err
215215
}
216216

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

0 commit comments

Comments
 (0)