Skip to content

Commit

Permalink
Update xhttp test
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed May 22, 2024
1 parent d42ecd6 commit 51d38e3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package xhttp_test

import (
"context"
"errors"
"fmt"
"github.com/avast/retry-go"
"github.com/mix-go/xhttp"
"github.com/stretchr/testify/assert"
"log"
"sync"
"testing"
)

Expand Down Expand Up @@ -141,3 +143,34 @@ func TestMiddlewares(t *testing.T) {
a.Equal(resp.StatusCode, 200)
a.Nil(err)
}

func TestShutdown(t *testing.T) {
a := assert.New(t)
logicMiddleware := func(next xhttp.HandlerFunc) xhttp.HandlerFunc {
return func(req *xhttp.Request, opts *xhttp.RequestOptions) (*xhttp.Response, error) {
// Before-logic
fmt.Printf("Before: %s %s\n", req.Method, req.URL)

// Call the next handler
resp, err := next(req, opts)

// After-logic
fmt.Printf("After: %s %s %v\n", req.Method, req.URL, err)

return resp, err
}
}
_, err := xhttp.NewRequest("GET", "https://github.com/", xhttp.WithMiddlewares(logicMiddleware))
a.Nil(err)
wg := sync.WaitGroup{}
wg.Add(3)
for i := 0; i < 3; i++ {
go func(i int, wg *sync.WaitGroup) {
defer wg.Done()
_, err := xhttp.NewRequest("GET", "https://github.com/", xhttp.WithMiddlewares(logicMiddleware))
a.Equal(err, xhttp.ErrShutdown)
}(i, &wg)
}
xhttp.Shutdown(context.Background())
wg.Wait()
}

0 comments on commit 51d38e3

Please sign in to comment.