90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
|
|
package waiterr_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"codeberg.org/danjones000/waiterr"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Example() {
|
||
|
|
we := new(waiterr.WaitErr)
|
||
|
|
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(100 * time.Millisecond)
|
||
|
|
fmt.Println("Goroutine 1 finished")
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
fmt.Println("Goroutine 2 finished with an error")
|
||
|
|
return errors.New("something went wrong in goroutine 2")
|
||
|
|
})
|
||
|
|
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(150 * time.Millisecond)
|
||
|
|
fmt.Println("Goroutine 3 finished")
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
|
||
|
|
// Wait for all goroutines and get all errors
|
||
|
|
if err := we.Wait(); err != nil {
|
||
|
|
fmt.Printf("All goroutines finished. Combined error:\n%v\n", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Output:
|
||
|
|
// Goroutine 2 finished with an error
|
||
|
|
// Goroutine 1 finished
|
||
|
|
// Goroutine 3 finished
|
||
|
|
// All goroutines finished. Combined error:
|
||
|
|
// something went wrong in goroutine 2
|
||
|
|
}
|
||
|
|
|
||
|
|
func ExampleWaitErr_WaitForError() {
|
||
|
|
we := new(waiterr.WaitErr)
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(100 * time.Millisecond)
|
||
|
|
return errors.New("first error from we")
|
||
|
|
})
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
return errors.New("second error from we")
|
||
|
|
})
|
||
|
|
|
||
|
|
if err := we.WaitForError(); err != nil {
|
||
|
|
fmt.Printf("First error returned from we: %v\n", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Output:
|
||
|
|
// First error returned from we: second error from we
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
func ExampleWaitErr_Unwrap() {
|
||
|
|
we := new(waiterr.WaitErr)
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(100 * time.Millisecond)
|
||
|
|
return errors.New("first error from we")
|
||
|
|
})
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(50 * time.Millisecond)
|
||
|
|
return errors.New("second error from we")
|
||
|
|
})
|
||
|
|
we.Go(func() error {
|
||
|
|
time.Sleep(75 * time.Millisecond)
|
||
|
|
return nil
|
||
|
|
})
|
||
|
|
|
||
|
|
_ = we.Wait()
|
||
|
|
|
||
|
|
errs := we.Unwrap()
|
||
|
|
for _, e := range errs {
|
||
|
|
fmt.Println(e)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Output:
|
||
|
|
// second error from we
|
||
|
|
// first error from we
|
||
|
|
}
|