- Add http.Server field to App struct for explicit server management - Add StartServer() method to create and start HTTP server - Replace echo.Close() with http.Server.Shutdown() in Shutdown() - Update import from echo/v4 to echo/v5 Changes: - New() initializes server field as nil - StartServer() creates http.Server with Echo as handler - Shutdown() uses http.Server.Shutdown() with context timeout - Removed deprecated echo.Close() call (v5 API change) This provides better control over server lifecycle and graceful shutdown.
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package app
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestApp_New tests App constructor
|
|
func TestApp_New(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
app := New(e)
|
|
|
|
assert.NotNil(t, app, "App should not be nil")
|
|
assert.Equal(t, e, app.echo, "Echo instance should be stored")
|
|
assert.Equal(t, 30*time.Second, app.shutdownTimeout, "Default shutdown timeout should be 30 seconds")
|
|
assert.NotNil(t, app.shutdownDone, "Shutdown done channel should be initialized")
|
|
}
|
|
|
|
// TestApp_SetShutdownTimeout tests configurable shutdown timeout
|
|
func TestApp_SetShutdownTimeout(t *testing.T) {
|
|
e := echo.New()
|
|
app := New(e)
|
|
|
|
customTimeout := 15 * time.Second
|
|
app.SetShutdownTimeout(customTimeout)
|
|
|
|
assert.Equal(t, customTimeout, app.shutdownTimeout, "Shutdown timeout should be updated")
|
|
}
|
|
|
|
// TestApp_ShutdownDone tests shutdown done channel
|
|
func TestApp_ShutdownDone(t *testing.T) {
|
|
e := echo.New()
|
|
app := New(e)
|
|
|
|
channel := app.ShutdownDone()
|
|
assert.NotNil(t, channel, "ShutdownDone should return a channel")
|
|
// Verify it's the same channel by checking if it's readable
|
|
select {
|
|
case <-channel:
|
|
// Channel should not be closed yet
|
|
t.Error("ShutdownDone channel should not be closed immediately")
|
|
default:
|
|
// Expected - channel is open but not ready
|
|
}
|
|
}
|
|
|
|
// BenchmarkApp_Shutdown benchmarks the shutdown process
|
|
func BenchmarkApp_Shutdown(b *testing.B) {
|
|
e := echo.New()
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
app := New(e)
|
|
app.Shutdown()
|
|
}
|
|
}
|