|
1 | 1 | package patron |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | +) |
| 11 | + |
| 12 | +type WorkerOrchestratorTestSuite struct { |
| 13 | + suite.Suite |
| 14 | + |
| 15 | + workerOrchestrator WorkerOrchestrator |
| 16 | +} |
| 17 | + |
| 18 | +func TestWorkerOrchestratorTestSuite(t *testing.T) { |
| 19 | + suite.Run(t, new(WorkerOrchestratorTestSuite)) |
| 20 | +} |
| 21 | + |
| 22 | +func (suite *WorkerOrchestratorTestSuite) SetupTest() { |
| 23 | + workerOrchestrator := NewWorkerOrchestrator( |
| 24 | + NewWorkerArray(func(job *Job) error { |
| 25 | + time.Sleep(1 * time.Second) |
| 26 | + payloadName, err := job.GetPayload("name") |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + fmt.Printf("%d. job completed.\nJob payload name: %s\n", job.ID, payloadName) |
| 32 | + |
| 33 | + return nil |
| 34 | + }, 5), |
| 35 | + ) |
| 36 | + |
| 37 | + suite.workerOrchestrator = workerOrchestrator |
| 38 | +} |
| 39 | + |
| 40 | +func (suite *WorkerOrchestratorTestSuite) TestNewWorkerOrchestrator() { |
| 41 | + suite.NotNil(NewWorkerOrchestrator( |
| 42 | + NewWorkerArray(nil, 1), |
| 43 | + )) |
| 44 | +} |
| 45 | + |
| 46 | +func (suite *WorkerOrchestratorTestSuite) TestAddJobToQueue() { |
| 47 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 48 | + ID: 10, |
| 49 | + Context: context.Background(), |
| 50 | + Payload: map[string]interface{}{ |
| 51 | + "name": "HTTP Request", |
| 52 | + "dest_url": "http://localhost:8080/", |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + suite.Equal(1, suite.workerOrchestrator.GetQueueLength()) |
| 57 | +} |
| 58 | + |
| 59 | +// Note: this test is not reliable. |
| 60 | +func (suite *WorkerOrchestratorTestSuite) TestStartAsAsync() { |
| 61 | + suite.T().Skip() |
| 62 | + |
| 63 | + workerResultCh := make(chan *WorkerResult) |
| 64 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 65 | + ID: 10, |
| 66 | + Context: context.Background(), |
| 67 | + Payload: map[string]interface{}{ |
| 68 | + "name": "HTTP Request", |
| 69 | + "dest_url": "http://localhost:8080/test", |
| 70 | + }, |
| 71 | + }) |
| 72 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 73 | + ID: 11, |
| 74 | + Context: context.Background(), |
| 75 | + Payload: map[string]interface{}{ |
| 76 | + "name": "HTTP Request", |
| 77 | + "dest_url": "http://localhost:8080/test2", |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + suite.workerOrchestrator.StartAsAsync(context.Background(), workerResultCh) |
| 82 | + suite.NotEmpty(<-workerResultCh) |
| 83 | + suite.NotEmpty(<-workerResultCh) |
| 84 | +} |
| 85 | + |
| 86 | +func (suite *WorkerOrchestratorTestSuite) TestStartAllJobsSuccess() { |
| 87 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 88 | + ID: 10, |
| 89 | + Context: context.Background(), |
| 90 | + Payload: map[string]interface{}{ |
| 91 | + "name": "HTTP Request", |
| 92 | + "dest_url": "http://localhost:8080/test", |
| 93 | + }, |
| 94 | + }) |
| 95 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 96 | + ID: 11, |
| 97 | + Context: context.Background(), |
| 98 | + Payload: map[string]interface{}{ |
| 99 | + "name": "HTTP Request", |
| 100 | + "dest_url": "http://localhost:8080/test2", |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + results := suite.workerOrchestrator.Start(context.Background()) |
| 105 | + |
| 106 | + suite.Len(results, 2) |
| 107 | + suite.Equal(nil, results[0].Error) |
| 108 | + suite.Equal(nil, results[1].Error) |
| 109 | +} |
| 110 | + |
| 111 | +func (suite *WorkerOrchestratorTestSuite) TestStartOneJobFailure() { |
| 112 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 113 | + ID: 10, |
| 114 | + Context: context.Background(), |
| 115 | + Payload: map[string]interface{}{ |
| 116 | + "name": "HTTP Request", |
| 117 | + "dest_url": "http://localhost:8080/test", |
| 118 | + }, |
| 119 | + }) |
| 120 | + suite.workerOrchestrator.AddJobToQueue(&Job{ |
| 121 | + ID: 11, |
| 122 | + Context: context.Background(), |
| 123 | + Payload: map[string]interface{}{}, |
| 124 | + }) |
| 125 | + |
| 126 | + results := suite.workerOrchestrator.Start(context.Background()) |
| 127 | + |
| 128 | + suite.Len(results, 2) |
| 129 | + for _, result := range results { |
| 130 | + if result.JobID == 11 { |
| 131 | + suite.ErrorIs(result.Error, ErrJobPayloadNotFound) |
| 132 | + } else { |
| 133 | + suite.Equal(nil, result.Error) |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments