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

v3: Improve and simplify logic of ctx.Next() #3063

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,19 +1023,20 @@
func (c *DefaultCtx) Next() error {
// Increment handler index
c.indexHandler++
var err error

// Did we execute all route handlers?
if c.indexHandler < len(c.route.Handlers) {
// Continue route stack
err = c.route.Handlers[c.indexHandler](c)
} else {
// Continue handler stack
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
return c.route.Handlers[c.indexHandler](c)
}

// Continue handler stack
if c.app.newCtxFunc != nil {
_, err := c.app.nextCustom(c)
return err

Check warning on line 1036 in ctx.go

View check run for this annotation

Codecov / codecov/patch

ctx.go#L1035-L1036

Added lines #L1035 - L1036 were not covered by tests
}
Comment on lines +1033 to +1037
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified control flow is correct but lacks test coverage.

The simplification of the control flow is correct and improves readability. However, lines 1035-1036 are not covered by tests.

+ // Ensure lines 1035-1036 are covered by tests.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: codecov/patch

[warning] 1035-1036: ctx.go#L1035-L1036
Added lines #L1035 - L1036 were not covered by tests


_, err := c.app.next(c)
return err
}

Expand Down
21 changes: 21 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ func Benchmark_Router_Next(b *testing.B) {
require.Equal(b, 4, c.indexRoute)
}

// go test -v ./... -run=^$ -bench=Benchmark_Router_Next_Default -benchmem -count=4
func Benchmark_Router_Next_Default(b *testing.B) {
app := New()
app.Get("/", func(_ Ctx) error {
return nil
})

h := app.Handler()

fctx := &fasthttp.RequestCtx{}
fctx.Request.Header.SetMethod(MethodGet)
fctx.Request.SetRequestURI("/")

b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
h(fctx)
}
}

// go test -v ./... -run=^$ -bench=Benchmark_Route_Match -benchmem -count=4
func Benchmark_Route_Match(b *testing.B) {
var match bool
Expand Down
Loading