github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/util/execution/parallel_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package execution 22 23 import ( 24 "context" 25 "fmt" 26 "testing" 27 28 "github.com/fortytw2/leaktest" 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 type request struct { 34 order int 35 processed bool 36 err error 37 wait <-chan bool 38 ack chan<- bool 39 } 40 41 func (f *request) Process(ctx context.Context) error { 42 defer func() { 43 if f.ack != nil { 44 f.ack <- true 45 } 46 }() 47 48 if f.err != nil { 49 return f.err 50 } 51 52 if f.wait != nil { 53 <-f.wait 54 } 55 56 select { 57 case <-ctx.Done(): 58 return ctx.Err() 59 default: 60 f.processed = true 61 return nil 62 } 63 } 64 65 func (f *request) String() string { 66 return fmt.Sprintf("%v %v %v", f.order, f.processed, f.err) 67 } 68 69 func TestOrderedParallel(t *testing.T) { 70 requests := make([]Request, 3) 71 signalChan := make(chan bool) 72 requests[0] = &request{order: 0, wait: signalChan} 73 requests[1] = &request{order: 1, ack: signalChan} 74 requests[2] = &request{order: 2} 75 76 err := ExecuteParallel(context.Background(), requests) 77 require.NoError(t, err, "no error during parallel execute") 78 assert.True(t, requests[0].(*request).processed, "slowest request processed") 79 } 80 81 func TestSingleError(t *testing.T) { 82 defer leaktest.Check(t)() 83 requests := make([]Request, 3) 84 signalChan := make(chan bool) 85 86 var cancelErr error 87 requests[0] = funcRequest(func(ctx context.Context) error { 88 // wait for the second goroutine to finish 89 <-signalChan 90 91 // wait for cancellation. This will hang if there's a bug here (i.e. the context doesn't get cancelled); 92 // we rely on leaktest to catch that case. 93 <-ctx.Done() 94 95 cancelErr = ctx.Err() 96 return nil 97 }) 98 requests[1] = &request{order: 1, err: fmt.Errorf("problem executing"), ack: signalChan} 99 requests[2] = &request{order: 2} 100 101 err := ExecuteParallel(context.Background(), requests) 102 assert.Error(t, err, "error in second request") 103 104 assert.EqualError(t, cancelErr, "context canceled", 105 "context should be cancelled in case of any request error") 106 } 107 108 type funcRequest func(ctx context.Context) error 109 110 func (f funcRequest) Process(ctx context.Context) error { 111 return f(ctx) 112 }