Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty done channel makes ctx always done when creating test server #1906

Closed
quagmt opened this issue Nov 25, 2024 · 0 comments
Closed

Empty done channel makes ctx always done when creating test server #1906

quagmt opened this issue Nov 25, 2024 · 0 comments

Comments

@quagmt
Copy link

quagmt commented Nov 25, 2024

Description

  • I'm using fiber, which uses fasthttp v1.57. I think the recent changes in server.done channel initialization causes the context to always done when writing tests. Considering this example:
package main

import (
	"context"
	"io"
	"net/http"

	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()

	app.Get("/", func(c *fiber.Ctx) error {
		select {
		case <-c.Context().Done():
			return c.JSON("context is done")
		default:
			return c.JSON("context is not done")
		}
	})

	req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
	resp, _ := app.Test(req)

	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)

	// Output: should be "context is not done" but it's "context is done"
	println(string(body))
}
  • The reason I think is that server.done channel is only initialized when calling server.Serve or when checking context.Done(). Its value is nil when creating a new fasthttp.Server struct, which fiber v2 is doing.
  • c.Context().Done() will create a new channel (because done is nil) and send an empty message to the channel, which causes the context.Done() to return immediately. (though I'm not sure if this is intentional).
  • done channel is also not exported so it's impossible to assign it to a channel to avoid this issue. I see there's a fakeServer which init the done channel, but it's also not exported. Is this possible to have a method to init this done channel?

EDIT: this doesn't happen if we run the server and serve real requests because done channel is created when calling server.Serve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant