tester.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package executor
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "github.com/smartystreets/goconvey/web/server/contract"
  8. )
  9. type ConcurrentTester struct {
  10. shell contract.Shell
  11. batchSize int
  12. }
  13. func (self *ConcurrentTester) SetBatchSize(batchSize int) {
  14. self.batchSize = batchSize
  15. log.Printf("Now configured to test %d packages concurrently.\n", self.batchSize)
  16. }
  17. func (self *ConcurrentTester) TestAll(folders []*contract.Package) {
  18. if self.batchSize == 1 {
  19. self.executeSynchronously(folders)
  20. } else {
  21. newConcurrentCoordinator(folders, self.batchSize, self.shell).ExecuteConcurrently()
  22. }
  23. return
  24. }
  25. func (self *ConcurrentTester) executeSynchronously(folders []*contract.Package) {
  26. for _, folder := range folders {
  27. packageName := strings.Replace(folder.Name, "\\", "/", -1)
  28. if !folder.Active() {
  29. log.Printf("Skipping execution: %s\n", packageName)
  30. continue
  31. }
  32. if folder.HasImportCycle {
  33. message := fmt.Sprintf("can't load package: import cycle not allowed\npackage %s\n\timports %s", packageName, packageName)
  34. log.Println(message)
  35. folder.Output, folder.Error = message, errors.New(message)
  36. } else {
  37. log.Printf("Executing tests: %s\n", packageName)
  38. folder.Output, folder.Error = self.shell.GoTest(folder.Path, packageName, folder.BuildTags, folder.TestArguments)
  39. }
  40. }
  41. }
  42. func NewConcurrentTester(shell contract.Shell) *ConcurrentTester {
  43. self := new(ConcurrentTester)
  44. self.shell = shell
  45. self.batchSize = defaultBatchSize
  46. return self
  47. }
  48. const defaultBatchSize = 10