Skip to content

Commit 5d29e17

Browse files
elhimovoleg-jukovec
authored andcommitted
test: handle multiple instances in parallel
1 parent 20dc742 commit 5d29e17

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

test_helpers/pool_helper.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package test_helpers
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"reflect"
8+
"sync"
79
"time"
810

911
"github.com/tarantool/go-tarantool/v2"
@@ -179,16 +181,22 @@ func InsertOnInstances(
179181
return fmt.Errorf("fail to set roles for cluster: %s", err.Error())
180182
}
181183

182-
for _, dialer := range dialers {
183-
ctx, cancel := GetConnectContext()
184-
err := InsertOnInstance(ctx, dialer, connOpts, space, tuple)
185-
cancel()
186-
if err != nil {
187-
return err
188-
}
184+
ctx, cancel := GetConnectContext()
185+
defer cancel()
186+
187+
errs := make([]error, len(dialers))
188+
var wg sync.WaitGroup
189+
wg.Add(len(dialers))
190+
for i, dialer := range dialers {
191+
// Pass loop variable(s) to avoid its capturing by reference (not needed since Go 1.22).
192+
go func(i int, dialer tarantool.Dialer) {
193+
defer wg.Done()
194+
errs[i] = InsertOnInstance(ctx, dialer, connOpts, space, tuple)
195+
}(i, dialer)
189196
}
197+
wg.Wait()
190198

191-
return nil
199+
return errors.Join(errs...)
192200
}
193201

194202
func SetInstanceRO(ctx context.Context, dialer tarantool.Dialer, connOpts tarantool.Opts,
@@ -215,16 +223,23 @@ func SetClusterRO(dialers []tarantool.Dialer, connOpts tarantool.Opts,
215223
return fmt.Errorf("number of servers should be equal to number of roles")
216224
}
217225

226+
ctx, cancel := GetConnectContext()
227+
defer cancel()
228+
229+
// Apply roles in parallel.
230+
errs := make([]error, len(dialers))
231+
var wg sync.WaitGroup
232+
wg.Add(len(dialers))
218233
for i, dialer := range dialers {
219-
ctx, cancel := GetConnectContext()
220-
err := SetInstanceRO(ctx, dialer, connOpts, roles[i])
221-
cancel()
222-
if err != nil {
223-
return err
224-
}
234+
// Pass loop variable(s) to avoid its capturing by reference (not needed since Go 1.22).
235+
go func(i int, dialer tarantool.Dialer) {
236+
defer wg.Done()
237+
errs[i] = SetInstanceRO(ctx, dialer, connOpts, roles[i])
238+
}(i, dialer)
225239
}
240+
wg.Wait()
226241

227-
return nil
242+
return errors.Join(errs...)
228243
}
229244

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

0 commit comments

Comments
 (0)