Skip to content

Commit

Permalink
Merge pull request #662 from Fenny/master
Browse files Browse the repository at this point in the history
🗑️ Remove deprecated tests
  • Loading branch information
Fenny authored Jul 22, 2020
2 parents 0fb21e5 + 396b6dd commit 2975344
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 209 deletions.
31 changes: 13 additions & 18 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
Expand All @@ -33,7 +32,7 @@ import (
)

// Version of current package
const Version = "1.13.2"
const Version = "1.13.3"

// Map is a shortcut for map[string]interface{}, useful for JSON returns
type Map map[string]interface{}
Expand All @@ -52,8 +51,8 @@ type App struct {
mutex sync.Mutex
// Route stack divided by HTTP methods
stack [][]*Route
// Amount of registered routes
routesCount int
// Amount of registered handlers
handlerCount int
// Ctx pool
pool sync.Pool
// Fasthttp server
Expand Down Expand Up @@ -425,10 +424,6 @@ func (app *App) Routes() []*Route {
routes = append(routes, app.stack[m][r])
}
}
// Sort routes by stack position
sort.Slice(routes, func(i, k int) bool {
return routes[i].pos < routes[k].pos
})
return routes
}

Expand Down Expand Up @@ -674,12 +669,12 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {

host, port := parseAddr(addr)
var (
tlsStr = "FALSE"
routesLen = len(app.Routes())
osName = utils.ToUpper(runtime.GOOS)
memTotal = utils.ByteSize(utils.MemoryTotal())
cpuThreads = runtime.NumCPU()
pid = os.Getpid()
tlsStr = "FALSE"
handlerCount = app.handlerCount
osName = utils.ToUpper(runtime.GOOS)
memTotal = utils.ByteSize(utils.MemoryTotal())
cpuThreads = runtime.NumCPU()
pid = os.Getpid()
)
if host == "" {
host = "0.0.0.0"
Expand All @@ -703,10 +698,10 @@ func (app *App) startupMessage(addr string, tls bool, pids string) {
}
// Build startup banner
fmt.Fprintf(out, logo, cBlack, cBlack,
cCyan, cBlack, fmt.Sprintf(" HOST %s\tOS %s", cyan(host), cyan(osName)),
cCyan, cBlack, fmt.Sprintf(" PORT %s\tTHREADS %s", cyan(port), cyan(cpuThreads)),
cCyan, cBlack, fmt.Sprintf(" TLS %s\tMEM %s", cyan(tlsStr), cyan(memTotal)),
cBlack, cyan(Version), fmt.Sprintf(" ROUTES %s\t\t\t PID %s%s%s\n", cyan(routesLen), cyan(pid), pids, cReset),
cCyan, cBlack, fmt.Sprintf(" HOST %s\tOS %s", cyan(host), cyan(osName)),
cCyan, cBlack, fmt.Sprintf(" PORT %s\tTHREADS %s", cyan(port), cyan(cpuThreads)),
cCyan, cBlack, fmt.Sprintf(" TLS %s\tMEM %s", cyan(tlsStr), cyan(memTotal)),
cBlack, cyan(Version), fmt.Sprintf(" HANDLERS %s\t\t\t PID %s%s%s\n", cyan(handlerCount), cyan(pid), pids, cReset),
)
// Write to io.write
_ = out.Flush()
Expand Down
123 changes: 0 additions & 123 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
}

func checkRouteCount(t *testing.T, app *App, expectedCount int) {
realStackCount := 0
for _, routes := range app.stack {
for range routes {
realStackCount++
}
}

utils.AssertEqual(t, expectedCount, app.routesCount)
utils.AssertEqual(t, expectedCount, realStackCount)
}

func Test_App_MethodNotAllowed(t *testing.T) {
app := New()

Expand Down Expand Up @@ -115,36 +103,6 @@ func Test_App_Custom_Middleware_404_Should_Not_SetMethodNotAllowed(t *testing.T)
utils.AssertEqual(t, 404, resp.StatusCode)
}

func Test_App_Routes(t *testing.T) {
app := New()
h := func(c *Ctx) {}
app.Use("/", h)
app.Use("/", h)
app.Get("/Get", h)
app.Head("/Head", h)
app.Post("/post", h)
utils.AssertEqual(t, 5, len(app.Routes()))
}

// go test -v -run=^$ -bench=Benchmark_App_Routes -benchmem -count=4
func Benchmark_App_Routes(b *testing.B) {
app := New()
h := func(c *Ctx) {}
app.Use("/", h)
app.Use("/", h)
app.Get("/Get", h)
app.Head("/Head", h)
app.Post("/post", h)

b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
app.Routes()
}
utils.AssertEqual(b, 4, len(app.Routes()))
}

func Test_App_ServerErrorHandler_SmallReadBuffer(t *testing.T) {
expectedError := regexp.MustCompile(
`error when reading request headers: small read buffer\. Increase ReadBufferSize\. Buffer size=4096, contents: "GET / HTTP/1.1\\r\\nHost: example\.com\\r\\nVery-Long-Header: -+`,
Expand Down Expand Up @@ -440,87 +398,6 @@ func Test_App_Methods(t *testing.T) {

}

func Test_App_RegisteredRouteCount(t *testing.T) {
var dummyHandler = func(c *Ctx) {}

app := New()
app.All("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodGet)
checkRouteCount(t, app, len(intMethod))

app = New()
app.Get("/:john?/:doe?", dummyHandler)
app.Head("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodGet)
checkRouteCount(t, app, 2)

app = New()
app.Head("/:john?/:doe?", dummyHandler)
app.Get("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodGet)
checkRouteCount(t, app, 2)

app = New()
app.Get("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodGet)
checkRouteCount(t, app, 2)

app = New()
app.Head("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodHead)
checkRouteCount(t, app, 1)

app = New()
app.Delete("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodDelete)
checkRouteCount(t, app, 1)

// with use
app = New()
app.Use("/:john?/:doe?", dummyHandler)
testStatus200(t, app, "/john/doe", MethodPut)
checkRouteCount(t, app, len(intMethod))

// with group
app = New()
app.Group("/:john?/:doe?", dummyHandler).Put("/wtf", dummyHandler)
testStatus200(t, app, "/john/doe/wtf", MethodPut)
checkRouteCount(t, app, len(intMethod)+1)

app = New()
app.Use("/", dummyHandler)
app.All("/bar", dummyHandler)
app.Get("/foo", dummyHandler)
app.Head("/foo", dummyHandler)
checkRouteCount(t, app, len(intMethod)*2+2)
}

func Test_App_RoutePositions(t *testing.T) {
var dummyHandler = func(c *Ctx) {}

app := New()
app.Use("/", dummyHandler)
app.All("/bar", dummyHandler)
app.Get("/foo", dummyHandler)
app.Head("/foo", dummyHandler)
testStatus200(t, app, "/foo", MethodGet)

expectedPos := 1
// check USE routes
for p := range intMethod {
utils.AssertEqual(t, expectedPos, app.stack[p][0].pos)
expectedPos++
}
// check ALL routes
for p := range intMethod {
utils.AssertEqual(t, expectedPos, app.stack[p][1].pos)
expectedPos++
}
// check GET and HEAD route
utils.AssertEqual(t, expectedPos, app.stack[methodInt(MethodGet)][2].pos)
utils.AssertEqual(t, expectedPos+1, app.stack[methodInt(MethodHead)][2].pos)
}

func Test_App_New(t *testing.T) {
app := New()
app.Get("/", func(*Ctx) {
Expand Down
12 changes: 0 additions & 12 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,6 @@ func Test_Ctx_BodyParser(t *testing.T) {

testDecodeParserError("invalid-content-type", "")
testDecodeParserError(MIMEMultipartForm+`;boundary="b"`, "--b")

type Query struct {
ID int
Name string
Hobby []string
}
ctx.Fasthttp.Request.SetBody([]byte(``))
ctx.Fasthttp.Request.Header.SetContentType("")
ctx.Fasthttp.Request.URI().SetQueryString("id=1&name=tom&hobby=basketball&hobby=football")
q := new(Query)
utils.AssertEqual(t, nil, ctx.BodyParser(q))
utils.AssertEqual(t, 2, len(q.Hobby))
}

// go test -v -run=^$ -bench=Benchmark_Ctx_BodyParser_JSON -benchmem -count=4
Expand Down
30 changes: 30 additions & 0 deletions prefork.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,33 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) {
// return error if child crashes
return (<-channel).err
}

// watchMaster watches child procs
func watchMaster() {
if runtime.GOOS == "windows" {
// finds parent process,
// and waits for it to exit
p, err := os.FindProcess(os.Getppid())
if err == nil {
_, _ = p.Wait()
}
os.Exit(1)
}
// if it is equal to 1 (init process ID),
// it indicates that the master process has exited
for range time.NewTicker(time.Millisecond * 500).C {
if os.Getppid() == 1 {
os.Exit(1)
}
}
}

var dummyChildCmd = "go"

// dummyCmd is for internal prefork testing
func dummyCmd() *exec.Cmd {
if runtime.GOOS == "windows" {
return exec.Command("cmd", "/C", dummyChildCmd, "version")
}
return exec.Command(dummyChildCmd, "version")
}
28 changes: 0 additions & 28 deletions prefork_utils.go

This file was deleted.

22 changes: 0 additions & 22 deletions prefork_utils_windows.go

This file was deleted.

14 changes: 8 additions & 6 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ type Router interface {
// Route is a struct that holds all metadata for each registered handler
type Route struct {
// Data for routing
pos int // Position in stack
use bool // USE matches path prefixes
star bool // Path equals '*'
root bool // Path equals '/'
Expand Down Expand Up @@ -196,6 +195,10 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Route {
Method: method,
Handlers: handlers,
}
// Increment global handler count
app.mutex.Lock()
app.handlerCount += len(handlers)
app.mutex.Unlock()
// Middleware route matches all HTTP methods
if isUse {
// Add route to all HTTP methods stack
Expand Down Expand Up @@ -309,6 +312,10 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Route {
Path: prefix,
Handlers: []Handler{handler},
}
// Increment global handler count
app.mutex.Lock()
app.handlerCount++
app.mutex.Unlock()
// Add route to stack
app.addRoute(MethodGet, &route)
// Add HEAD route
Expand All @@ -327,11 +334,6 @@ func (app *App) addRoute(method string, route *Route) {
preRoute := app.stack[m][l-1]
preRoute.Handlers = append(preRoute.Handlers, route.Handlers...)
} else {
// Increment global route position
app.mutex.Lock()
app.routesCount++
app.mutex.Unlock()
route.pos = app.routesCount
route.Method = method
// Add route to the stack
app.stack[m] = append(app.stack[m], route)
Expand Down

0 comments on commit 2975344

Please sign in to comment.