gitee.com/mirrors/gauge@v1.0.6/execution/parallelExecution_test.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package execution 19 20 import ( 21 "testing" 22 23 "net" 24 25 "github.com/getgauge/gauge/env" 26 "github.com/getgauge/gauge/execution/result" 27 "github.com/getgauge/gauge/gauge" 28 "github.com/getgauge/gauge/gauge_messages" 29 . "gopkg.in/check.v1" 30 ) 31 32 func Test(t *testing.T) { TestingT(t) } 33 34 type MySuite struct{} 35 36 var _ = Suite(&MySuite{}) 37 38 func (s *MySuite) TestNumberOfStreams(c *C) { 39 specs := createSpecsList(6) 40 e := parallelExecution{numberOfExecutionStreams: 5, specCollection: gauge.NewSpecCollection(specs, false)} 41 c.Assert(e.numberOfStreams(), Equals, 5) 42 43 specs = createSpecsList(6) 44 e = parallelExecution{numberOfExecutionStreams: 10, specCollection: gauge.NewSpecCollection(specs, false)} 45 c.Assert(e.numberOfStreams(), Equals, 6) 46 47 specs = createSpecsList(0) 48 e = parallelExecution{numberOfExecutionStreams: 17, specCollection: gauge.NewSpecCollection(specs, false)} 49 c.Assert(e.numberOfStreams(), Equals, 0) 50 } 51 52 func getValidationErrorMap() *gauge.BuildErrors { 53 return &gauge.BuildErrors{ 54 SpecErrs: make(map[*gauge.Specification][]error), 55 ScenarioErrs: make(map[*gauge.Scenario][]error), 56 StepErrs: make(map[*gauge.Step]error), 57 } 58 } 59 60 func (s *MySuite) TestAggregationOfSuiteResult(c *C) { 61 e := parallelExecution{errMaps: getValidationErrorMap()} 62 suiteRes1 := &result.SuiteResult{ExecutionTime: 1, SpecsFailedCount: 1, IsFailed: true, SpecResults: []*result.SpecResult{&result.SpecResult{}, &result.SpecResult{}}} 63 suiteRes2 := &result.SuiteResult{ExecutionTime: 3, SpecsFailedCount: 0, IsFailed: false, SpecResults: []*result.SpecResult{&result.SpecResult{}, &result.SpecResult{}}} 64 suiteRes3 := &result.SuiteResult{ExecutionTime: 5, SpecsFailedCount: 0, IsFailed: false, SpecResults: []*result.SpecResult{&result.SpecResult{}, &result.SpecResult{}}} 65 var suiteResults []*result.SuiteResult 66 suiteResults = append(suiteResults, suiteRes1, suiteRes2, suiteRes3) 67 e.aggregateResults(suiteResults) 68 69 aggregatedRes := e.suiteResult 70 c.Assert(aggregatedRes.SpecsFailedCount, Equals, 1) 71 c.Assert(aggregatedRes.IsFailed, Equals, true) 72 c.Assert(len(aggregatedRes.SpecResults), Equals, 6) 73 c.Assert(aggregatedRes.SpecsSkippedCount, Equals, 0) 74 } 75 76 func (s *MySuite) TestAggregationOfSuiteResultWithUnhandledErrors(c *C) { 77 e := parallelExecution{} 78 suiteRes1 := &result.SuiteResult{IsFailed: true, UnhandledErrors: []error{streamExecError{specsSkipped: []string{"spec1", "spec2"}, message: "Runner failed to start"}}} 79 suiteRes2 := &result.SuiteResult{IsFailed: false, UnhandledErrors: []error{streamExecError{specsSkipped: []string{"spec3", "spec4"}, message: "Runner failed to start"}}} 80 suiteRes3 := &result.SuiteResult{IsFailed: false} 81 suiteRes4 := &result.SuiteResult{SpecResults: []*result.SpecResult{&result.SpecResult{Skipped: true}}} 82 var suiteResults []*result.SuiteResult 83 suiteResults = append(suiteResults, suiteRes1, suiteRes2, suiteRes3, suiteRes4) 84 e.aggregateResults(suiteResults) 85 86 aggregatedRes := e.suiteResult 87 c.Assert(len(aggregatedRes.UnhandledErrors), Equals, 2) 88 c.Assert(aggregatedRes.UnhandledErrors[0].Error(), Equals, "The following specifications could not be executed:\n"+ 89 "spec1\n"+ 90 "spec2\n"+ 91 "Reason : Runner failed to start.") 92 c.Assert(aggregatedRes.UnhandledErrors[1].Error(), Equals, "The following specifications could not be executed:\n"+ 93 "spec3\n"+ 94 "spec4\n"+ 95 "Reason : Runner failed to start.") 96 err := (aggregatedRes.UnhandledErrors[0]).(streamExecError) 97 c.Assert(len(err.specsSkipped), Equals, 2) 98 c.Assert(aggregatedRes.SpecsSkippedCount, Equals, 1) 99 } 100 101 func (s *MySuite) TestAggregationOfSuiteResultWithHook(c *C) { 102 e := parallelExecution{errMaps: getValidationErrorMap()} 103 suiteRes1 := &result.SuiteResult{PreSuite: &gauge_messages.ProtoHookFailure{}} 104 suiteRes2 := &result.SuiteResult{PreSuite: &gauge_messages.ProtoHookFailure{}} 105 suiteRes3 := &result.SuiteResult{PostSuite: &gauge_messages.ProtoHookFailure{}} 106 var suiteResults []*result.SuiteResult 107 suiteResults = append(suiteResults, suiteRes1, suiteRes2, suiteRes3) 108 e.aggregateResults(suiteResults) 109 110 aggregatedRes := e.suiteResult 111 c.Assert(aggregatedRes.PreSuite, Equals, suiteRes2.PreSuite) 112 c.Assert(aggregatedRes.PostSuite, Equals, suiteRes3.PostSuite) 113 } 114 115 func (s *MySuite) TestIsMultiThreadedWithEnvSetToFalse(c *C) { 116 e := parallelExecution{errMaps: getValidationErrorMap()} 117 118 env.EnableMultiThreadedExecution = func() bool { return false } 119 120 c.Assert(false, Equals, e.isMultithreaded()) 121 } 122 123 func (s *MySuite) TestIsMultiThreadedWithRunnerWhenSupportsMultithreading(c *C) { 124 e := parallelExecution{errMaps: getValidationErrorMap(), runner: &fakeRunner{isMultiThreaded: true}} 125 126 env.EnableMultiThreadedExecution = func() bool { return true } 127 128 c.Assert(true, Equals, e.isMultithreaded()) 129 } 130 131 func (s *MySuite) TestIsMultiThreadedWithRunnerWhenDoesNotSupportMultithreading(c *C) { 132 e := parallelExecution{errMaps: getValidationErrorMap(), runner: &fakeRunner{isMultiThreaded: false}} 133 134 env.EnableMultiThreadedExecution = func() bool { return true } 135 136 c.Assert(false, Equals, e.isMultithreaded()) 137 } 138 139 type fakeRunner struct { 140 isMultiThreaded bool 141 } 142 143 func (f *fakeRunner) ExecuteMessageWithTimeout(m *gauge_messages.Message) (*gauge_messages.Message, error) { 144 return nil, nil 145 } 146 147 func (f *fakeRunner) ExecuteAndGetStatus(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 148 return nil 149 } 150 func (f *fakeRunner) Alive() bool { 151 return false 152 } 153 func (f *fakeRunner) Kill() error { 154 return nil 155 } 156 func (f *fakeRunner) Connection() net.Conn { 157 return nil 158 } 159 func (f *fakeRunner) IsMultithreaded() bool { 160 return f.isMultiThreaded 161 } 162 func (f *fakeRunner) Pid() int { 163 return 0 164 }