You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/08/18 13:11:22 UTC

[skywalking-infra-e2e] branch main updated: support verify cases concurrently. (#82)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-infra-e2e.git


The following commit(s) were added to refs/heads/main by this push:
     new d71bbba  support verify cases concurrently. (#82)
d71bbba is described below

commit d71bbba95f95d6629db199e4bc0c0e097ae4f2dc
Author: Miku~forever <92...@users.noreply.github.com>
AuthorDate: Thu Aug 18 21:11:18 2022 +0800

    support verify cases concurrently. (#82)
    
    Co-authored-by: Hoshea Jiang <fg...@gmail.com>
---
 commands/verify/verify.go           | 153 ++++++++++++++++++++++++++++++------
 docs/en/setup/Configuration-File.md |   1 +
 examples/compose/e2e.yaml           |   2 +
 internal/config/e2eConfig.go        |   1 +
 4 files changed, 135 insertions(+), 22 deletions(-)

diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index de78966..775b358 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -20,6 +20,7 @@ package verify
 import (
 	"errors"
 	"fmt"
+	"sync"
 	"time"
 
 	"github.com/apache/skywalking-infra-e2e/internal/components/verifier"
@@ -87,6 +88,85 @@ func verifySingleCase(expectedFile, actualFile, query string) error {
 	return nil
 }
 
+// concurrentErrors store errors that occurred when verifying cases in goroutines.
+type concurrentErrors struct {
+	errs  *multierror.Error
+	mutex sync.Mutex
+}
+
+// verifyInfo contains necessary information about verification
+type verifyInfo struct {
+	retryCount int
+	interval   time.Duration
+	failFast   bool
+}
+
+func concurrentSafeErrAppend(concurrentError *concurrentErrors, err error) {
+	concurrentError.mutex.Lock()
+	concurrentError.errs = multierror.Append(concurrentError.errs, err)
+	concurrentError.mutex.Unlock()
+}
+
+func check(stopChan chan bool, goroutineNum int) bool {
+	count := 0
+	for shouldExit := range stopChan {
+		if shouldExit {
+			return true
+		}
+
+		count++
+
+		if count == goroutineNum {
+			return false
+		}
+	}
+	return false
+}
+
+func concurrentVerifySingleCase(idx int, v config.VerifyCase, errs *concurrentErrors, verify verifyInfo, wg *sync.WaitGroup, stopChan chan bool) {
+	var err error
+
+	defer func() {
+		if verify.failFast {
+			if err != nil {
+				concurrentSafeErrAppend(errs, err)
+				stopChan <- true
+			} else {
+				stopChan <- false
+			}
+		} else {
+			if err != nil {
+				concurrentSafeErrAppend(errs, err)
+			}
+		}
+		wg.Done()
+	}()
+
+	if v.GetExpected() == "" {
+		errMsg := fmt.Sprintf("the expected data file for case[%v] is not specified\n", idx)
+		logger.Log.Warnf(errMsg)
+		err = errors.New(errMsg)
+		return
+	}
+
+	for current := 1; current <= verify.retryCount; current++ {
+		if err = verifySingleCase(v.GetExpected(), v.GetActual(), v.Query); err == nil {
+			break
+		} else if current != verify.retryCount {
+			logger.Log.Warnf("verify case[%v] failure, will continue retry, %v", idx, err)
+			time.Sleep(verify.interval)
+		}
+	}
+}
+
+func checkForRetryCount(retryCount int) int {
+	if retryCount <= 0 {
+		return 1
+	}
+
+	return retryCount
+}
+
 // DoVerifyAccordingConfig reads cases from the config file and verifies them.
 func DoVerifyAccordingConfig() error {
 	if config.GlobalConfig.Error != nil {
@@ -96,44 +176,73 @@ func DoVerifyAccordingConfig() error {
 	e2eConfig := config.GlobalConfig.E2EConfig
 
 	retryCount := e2eConfig.Verify.RetryStrategy.Count
-	if retryCount <= 0 {
-		retryCount = 1
-	}
+	retryCount = checkForRetryCount(retryCount)
+
 	interval, err := parseInterval(e2eConfig.Verify.RetryStrategy.Interval)
 	if err != nil {
 		return err
 	}
 
 	failFast := e2eConfig.Verify.FailFast
+	concurrency := e2eConfig.Verify.Concurrency
 
-	var errCollection *multierror.Error
-	for idx, v := range e2eConfig.Verify.Cases {
-		if v.GetExpected() == "" {
-			errMsg := fmt.Sprintf("the expected data file for case[%v] is not specified\n", idx)
-			if failFast {
-				return errors.New(errMsg)
+	errs := &multierror.Error{}
+
+	if concurrency {
+		var waitGroup sync.WaitGroup
+		ConcurrentErrors := concurrentErrors{
+			errs: errs,
+		}
+
+		VerifyInfo := verifyInfo{
+			retryCount,
+			interval,
+			failFast,
+		}
+		stopChan := make(chan bool)
+		goroutineNum := len(e2eConfig.Verify.Cases)
+		waitGroup.Add(goroutineNum)
+
+		for idx, v := range e2eConfig.Verify.Cases {
+			go concurrentVerifySingleCase(idx, v, &ConcurrentErrors, VerifyInfo, &waitGroup, stopChan)
+		}
+
+		if failFast {
+			if shouldExit := check(stopChan, goroutineNum); shouldExit {
+				return errs.ErrorOrNil()
 			}
-			logger.Log.Warnf(errMsg)
-			errCollection = multierror.Append(errCollection, errors.New(errMsg))
-			continue
 		}
 
-		for current := 1; current <= retryCount; current++ {
-			if err := verifySingleCase(v.GetExpected(), v.GetActual(), v.Query); err == nil {
-				break
-			} else if current != retryCount {
-				logger.Log.Warnf("verify case failure, will continue retry, %v", err)
-				time.Sleep(interval)
-			} else {
+		waitGroup.Wait()
+	} else {
+		for idx, v := range e2eConfig.Verify.Cases {
+			if v.GetExpected() == "" {
+				errMsg := fmt.Sprintf("the expected data file for case[%v] is not specified\n", idx)
 				if failFast {
-					return err
+					return errors.New(errMsg)
+				}
+				logger.Log.Warnf(errMsg)
+				errs = multierror.Append(errs, errors.New(errMsg))
+				continue
+			}
+
+			for current := 1; current <= retryCount; current++ {
+				if err := verifySingleCase(v.GetExpected(), v.GetActual(), v.Query); err == nil {
+					break
+				} else if current != retryCount {
+					logger.Log.Warnf("verify case failure, will continue retry, %v", err)
+					time.Sleep(interval)
+				} else {
+					if failFast {
+						return err
+					}
+					errs = multierror.Append(errs, err)
 				}
-				errCollection = multierror.Append(errCollection, err)
 			}
 		}
 	}
 
-	return errCollection.ErrorOrNil()
+	return errs.ErrorOrNil()
 }
 
 // TODO remove this in 2.0.0
diff --git a/docs/en/setup/Configuration-File.md b/docs/en/setup/Configuration-File.md
index d10a93f..23f3d1f 100644
--- a/docs/en/setup/Configuration-File.md
+++ b/docs/en/setup/Configuration-File.md
@@ -160,6 +160,7 @@ verify:
     count: 10       # max retry count
     interval: 10s   # the interval between two attempts, e.g. 10s, 1m.
   fail-fast: true  # when a case fails, whether to stop verifying other cases. This property defaults to true.
+  concurrency: false # whether to verify cases concurrently. This property defaults to false.
   cases:            # verify test cases
     - actual: path/to/actual.yaml       # verify by actual file path
       expected: path/to/expected.yaml   # excepted content file path
diff --git a/examples/compose/e2e.yaml b/examples/compose/e2e.yaml
index 89f7d0f..cec36e8 100644
--- a/examples/compose/e2e.yaml
+++ b/examples/compose/e2e.yaml
@@ -54,6 +54,8 @@ verify:
 
   # when a case fails, whether to stop verifying other cases. This property defaults to true.
   fail-fast: true
+  # Whether to verify cases concurrently. This property defaults to false.
+  concurrency: false
 
 
   cases:
diff --git a/internal/config/e2eConfig.go b/internal/config/e2eConfig.go
index 7237c70..ea35ab2 100644
--- a/internal/config/e2eConfig.go
+++ b/internal/config/e2eConfig.go
@@ -89,6 +89,7 @@ type Verify struct {
 	RetryStrategy VerifyRetryStrategy `yaml:"retry"`
 	Cases         []VerifyCase        `yaml:"cases"`
 	FailFast      bool                `yaml:"fail-fast"`
+	Concurrency   bool                `yaml:"concurrency"`
 }
 
 func (s *Setup) GetFile() string {