@@ -57,11 +57,16 @@ type GoRoutineMap interface {
5757 // a new operation to be started with the same operation name without error.
5858 Run (operationName string , operationFunc func () error ) error
5959
60- // Wait blocks until all operations are completed . This is typically
60+ // Wait blocks until operations map is empty . This is typically
6161 // necessary during tests - the test should wait until all operations finish
6262 // and evaluate results after that.
6363 Wait ()
6464
65+ // WaitForCompletion blocks until either all operations have successfully completed
66+ // or have failed but are not pending. The test should wait until operations are either
67+ // complete or have failed.
68+ WaitForCompletion ()
69+
6570 // IsOperationPending returns true if the operation is pending (currently
6671 // running), otherwise returns false.
6772 IsOperationPending (operationName string ) bool
@@ -179,6 +184,32 @@ func (grm *goRoutineMap) Wait() {
179184 }
180185}
181186
187+ func (grm * goRoutineMap ) WaitForCompletion () {
188+ grm .lock .Lock ()
189+ defer grm .lock .Unlock ()
190+
191+ for {
192+ if len (grm .operations ) == 0 || grm .nothingPending () {
193+ break
194+ } else {
195+ grm .cond .Wait ()
196+ }
197+ }
198+ }
199+
200+ // Check if any operation is pending. Already assumes caller has the
201+ // necessary locks
202+ func (grm * goRoutineMap ) nothingPending () bool {
203+ nothingIsPending := true
204+ for _ , operation := range grm .operations {
205+ if operation .operationPending {
206+ nothingIsPending = false
207+ break
208+ }
209+ }
210+ return nothingIsPending
211+ }
212+
182213// NewAlreadyExistsError returns a new instance of AlreadyExists error.
183214func NewAlreadyExistsError (operationName string ) error {
184215 return alreadyExistsError {operationName }
0 commit comments