github.com/getgauge/gauge@v1.6.9/execution/parallelGrpcExecution_test.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package execution 8 9 import ( 10 "net" 11 "testing" 12 "time" 13 14 "github.com/getgauge/gauge-proto/go/gauge_messages" 15 "github.com/getgauge/gauge/execution/result" 16 "github.com/getgauge/gauge/gauge" 17 "github.com/getgauge/gauge/runner" 18 ) 19 20 func TestSuiteHooksAreExecutedOncePerRun(t *testing.T) { 21 specs := createSpecsList(6) 22 var receivedMesseges []*gauge_messages.Message 23 runner1 := &fakeGrpcRunner{messageCount: make(map[gauge_messages.Message_MessageType]int)} 24 runner2 := &fakeGrpcRunner{messageCount: make(map[gauge_messages.Message_MessageType]int)} 25 e := parallelExecution{ 26 numberOfExecutionStreams: 5, 27 specCollection: gauge.NewSpecCollection(specs, false), 28 runners: []runner.Runner{runner1, runner2}, 29 pluginHandler: &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) { 30 receivedMesseges = append(receivedMesseges, m) 31 }}, 32 } 33 34 t.Run("BeforeSuite", func(t *testing.T) { 35 receivedMesseges = []*gauge_messages.Message{} 36 e.suiteResult = result.NewSuiteResult("", time.Now()) 37 runner1.mockResult = &gauge_messages.ProtoExecutionResult{} 38 e.notifyBeforeSuite() 39 r1count := runner1.messageCount[gauge_messages.Message_ExecutionStarting] 40 if r1count != 1 { 41 t.Errorf("Expected runner1 to have received 1 ExecutionStarting request, got %d", r1count) 42 } 43 r2count := runner2.messageCount[gauge_messages.Message_ExecutionStarting] 44 if r2count != 0 { 45 t.Errorf("Expected runner2 to have received 0 ExecutionStarting request, got %d", r2count) 46 } 47 if len(receivedMesseges) != 2 { 48 t.Errorf("Expected plugins to have received 2 ExecutionStarting notifications, got %d", len(receivedMesseges)) 49 } 50 }) 51 52 t.Run("AfterSuite", func(t *testing.T) { 53 receivedMesseges = []*gauge_messages.Message{} 54 e.notifyAfterSuite() 55 e.suiteResult = result.NewSuiteResult("", time.Now()) 56 runner1.mockResult = &gauge_messages.ProtoExecutionResult{} 57 r1count := runner1.messageCount[gauge_messages.Message_ExecutionEnding] 58 if r1count != 1 { 59 t.Errorf("Expected runner1 to have received 1 ExecutionEnding request, got %d", r1count) 60 } 61 r2count := runner2.messageCount[gauge_messages.Message_ExecutionEnding] 62 if r2count != 0 { 63 t.Errorf("Expected runner2 to have received 0 ExecutionEnding request, got %d", r2count) 64 } 65 if len(receivedMesseges) != 2 { 66 t.Errorf("Expected plugins to have received 2 ExecutionStarting notifications, got %d", len(receivedMesseges)) 67 } 68 }) 69 } 70 71 type fakeGrpcRunner struct { 72 isMultiThreaded bool 73 messageCount map[gauge_messages.Message_MessageType]int 74 mockResult *gauge_messages.ProtoExecutionResult 75 } 76 77 func (f *fakeGrpcRunner) ExecuteMessageWithTimeout(m *gauge_messages.Message) (*gauge_messages.Message, error) { 78 return nil, nil 79 } 80 81 func (f *fakeGrpcRunner) ExecuteAndGetStatus(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult { 82 f.messageCount[m.MessageType]++ 83 return f.mockResult 84 } 85 func (f *fakeGrpcRunner) Alive() bool { 86 return false 87 } 88 func (f *fakeGrpcRunner) Kill() error { 89 return nil 90 } 91 func (f *fakeGrpcRunner) Connection() net.Conn { 92 return nil 93 } 94 func (f *fakeGrpcRunner) IsMultithreaded() bool { 95 return f.isMultiThreaded 96 } 97 98 func (f *fakeGrpcRunner) Info() *runner.RunnerInfo { 99 return nil 100 } 101 func (f *fakeGrpcRunner) Pid() int { 102 return 0 103 }