func ForEachTarget()

in plugins/teststeps/teststeps.go [32:71]


func ForEachTarget(pluginName string, ctx xcontext.Context, ch test.TestStepChannels, f PerTargetFunc) (json.RawMessage, error) {
	reportTarget := func(t *target.Target, err error) {
		if err != nil {
			ctx.Errorf("%s: ForEachTarget: failed to apply test step function on target %s: %v", pluginName, t, err)
		} else {
			ctx.Debugf("%s: ForEachTarget: target %s completed successfully", pluginName, t)
		}
		select {
		case ch.Out <- test.TestStepResult{Target: t, Err: err}:
		case <-ctx.Done():
			ctx.Debugf("%s: ForEachTarget: received cancellation signal while reporting result", pluginName)
		}
	}

	var wg sync.WaitGroup
	func() {
		for {
			select {
			case tgt, ok := <-ch.In:
				if !ok {
					ctx.Debugf("%s: ForEachTarget: all targets have been received", pluginName)
					return
				}
				ctx.Debugf("%s: ForEachTarget: received target %s", pluginName, tgt)
				wg.Add(1)
				go func() {
					defer wg.Done()

					err := f(ctx, tgt)
					reportTarget(tgt, err)
				}()
			case <-ctx.Done():
				ctx.Debugf("%s: ForEachTarget: incoming loop canceled", pluginName)
				return
			}
		}
	}()
	wg.Wait()
	return nil, nil
}