Skip to content

Commit 51d38e3

Browse files
committed
Update xhttp test
1 parent d42ecd6 commit 51d38e3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

client_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package xhttp_test
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"github.com/avast/retry-go"
78
"github.com/mix-go/xhttp"
89
"github.com/stretchr/testify/assert"
910
"log"
11+
"sync"
1012
"testing"
1113
)
1214

@@ -141,3 +143,34 @@ func TestMiddlewares(t *testing.T) {
141143
a.Equal(resp.StatusCode, 200)
142144
a.Nil(err)
143145
}
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

Comments
 (0)