go.temporal.io/server@v1.23.0/common/tasks/sequential_scheduler_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2023 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package tasks
    26  
    27  import (
    28  	"errors"
    29  	"sync"
    30  	"testing"
    31  	"time"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/stretchr/testify/require"
    35  	"github.com/stretchr/testify/suite"
    36  
    37  	"go.temporal.io/server/common/backoff"
    38  	"go.temporal.io/server/common/dynamicconfig"
    39  	"go.temporal.io/server/common/log"
    40  )
    41  
    42  type (
    43  	sequentialSchedulerSuite struct {
    44  		*require.Assertions
    45  		suite.Suite
    46  
    47  		controller *gomock.Controller
    48  
    49  		scheduler   *SequentialScheduler[*MockTask]
    50  		retryPolicy backoff.RetryPolicy
    51  	}
    52  )
    53  
    54  func TestSequentialSchedulerSuite(t *testing.T) {
    55  	s := new(sequentialSchedulerSuite)
    56  	suite.Run(t, s)
    57  }
    58  
    59  func (s *sequentialSchedulerSuite) SetupTest() {
    60  	s.Assertions = require.New(s.T())
    61  
    62  	s.controller = gomock.NewController(s.T())
    63  
    64  	s.retryPolicy = backoff.NewExponentialRetryPolicy(time.Millisecond)
    65  	s.scheduler = s.newTestProcessor()
    66  	s.scheduler.Start()
    67  }
    68  
    69  func (s *sequentialSchedulerSuite) TearDownTest() {
    70  	s.scheduler.Stop()
    71  	s.controller.Finish()
    72  }
    73  
    74  func (s *sequentialSchedulerSuite) TestSubmitProcess_Running_Success() {
    75  	testWaitGroup := sync.WaitGroup{}
    76  	testWaitGroup.Add(1)
    77  
    78  	mockTask := NewMockTask(s.controller)
    79  	mockTask.EXPECT().RetryPolicy().Return(s.retryPolicy).AnyTimes()
    80  	mockTask.EXPECT().Execute().Return(nil).Times(1)
    81  	mockTask.EXPECT().Ack().Do(func() { testWaitGroup.Done() }).Times(1)
    82  
    83  	s.scheduler.Submit(mockTask)
    84  
    85  	testWaitGroup.Wait()
    86  }
    87  
    88  func (s *sequentialSchedulerSuite) TestSubmitProcess_Running_FailExecution() {
    89  	testWaitGroup := sync.WaitGroup{}
    90  	testWaitGroup.Add(1)
    91  
    92  	mockTask := NewMockTask(s.controller)
    93  	mockTask.EXPECT().RetryPolicy().Return(s.retryPolicy).AnyTimes()
    94  	executionErr := errors.New("random error")
    95  	mockTask.EXPECT().Execute().Return(executionErr).Times(1)
    96  	mockTask.EXPECT().HandleErr(executionErr).Return(executionErr).Times(1)
    97  	mockTask.EXPECT().IsRetryableError(executionErr).Return(false).MaxTimes(1)
    98  	mockTask.EXPECT().Nack(executionErr).Do(func(_ error) { testWaitGroup.Done() }).Times(1)
    99  
   100  	s.scheduler.Submit(mockTask)
   101  
   102  	testWaitGroup.Wait()
   103  }
   104  
   105  func (s *sequentialSchedulerSuite) TestSubmitProcess_Stopped_Submission() {
   106  	testWaitGroup := sync.WaitGroup{}
   107  	testWaitGroup.Add(1)
   108  
   109  	s.scheduler.Stop()
   110  
   111  	mockTask := NewMockTask(s.controller)
   112  
   113  	// if task get picked up before worker goroutine receives the shutdown notification
   114  	mockTask.EXPECT().RetryPolicy().Return(s.retryPolicy).MaxTimes(1)
   115  	mockTask.EXPECT().Execute().Return(nil).MaxTimes(1)
   116  	mockTask.EXPECT().Ack().Do(func() { testWaitGroup.Done() }).MaxTimes(1)
   117  
   118  	// if task get drained
   119  	mockTask.EXPECT().Abort().Do(func() { testWaitGroup.Done() }).MaxTimes(1)
   120  
   121  	s.scheduler.Submit(mockTask)
   122  
   123  	testWaitGroup.Wait()
   124  }
   125  
   126  func (s *sequentialSchedulerSuite) TestSubmitProcess_Stopped_FailExecution() {
   127  	testWaitGroup := sync.WaitGroup{}
   128  	testWaitGroup.Add(1)
   129  
   130  	mockTask := NewMockTask(s.controller)
   131  	mockTask.EXPECT().RetryPolicy().Return(s.retryPolicy).AnyTimes()
   132  	executionErr := errors.New("random transient error")
   133  	mockTask.EXPECT().Execute().Return(executionErr).Times(1)
   134  	mockTask.EXPECT().HandleErr(executionErr).DoAndReturn(func(err error) error {
   135  		s.scheduler.Stop()
   136  		return err
   137  	}).Times(1)
   138  	mockTask.EXPECT().IsRetryableError(executionErr).Return(true).MaxTimes(1)
   139  	mockTask.EXPECT().Abort().Do(func() { testWaitGroup.Done() }).Times(1)
   140  
   141  	s.scheduler.Submit(mockTask)
   142  
   143  	testWaitGroup.Wait()
   144  }
   145  
   146  func (s *sequentialSchedulerSuite) TestParallelSubmitProcess() {
   147  	numSubmitter := 200
   148  	numTasks := 100
   149  
   150  	testWaitGroup := sync.WaitGroup{}
   151  	testWaitGroup.Add(numSubmitter * numTasks)
   152  
   153  	startWaitGroup := sync.WaitGroup{}
   154  	endWaitGroup := sync.WaitGroup{}
   155  
   156  	startWaitGroup.Add(numSubmitter)
   157  
   158  	for i := 0; i < numSubmitter; i++ {
   159  		channel := make(chan *MockTask, numTasks)
   160  		for j := 0; j < numTasks; j++ {
   161  			mockTask := NewMockTask(s.controller)
   162  			mockTask.EXPECT().RetryPolicy().Return(s.retryPolicy).AnyTimes()
   163  			switch j % 2 {
   164  			case 0:
   165  				// success
   166  				mockTask.EXPECT().Execute().Return(nil).Times(1)
   167  				mockTask.EXPECT().Ack().Do(func() { testWaitGroup.Done() }).Times(1)
   168  
   169  			case 1:
   170  				// fail
   171  				executionErr := errors.New("random error")
   172  				mockTask.EXPECT().Execute().Return(executionErr).Times(1)
   173  				mockTask.EXPECT().HandleErr(executionErr).Return(executionErr).Times(1)
   174  				mockTask.EXPECT().IsRetryableError(executionErr).Return(false).Times(1)
   175  				mockTask.EXPECT().Nack(executionErr).Do(func(_ error) { testWaitGroup.Done() }).Times(1)
   176  
   177  			default:
   178  				s.Fail("case not expected")
   179  			}
   180  			channel <- mockTask
   181  		}
   182  		close(channel)
   183  
   184  		endWaitGroup.Add(1)
   185  		go func() {
   186  			startWaitGroup.Wait()
   187  
   188  			for mockTask := range channel {
   189  				s.scheduler.Submit(mockTask)
   190  			}
   191  
   192  			endWaitGroup.Done()
   193  		}()
   194  		startWaitGroup.Done()
   195  	}
   196  	endWaitGroup.Wait()
   197  
   198  	testWaitGroup.Wait()
   199  }
   200  
   201  func (s *sequentialSchedulerSuite) TestStartStopWorkers() {
   202  	processor := s.newTestProcessor()
   203  	// don't start the processor,
   204  	// manually add/remove workers here to test the start/stop logic
   205  
   206  	numWorkers := 10
   207  	processor.startWorkers(numWorkers)
   208  	s.Len(processor.workerShutdownCh, numWorkers)
   209  
   210  	processor.stopWorkers(numWorkers / 2)
   211  	s.Len(processor.workerShutdownCh, numWorkers/2)
   212  
   213  	processor.stopWorkers(len(processor.workerShutdownCh))
   214  	s.Empty(processor.workerShutdownCh)
   215  
   216  	processor.shutdownWG.Wait()
   217  }
   218  
   219  func (s *sequentialSchedulerSuite) newTestProcessor() *SequentialScheduler[*MockTask] {
   220  	hashFn := func(key interface{}) uint32 {
   221  		return 1
   222  	}
   223  	factory := func(task *MockTask) SequentialTaskQueue[*MockTask] {
   224  		return newTestSequentialTaskQueue[*MockTask](1, 3000)
   225  	}
   226  	return NewSequentialScheduler[*MockTask](
   227  		&SequentialSchedulerOptions{
   228  			QueueSize:   1,
   229  			WorkerCount: dynamicconfig.GetIntPropertyFn(1),
   230  		},
   231  		hashFn,
   232  		factory,
   233  		log.NewNoopLogger(),
   234  	)
   235  }