Skip to content

Commit

Permalink
🩹 Fix: go lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
JIeJaitt committed Feb 11, 2025
1 parent e5a1ef5 commit bab3038
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
6 changes: 4 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ func Test_App_ShutdownWithContext(t *testing.T) {
return nil
})

app.Hooks().OnPostShutdown(func(err error) error {
app.Hooks().OnPostShutdown(func(_ error) error {
hookMutex.Lock()
hookOrder = append(hookOrder, "post")
hookMutex.Unlock()
Expand Down Expand Up @@ -962,7 +962,9 @@ func Test_App_ShutdownWithContext(t *testing.T) {

ln := fasthttputil.NewInmemoryListener()
go func() {
_ = app.Listener(ln)
if err := app.Listener(ln); err != nil {
t.Errorf("Failed to start listener: %v", err)
}
}()

time.Sleep(100 * time.Millisecond)
Expand Down
14 changes: 8 additions & 6 deletions hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ func Test_Hook_OnPostShutdown(t *testing.T) {
})

go func() {
_ = app.Listen(":0")
if err := app.Listen(":0"); err != nil {
t.Errorf("Failed to start listener: %v", err)
}
}()

time.Sleep(100 * time.Millisecond)
Expand All @@ -225,7 +227,7 @@ func Test_Hook_OnPostShutdown(t *testing.T) {
t.Fatal("hook was not called")
}

if receivedErr != expectedErr {
if !errors.Is(receivedErr, expectedErr) {
t.Fatalf("hook received wrong error: want %v, got %v", expectedErr, receivedErr)
}
})
Expand All @@ -235,12 +237,12 @@ func Test_Hook_OnPostShutdown(t *testing.T) {

execution := make([]int, 0)

app.Hooks().OnPostShutdown(func(err error) error {
app.Hooks().OnPostShutdown(func(_ error) error {
execution = append(execution, 1)
return nil
})

app.Hooks().OnPostShutdown(func(err error) error {
app.Hooks().OnPostShutdown(func(_ error) error {
execution = append(execution, 2)
return nil
})
Expand All @@ -256,11 +258,11 @@ func Test_Hook_OnPostShutdown(t *testing.T) {
}
})

t.Run("should handle hook error", func(t *testing.T) {
t.Run("should handle hook error", func(_ *testing.T) {
app := New()
hookErr := errors.New("hook error")

app.Hooks().OnPostShutdown(func(err error) error {
app.Hooks().OnPostShutdown(func(_ error) error {
return hookErr
})

Expand Down
31 changes: 18 additions & 13 deletions listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func Test_Listen_Graceful_Shutdown(t *testing.T) {
}

func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
t.Helper()

var mu sync.Mutex
var shutdown bool

Expand All @@ -59,7 +61,7 @@ func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
ln := fasthttputil.NewInmemoryListener()
errs := make(chan error, 1)

app.hooks.OnPostShutdown(func(err error) error {
app.hooks.OnPostShutdown(func(_ error) error {
mu.Lock()
defer mu.Unlock()
shutdown = true
Expand All @@ -80,7 +82,9 @@ func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
require.Eventually(t, func() bool {
conn, err := ln.Dial()
if err == nil {
conn.Close()
if err := conn.Close(); err != nil {
t.Logf("error closing connection: %v", err)
}
return true
}
return false
Expand All @@ -90,14 +94,16 @@ func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
Dial: func(_ string) (net.Conn, error) { return ln.Dial() },
}

testCases := []struct {
type testCase struct {
name string
waitTime time.Duration
expectedErr error
expectedBody string
waitTime time.Duration
expectedStatusCode int
expectedErr error
closeConnection bool
}{
}

testCases := []testCase{
{
name: "Server running normally",
waitTime: 500 * time.Millisecond,
Expand All @@ -117,7 +123,6 @@ func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
time.Sleep(tc.waitTime)

Expand All @@ -131,18 +136,18 @@ func testGracefulShutdown(t *testing.T, shutdownTimeout time.Duration) {
err := client.Do(req, resp)

if tc.expectedErr == nil {
assert.NoError(t, err)
assert.Equal(t, tc.expectedStatusCode, resp.StatusCode())
assert.Equal(t, tc.expectedBody, utils.UnsafeString(resp.Body()))
require.NoError(t, err)
require.Equal(t, tc.expectedStatusCode, resp.StatusCode())
require.Equal(t, tc.expectedBody, utils.UnsafeString(resp.Body()))
} else {
assert.ErrorIs(t, err, tc.expectedErr)
require.ErrorIs(t, err, tc.expectedErr)
}
})
}

mu.Lock()
assert.True(t, shutdown)
assert.NoError(t, <-errs)
require.True(t, shutdown)
require.NoError(t, <-errs)
mu.Unlock()
}

Expand Down

0 comments on commit bab3038

Please sign in to comment.