|
| 1 | +package group_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/signal" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/pkg/group" |
| 14 | +) |
| 15 | + |
| 16 | +type Group = group.G |
| 17 | + |
| 18 | +func ExampleGroup_Wait() { |
| 19 | + // A Group's zero value is ready to use. |
| 20 | + var g group.G |
| 21 | + |
| 22 | + // Add a goroutine to the group. |
| 23 | + g.Add(func(c context.Context) error { |
| 24 | + select { |
| 25 | + case <-c.Done(): |
| 26 | + return c.Err() |
| 27 | + case <-time.After(1 * time.Second): |
| 28 | + return errors.New("timed out") |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + // Wait for all goroutines to finish. |
| 33 | + if err := g.Wait(); err != nil { |
| 34 | + fmt.Println(err) |
| 35 | + } |
| 36 | + |
| 37 | + // Output: timed out |
| 38 | +} |
| 39 | + |
| 40 | +func ExampleGroup_Wait_with_startup_error() { |
| 41 | + // A Group's zero value is ready to use. |
| 42 | + var g group.G |
| 43 | + |
| 44 | + // Add a goroutine to the group. |
| 45 | + g.Add(func(_ context.Context) error { |
| 46 | + return errors.New("startup error") |
| 47 | + }) |
| 48 | + |
| 49 | + // Wait for all goroutines to finish, in this case it will return the startup error. |
| 50 | + if err := g.Wait(); err != nil { |
| 51 | + fmt.Println(err) |
| 52 | + } |
| 53 | + |
| 54 | + // Output: startup error |
| 55 | +} |
| 56 | + |
| 57 | +func ExampleGroup_Wait_with_panic() { |
| 58 | + // A Group's zero value is ready to use. |
| 59 | + var g group.G |
| 60 | + |
| 61 | + // Add a goroutine to the group. |
| 62 | + g.Add(func(c context.Context) error { |
| 63 | + panic("boom") |
| 64 | + }) |
| 65 | + |
| 66 | + // Wait for all goroutines to finish. |
| 67 | + if err := g.Wait(); err != nil { |
| 68 | + fmt.Println(err) |
| 69 | + } |
| 70 | + |
| 71 | + // Output: panic: boom |
| 72 | +} |
| 73 | + |
| 74 | +func ExampleGroup_Wait_with_shutdown() { |
| 75 | + // A Group's zero value is ready to use. |
| 76 | + var g group.G |
| 77 | + |
| 78 | + shutdown := make(chan struct{}) |
| 79 | + |
| 80 | + // Add a goroutine to the group. |
| 81 | + g.Add(func(c context.Context) error { |
| 82 | + select { |
| 83 | + case <-c.Done(): |
| 84 | + return errors.New("stopped") |
| 85 | + case <-shutdown: |
| 86 | + return errors.New("shutdown") |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + time.AfterFunc(100*time.Millisecond, func() { |
| 91 | + close(shutdown) |
| 92 | + }) |
| 93 | + |
| 94 | + // Wait for all goroutines to finish. |
| 95 | + if err := g.Wait(); err != nil { |
| 96 | + fmt.Println(err) |
| 97 | + } |
| 98 | + |
| 99 | + // Output: shutdown |
| 100 | +} |
| 101 | + |
| 102 | +func ExampleGroup_Wait_with_context_cancel() { |
| 103 | + ctx := context.Background() |
| 104 | + ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond)) |
| 105 | + |
| 106 | + // pass WithContext option to New to use the provided context. |
| 107 | + g := group.New(group.WithContext(ctx)) |
| 108 | + |
| 109 | + // Add a goroutine to the group. |
| 110 | + g.Add(func(c context.Context) error { |
| 111 | + select { |
| 112 | + case <-c.Done(): |
| 113 | + return c.Err() |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + // Cancel the context. |
| 118 | + cancel() |
| 119 | + |
| 120 | + // Wait for all goroutines to finish. |
| 121 | + if err := g.Wait(); err != nil { |
| 122 | + fmt.Println(err) |
| 123 | + } |
| 124 | + |
| 125 | + // Output: context canceled |
| 126 | +} |
| 127 | + |
| 128 | +func ExampleGroup_Wait_with_signal() { |
| 129 | + ctx := context.Background() |
| 130 | + ctx, _ = signal.NotifyContext(ctx, os.Interrupt) |
| 131 | + |
| 132 | + g := group.New(group.WithContext(ctx)) |
| 133 | + |
| 134 | + g.Add(MainHTTPServer) |
| 135 | + g.Add(DebugHTTPServer) |
| 136 | + g.Add(AsyncLogger) |
| 137 | + |
| 138 | + <-time.After(100 * time.Millisecond) |
| 139 | + |
| 140 | + // simulate ^C |
| 141 | + proc, _ := os.FindProcess(os.Getpid()) |
| 142 | + proc.Signal(os.Interrupt) |
| 143 | + |
| 144 | + if err := g.Wait(); err != nil { |
| 145 | + fmt.Println(err) |
| 146 | + } |
| 147 | + |
| 148 | + // Unordered output: |
| 149 | + // async logger started |
| 150 | + // debug http server started |
| 151 | + // main http server started |
| 152 | + // async logger stopped |
| 153 | + // main http server stopped |
| 154 | + // debug http server stopped |
| 155 | + // context canceled |
| 156 | +} |
| 157 | + |
| 158 | +func ExampleGroup_Wait_with_http_shutdown() { |
| 159 | + ctx := context.Background() |
| 160 | + ctx, cancel := context.WithDeadline(ctx, time.Now().Add(100*time.Millisecond)) |
| 161 | + defer cancel() |
| 162 | + |
| 163 | + g := group.New(group.WithContext(ctx)) |
| 164 | + |
| 165 | + g.Add(func(ctx context.Context) error { |
| 166 | + l, err := net.Listen("tcp", "127.0.0.1:0") |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + svr := http.Server{ |
| 171 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 172 | + fmt.Fprintln(w, "hello, world!") |
| 173 | + })} |
| 174 | + go func() { |
| 175 | + svr.Serve(l) |
| 176 | + }() |
| 177 | + |
| 178 | + <-ctx.Done() // wait for group to stop |
| 179 | + |
| 180 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // five seconds graceful timeout |
| 181 | + defer cancel() |
| 182 | + return svr.Shutdown(shutdownCtx) |
| 183 | + }) |
| 184 | + |
| 185 | + if err := g.Wait(); err != nil { |
| 186 | + fmt.Println(err) |
| 187 | + } |
| 188 | + |
| 189 | + // Output: |
| 190 | +} |
| 191 | +func MainHTTPServer(ctx context.Context) error { |
| 192 | + fmt.Println("main http server started") |
| 193 | + defer fmt.Println("main http server stopped") |
| 194 | + <-ctx.Done() |
| 195 | + return ctx.Err() |
| 196 | +} |
| 197 | + |
| 198 | +func DebugHTTPServer(ctx context.Context) error { |
| 199 | + fmt.Println("debug http server started") |
| 200 | + defer fmt.Println("debug http server stopped") |
| 201 | + <-ctx.Done() |
| 202 | + return ctx.Err() |
| 203 | +} |
| 204 | + |
| 205 | +func AsyncLogger(ctx context.Context) error { |
| 206 | + fmt.Println("async logger started") |
| 207 | + defer fmt.Println("async logger stopped") |
| 208 | + <-ctx.Done() |
| 209 | + return ctx.Err() |
| 210 | +} |
0 commit comments