|
1 | 1 | package xhttp_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "errors"
|
5 | 6 | "fmt"
|
6 | 7 | "github.com/avast/retry-go"
|
7 | 8 | "github.com/mix-go/xhttp"
|
8 | 9 | "github.com/stretchr/testify/assert"
|
9 | 10 | "log"
|
| 11 | + "sync" |
10 | 12 | "testing"
|
11 | 13 | )
|
12 | 14 |
|
@@ -141,3 +143,34 @@ func TestMiddlewares(t *testing.T) {
|
141 | 143 | a.Equal(resp.StatusCode, 200)
|
142 | 144 | a.Nil(err)
|
143 | 145 | }
|
| 146 | + |
| 147 | +func TestShutdown(t *testing.T) { |
| 148 | + a := assert.New(t) |
| 149 | + logicMiddleware := func(next xhttp.HandlerFunc) xhttp.HandlerFunc { |
| 150 | + return func(req *xhttp.Request, opts *xhttp.RequestOptions) (*xhttp.Response, error) { |
| 151 | + // Before-logic |
| 152 | + fmt.Printf("Before: %s %s\n", req.Method, req.URL) |
| 153 | + |
| 154 | + // Call the next handler |
| 155 | + resp, err := next(req, opts) |
| 156 | + |
| 157 | + // After-logic |
| 158 | + fmt.Printf("After: %s %s %v\n", req.Method, req.URL, err) |
| 159 | + |
| 160 | + return resp, err |
| 161 | + } |
| 162 | + } |
| 163 | + _, err := xhttp.NewRequest("GET", "https://github.com/", xhttp.WithMiddlewares(logicMiddleware)) |
| 164 | + a.Nil(err) |
| 165 | + wg := sync.WaitGroup{} |
| 166 | + wg.Add(3) |
| 167 | + for i := 0; i < 3; i++ { |
| 168 | + go func(i int, wg *sync.WaitGroup) { |
| 169 | + defer wg.Done() |
| 170 | + _, err := xhttp.NewRequest("GET", "https://github.com/", xhttp.WithMiddlewares(logicMiddleware)) |
| 171 | + a.Equal(err, xhttp.ErrShutdown) |
| 172 | + }(i, &wg) |
| 173 | + } |
| 174 | + xhttp.Shutdown(context.Background()) |
| 175 | + wg.Wait() |
| 176 | +} |
0 commit comments