@@ -36,6 +36,7 @@ import (
36
36
"github.com/olivere/elastic/v7"
37
37
"github.com/stretchr/testify/suite"
38
38
39
+ "go.temporal.io/server/common"
39
40
"go.temporal.io/server/common/collection"
40
41
"go.temporal.io/server/common/dynamicconfig"
41
42
"go.temporal.io/server/common/future"
@@ -89,6 +90,7 @@ func (s *processorSuite) SetupTest() {
89
90
// esProcessor.Start mock
90
91
s .esProcessor .mapToAckFuture = collection .NewShardedConcurrentTxMap (1024 , s .esProcessor .hashFn )
91
92
s .esProcessor .bulkProcessor = s .mockBulkProcessor
93
+ s .esProcessor .status = common .DaemonStatusStarted
92
94
}
93
95
94
96
func (s * processorSuite ) TearDownTest () {
@@ -126,8 +128,8 @@ func (s *processorSuite) TestNewESProcessorAndStartStop() {
126
128
s .NotNil (p .bulkProcessor )
127
129
128
130
p .Stop ()
129
- s .Nil (p .mapToAckFuture )
130
- s .Nil (p .bulkProcessor )
131
+ s .NotNil (p .mapToAckFuture )
132
+ s .NotNil (p .bulkProcessor )
131
133
}
132
134
133
135
func (s * processorSuite ) TestAdd () {
@@ -219,6 +221,43 @@ func (s *processorSuite) TestAdd_ConcurrentAdd_Duplicates() {
219
221
s .Equal (1 , s .esProcessor .mapToAckFuture .Len (), "only one request should be in the bulk" )
220
222
}
221
223
224
+ func (s * processorSuite ) TestAdd_ConcurrentAdd_Shutdown () {
225
+ request := & client.BulkableRequest {}
226
+ docsCount := 1000
227
+ parallelFactor := 10
228
+ futures := make ([]future.Future [bool ], docsCount )
229
+
230
+ s .mockBulkProcessor .EXPECT ().Add (request ).MaxTimes (docsCount + 2 ) // +2 for explicit adds before and after shutdown
231
+ s .mockBulkProcessor .EXPECT ().Stop ().Return (nil ).Times (1 )
232
+ s .mockMetricHandler .EXPECT ().Timer (metrics .ElasticsearchBulkProcessorWaitAddLatency .GetMetricName ()).Return (metrics .NoopTimerMetricFunc ).MaxTimes (docsCount + 2 )
233
+
234
+ addBefore := s .esProcessor .Add (request , "test-key-before" )
235
+
236
+ wg := sync.WaitGroup {}
237
+ wg .Add (parallelFactor + 1 ) // +1 for separate shutdown goroutine
238
+ for i := 0 ; i < parallelFactor ; i ++ {
239
+ go func (i int ) {
240
+ for j := 0 ; j < docsCount / parallelFactor ; j ++ {
241
+ futures [i * docsCount / parallelFactor + j ] = s .esProcessor .Add (request , fmt .Sprintf ("test-key-%d-%d" , i , j ))
242
+ }
243
+ wg .Done ()
244
+ }(i )
245
+ }
246
+ go func () {
247
+ time .Sleep (1 * time .Millisecond ) // slight delay so at least a few docs get added
248
+ s .esProcessor .Stop ()
249
+ wg .Done ()
250
+ }()
251
+
252
+ wg .Wait ()
253
+ addAfter := s .esProcessor .Add (request , "test-key-after" )
254
+
255
+ s .False (addBefore .Ready ()) // first request should be in bulk
256
+ s .True (addAfter .Ready ()) // final request should be only error
257
+ _ , err := addAfter .Get (context .Background ())
258
+ s .ErrorIs (err , errVisibilityShutdown )
259
+ }
260
+
222
261
func (s * processorSuite ) TestBulkAfterAction_Ack () {
223
262
version := int64 (3 )
224
263
testKey := "testKey"
0 commit comments