Skip to content

Commit

Permalink
Workers should fail to start if they were stopped before (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitarb authored May 26, 2021
1 parent b434b50 commit 67ea830
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/internal_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ func (aw *AggregatedWorker) RegisterActivityWithOptions(a interface{}, options R

// Start the worker in a non-blocking fashion.
func (aw *AggregatedWorker) Start() error {
aw.assertNotStopped()
if err := initBinaryChecksum(); err != nil {
return fmt.Errorf("failed to get executable checksum: %v", err)
}
Expand Down Expand Up @@ -934,6 +935,18 @@ func (aw *AggregatedWorker) Start() error {
return nil
}

func (aw *AggregatedWorker) assertNotStopped() {
stopped := true
select {
case <-aw.stopC:
default:
stopped = false
}
if stopped {
panic("attempted to start a worker that has been stopped before")
}
}

var binaryChecksum string
var binaryChecksumLock sync.Mutex

Expand Down
20 changes: 20 additions & 0 deletions internal/internal_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,26 @@ func (s *internalWorkerTestSuite) TestWorkerStartFailsWithInvalidNamespace() {
}
}

func (s *internalWorkerTestSuite) TestStartWorkerAfterStopped() {
defer func() {
if r := recover(); r == nil {
assert.Fail(s.T(), "calling start after stop must result in panic")
}
}()
worker := createWorkerWithThrottle(s.service, 500.0, nil)
worker.RegisterActivity(testActivityNoResult)
worker.RegisterWorkflow(testWorkflowReturnStruct)
err := worker.Start()
require.NoError(s.T(), err)
time.Sleep(time.Millisecond * 200)
assert.True(s.T(), worker.activityWorker.worker.isWorkerStarted)
assert.True(s.T(), worker.workflowWorker.worker.isWorkerStarted)
worker.Stop()
assert.False(s.T(), worker.activityWorker.worker.isWorkerStarted)
assert.False(s.T(), worker.workflowWorker.worker.isWorkerStarted)
_ = worker.Start() // must panic
}

func ofPollActivityTaskQueueRequest(tps float64) gomock.Matcher {
return &mockPollActivityTaskQueueRequest{tps: tps}
}
Expand Down

0 comments on commit 67ea830

Please sign in to comment.