Skip to content

Commit e7c2e9c

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 0648623 commit e7c2e9c

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

.github/workflows/testing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ jobs:
273273
cd "${SRCDIR}"
274274
make deps
275275
276+
- name: Setup tmate session
277+
uses: mxschmitt/action-tmate@v3
278+
276279
- name: Run regression tests
277280
run: |
278281
cd "${SRCDIR}"

test_helpers/pool_helper.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"sync"
78
"time"
89

910
"github.com/tarantool/go-tarantool/v2"
@@ -195,28 +196,84 @@ func SetInstanceRO(ctx context.Context, instance pool.Instance, isReplica bool)
195196

196197
req := tarantool.NewCallRequest("box.cfg").
197198
Args([]interface{}{map[string]bool{"read_only": isReplica}})
198-
if _, err := conn.Do(req).Get(); err != nil {
199-
return err
199+
if resp, err := conn.Do(req).Get(); err != nil {
200+
// return err
201+
return fmt.Errorf("%w: resp: %v", err, resp)
200202
}
201203

202204
return nil
203205
}
204206

207+
type initPoolHandler struct {
208+
roles map[string]pool.Role
209+
rolesMutex sync.Mutex
210+
}
211+
212+
func (h *initPoolHandler) Discovered(name string, conn *tarantool.Connection,
213+
role pool.Role) error {
214+
h.rolesMutex.Lock()
215+
h.roles[name] = role
216+
h.rolesMutex.Unlock()
217+
return nil
218+
}
219+
220+
func (h *initPoolHandler) Deactivated(name string, conn *tarantool.Connection,
221+
role pool.Role) error {
222+
h.rolesMutex.Lock()
223+
delete(h.roles, name)
224+
h.rolesMutex.Unlock()
225+
return nil
226+
}
227+
205228
func SetClusterRO(instances []pool.Instance, roles []bool) error {
206229
if len(instances) != len(roles) {
207230
return fmt.Errorf("number of instances should be equal to number of roles")
208231
}
209232

233+
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
234+
defer cancel()
235+
210236
for i, instance := range instances {
211-
ctx, cancel := GetConnectContext()
212237
err := SetInstanceRO(ctx, instance, roles[i])
213238
cancel()
214239
if err != nil {
215240
return err
216241
}
217242
}
218243

219-
return nil
244+
// if ctx.Done() {
245+
// return nil, ctx.Err()
246+
// }
247+
248+
h := &initPoolHandler{}
249+
initPoolOpts := pool.Opts{
250+
CheckTimeout: 100 * time.Microsecond,
251+
ConnectionHandler: h,
252+
}
253+
pool, err := pool.ConnectWithOpts(ctx, instances, initPoolOpts)
254+
if err != nil {
255+
return err
256+
}
257+
defer pool.Close()
258+
259+
// Prepare roles map that
260+
expectedRoles := make(map[string]bool, len(roles))
261+
for i, role := range roles {
262+
expectedRoles[instances[i].Name] = role
263+
}
264+
265+
// Wait for the roles to be applied.
266+
for i := 0; i < 100; i++ {
267+
h.rolesMutex.Lock()
268+
if reflect.DeepEqual(h.roles, expectedRoles) {
269+
h.rolesMutex.Unlock()
270+
return nil
271+
}
272+
h.rolesMutex.Unlock()
273+
274+
time.Sleep(100 * time.Millisecond)
275+
}
276+
return fmt.Errorf("Failed to wait roles are applied")
220277
}
221278

222279
func StartTarantoolInstances(instsOpts []StartOpts) ([]*TarantoolInstance, error) {

0 commit comments

Comments
 (0)