mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 12:12:40 +02:00
IMG-51: router -> server ns, handlers ns, added error to handler ret val (#1494)
* Introduced server, handlers, error ret in handlerfn * Server struct with tests * replace checkErr with return
This commit is contained in:
67
server/timer_test.go
Normal file
67
server/timer_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCheckTimeout(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
setup func() context.Context
|
||||
fail bool
|
||||
}{
|
||||
{
|
||||
name: "WithoutTimeout",
|
||||
setup: context.Background,
|
||||
fail: false,
|
||||
},
|
||||
{
|
||||
name: "ActiveTimerContext",
|
||||
setup: func() context.Context {
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
newReq, _ := startRequestTimer(req)
|
||||
return newReq.Context()
|
||||
},
|
||||
fail: false,
|
||||
},
|
||||
{
|
||||
name: "CancelledContext",
|
||||
setup: func() context.Context {
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
newReq, cancel := startRequestTimer(req)
|
||||
cancel() // Cancel immediately
|
||||
return newReq.Context()
|
||||
},
|
||||
fail: true,
|
||||
},
|
||||
{
|
||||
name: "DeadlineExceeded",
|
||||
setup: func() context.Context {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||
defer cancel()
|
||||
time.Sleep(time.Millisecond * 10) // Ensure timeout
|
||||
return ctx
|
||||
},
|
||||
fail: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := tt.setup()
|
||||
err := CheckTimeout(ctx)
|
||||
|
||||
if tt.fail {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user