Skip to content

Commit

Permalink
Init routes in ServeConn
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Jun 2, 2020
1 parent 972a673 commit 5c63aec
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func (s *Atreugo) HandleOPTIONS(v bool) {
//
// ServeConn closes c before returning.
func (s *Atreugo) ServeConn(c net.Conn) error {
s.init()

return s.server.ServeConn(c)
}

Expand Down
2 changes: 2 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ func (r *Router) init() {
optionsURLsHandled = append(optionsURLsHandled, p.url)
}
}

r.paths = nil
}

func (r *Router) buildMiddlewaresChain(skip ...Middleware) Middlewares {
Expand Down
36 changes: 25 additions & 11 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,27 @@ func TestRouter_init(t *testing.T) { // nolint:funlen
r := newRouter(testLog, nil)
r.appendPath(path)
r.appendPath(pathOptions)

totalRegisteredViews := len(r.paths) + 1 // Add +1 for auto OPTIONS handle

r.init()

// Check if a re-execution raise a panic
func() {
defer func() {
err := recover()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}()

r.init()
}()

if len(registeredViews) != totalRegisteredViews {
t.Fatalf("Registered views == %d, want %d", len(registeredViews), totalRegisteredViews)
}

ctx := new(fasthttp.RequestCtx)

h, _ := r.router.Lookup(path.method, path.url, ctx)
Expand All @@ -218,11 +237,6 @@ func TestRouter_init(t *testing.T) { // nolint:funlen
}
}

totalRegisteredViews := len(r.paths) + 1 // Add +1 for auto OPTIONS handle
if len(registeredViews) != totalRegisteredViews {
t.Fatalf("Registered views == %d, want %d", len(registeredViews), totalRegisteredViews)
}

if reflect.ValueOf(registeredViews[0]).Pointer() != reflect.ValueOf(path.view).Pointer() {
t.Errorf("Registered view == %p, want %p", registeredViews[0], path.view)
}
Expand Down Expand Up @@ -889,15 +903,15 @@ func TestRouter_Path_Shortcuts(t *testing.T) { //nolint:funlen
}

for _, test := range tests {
tt := test
test.args.fn(path, viewFn)
}

t.Run(tt.name, func(t *testing.T) {
r.paths = r.paths[:0]
r.router = fastrouter.New()
r.init()

tt.args.fn(path, viewFn)
r.init()
for _, test := range tests {
tt := test

t.Run(tt.name, func(t *testing.T) {
reqMethod := tt.args.method
for reqMethod == fastrouter.MethodWild {
reqMethod = randomHTTPMethod()
Expand Down

0 comments on commit 5c63aec

Please sign in to comment.