Skip to content

Commit

Permalink
Code refactor and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Andres Virviescas Santana committed Jun 17, 2020
1 parent e30c88c commit 4a5d4ee
Show file tree
Hide file tree
Showing 9 changed files with 505 additions and 551 deletions.
4 changes: 0 additions & 4 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ 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 All @@ -184,8 +182,6 @@ func (s *Atreugo) ServeConn(c net.Conn) error {
func (s *Atreugo) Serve(ln net.Listener) error {
defer ln.Close()

s.init()

s.cfg.Addr = ln.Addr().String()
s.cfg.Network = ln.Addr().Network()

Expand Down
15 changes: 7 additions & 8 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ func Test_AttachContext(t *testing.T) {
t.Error("ctx.AttachContext() the context is not attached")
}

defer func() {
err := recover()
if err == nil {
t.Error("Panic expected when attachs to itself")
}
}()

actx.AttachContext(actx)
err := catchPanic(func() {
actx.AttachContext(actx)
})

if err == nil {
t.Error("Panic expected when attachs to itself")
}
}

func Test_AttachedContext(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/atreugo/mock v0.0.0-20200601091009-13c275b330b0
github.com/fasthttp/router v1.2.2
github.com/savsgio/go-logger/v2 v2.0.1
github.com/savsgio/gotils v0.0.0-20200608150037-a5f6f5aef16c
github.com/savsgio/gotils v0.0.0-20200616100644-13ff1fd2c28c
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.14.0
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss
github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/savsgio/go-logger/v2 v2.0.1 h1:MgmsI5hnQFz0n5oX1Do/T9DldTo60guQTGwlRpzk0mk=
github.com/savsgio/go-logger/v2 v2.0.1/go.mod h1:fog+sZsg75Or4EENyd1jrS/Ps44s39hd6lccqALz0C8=
github.com/savsgio/gotils v0.0.0-20200608150037-a5f6f5aef16c h1:2nF5+FZ4/qp7pZVL7fR6DEaSTzuDmNaFTyqp92/hwF8=
github.com/savsgio/gotils v0.0.0-20200608150037-a5f6f5aef16c/go.mod h1:TWNAOTaVzGOXq8RbEvHnhzA/A2sLZzgn0m6URjnukY8=
github.com/savsgio/gotils v0.0.0-20200616100644-13ff1fd2c28c h1:KKqhycXW1WVNkX7r4ekTV2gFkbhdyihlWD8c0/FiWmk=
github.com/savsgio/gotils v0.0.0-20200616100644-13ff1fd2c28c/go.mod h1:TWNAOTaVzGOXq8RbEvHnhzA/A2sLZzgn0m6URjnukY8=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
Expand Down
12 changes: 12 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
func (p *Path) Middlewares(middlewares Middlewares) *Path {
p.middlewares = middlewares

p.router.handlePath(p)

return p
}

Expand All @@ -21,6 +23,8 @@ func (p *Path) Middlewares(middlewares Middlewares) *Path {
func (p *Path) UseBefore(fns ...Middleware) *Path {
p.middlewares.Before = append(p.middlewares.Before, fns...)

p.router.handlePath(p)

return p
}

Expand All @@ -29,13 +33,17 @@ func (p *Path) UseBefore(fns ...Middleware) *Path {
func (p *Path) UseAfter(fns ...Middleware) *Path {
p.middlewares.After = append(p.middlewares.After, fns...)

p.router.handlePath(p)

return p
}

// SkipMiddlewares registers the middlewares that you want to skip only when executing the view.
func (p *Path) SkipMiddlewares(fns ...Middleware) *Path {
p.middlewares.Skip = append(p.middlewares.Skip, fns...)

p.router.handlePath(p)

return p
}

Expand All @@ -52,6 +60,8 @@ func (p *Path) Timeout(timeout time.Duration, msg string) *Path {
p.timeoutMsg = msg
p.timeoutCode = fasthttp.StatusRequestTimeout

p.router.handlePath(p)

return p
}

Expand All @@ -68,5 +78,7 @@ func (p *Path) TimeoutCode(timeout time.Duration, msg string, statusCode int) *P
p.timeoutMsg = msg
p.timeoutCode = statusCode

p.router.handlePath(p)

return p
}
72 changes: 49 additions & 23 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,42 @@ var middlewareFns = []Middleware{
},
}

func assertTimeoutFields(t *testing.T, p *Path, timeout time.Duration, msg string, statusCode int) {
if !p.withTimeout {
t.Error("Path.withTimeout is not true")
}

if p.timeout != timeout {
t.Errorf("Path.timeout == %d, want %d", p.timeout, timeout)
}

if p.timeoutMsg != msg {
t.Errorf("Path.timeoutMsg == %s, want %s", p.timeoutMsg, msg)
}

if p.timeoutCode != statusCode {
t.Errorf("Path.timeoutCode == %d, want %d", p.timeoutCode, statusCode)
}
}

func assertHandle(t *testing.T, p *Path) {
h, _ := p.router.router.Lookup(p.method, p.url, nil)
if h == nil {
t.Error("Path not updated")
}
}

func newTestPath() *Path {
return &Path{
router: newRouter(testLog, nil),
method: fasthttp.MethodGet,
url: "/test",
view: func(ctx *RequestCtx) error { return nil },
}
}

func TestPath_Middlewares(t *testing.T) {
p := new(Path)
p := newTestPath()
p.Middlewares(Middlewares{Before: middlewareFns, After: middlewareFns, Skip: middlewareFns})

if len(p.middlewares.Before) != len(middlewareFns) {
Expand All @@ -32,66 +66,57 @@ func TestPath_Middlewares(t *testing.T) {
if len(p.middlewares.Skip) != len(middlewareFns) {
t.Errorf("Skip middlewares are not registered")
}

assertHandle(t, p)
}

func TestPath_UseBefore(t *testing.T) {
p := new(Path)
p := newTestPath()
p.UseBefore(middlewareFns...)

if len(p.middlewares.Before) != len(middlewareFns) {
t.Errorf("Before middlewares are not registered")
}

assertHandle(t, p)
}

func TestPath_UseAfter(t *testing.T) {
p := new(Path)
p := newTestPath()
p.UseAfter(middlewareFns...)

if len(p.middlewares.After) != len(middlewareFns) {
t.Errorf("After middlewares are not registered")
}

assertHandle(t, p)
}

func TestPath_SkipMiddlewares(t *testing.T) {
p := new(Path)
p := newTestPath()
p.SkipMiddlewares(middlewareFns...)

if len(p.middlewares.Skip) != len(middlewareFns) {
t.Errorf("Skip middlewares are not registered")
}
}

func assertTimeoutFields(t *testing.T, p *Path, timeout time.Duration, msg string, statusCode int) {
if !p.withTimeout {
t.Error("Path.withTimeout is not true")
}

if p.timeout != timeout {
t.Errorf("Path.timeout == %d, want %d", p.timeout, timeout)
}

if p.timeoutMsg != msg {
t.Errorf("Path.timeoutMsg == %s, want %s", p.timeoutMsg, msg)
}

if p.timeoutCode != statusCode {
t.Errorf("Path.timeoutCode == %d, want %d", p.timeoutCode, statusCode)
}
assertHandle(t, p)
}

func TestPath_Timeout(t *testing.T) {
p := new(Path)
p := newTestPath()

timeout := 10 * time.Millisecond
msg := "test"

p.Timeout(timeout, msg)

assertTimeoutFields(t, p, timeout, msg, fasthttp.StatusRequestTimeout)
assertHandle(t, p)
}

func TestPath_TimeoutCode(t *testing.T) {
p := new(Path)
p := newTestPath()

timeout := 10 * time.Millisecond
msg := "test"
Expand All @@ -100,4 +125,5 @@ func TestPath_TimeoutCode(t *testing.T) {
p.TimeoutCode(timeout, msg, statusCode)

assertTimeoutFields(t, p, timeout, msg, statusCode)
assertHandle(t, p)
}
Loading

0 comments on commit 4a5d4ee

Please sign in to comment.