github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/checker/worker_pool_test.go (about) 1 // Copyright 2022 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package checker 15 16 import ( 17 "context" 18 "errors" 19 "sync/atomic" 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/require" 24 ) 25 26 type slowIncrementer struct { 27 atomic.Int64 28 } 29 30 func (i *slowIncrementer) Inc() int64 { 31 time.Sleep(100 * time.Millisecond) 32 return i.Add(1) 33 } 34 35 type job struct { 36 inc *slowIncrementer 37 } 38 39 type baseAdder struct { 40 base int64 41 closed atomic.Bool 42 } 43 44 func (a *baseAdder) add(_ context.Context, j job) (int64, error) { 45 i := j.inc.Inc() 46 return a.base + i, nil 47 } 48 49 func TestExampleWorkerPool(t *testing.T) { 50 sum := int64(0) 51 concurrency := 100 52 jobNum := 1000 53 incrementer := slowIncrementer{} 54 55 pool := NewWorkerPool[job, int64](func(result int64) { 56 sum += result 57 }) 58 for i := 0; i < concurrency; i++ { 59 worker := &baseAdder{base: 666} 60 pool.Go(worker.add) 61 } 62 for i := 0; i < jobNum; i++ { 63 pool.PutJob(job{inc: &incrementer}) 64 } 65 66 err := pool.Wait() 67 require.NoError(t, err) 68 // sum 1 to 1000 = 500500 69 require.Equal(t, int64(666*jobNum+500500), sum) 70 } 71 72 var ( 73 errMock = errors.New("mock error") 74 errorAt = int64(500) 75 ) 76 77 func (a *baseAdder) addAndError(_ context.Context, j job) (int64, error) { 78 i := j.inc.Inc() 79 if i == errorAt { 80 if a.closed.Load() { 81 panic("worker is used after closed") 82 } 83 a.closed.Store(true) 84 return 0, errMock 85 } 86 return a.base + i, nil 87 } 88 89 func TestExampleWorkerPoolError(t *testing.T) { 90 sum := int64(0) 91 concurrency := 100 92 jobNum := 1000 93 incrementer := slowIncrementer{} 94 95 pool := NewWorkerPool[job, int64](func(result int64) { 96 sum += result 97 }) 98 for i := 0; i < concurrency; i++ { 99 worker := &baseAdder{base: 666} 100 pool.Go(worker.addAndError) 101 } 102 for i := 0; i < jobNum; i++ { 103 ok := pool.PutJob(job{inc: &incrementer}) 104 if !ok { 105 require.GreaterOrEqual(t, int64(i), errorAt) 106 break 107 } 108 } 109 110 err := pool.Wait() 111 require.ErrorIs(t, err, errMock) 112 }