Skip to content

Commit

Permalink
Add tests, refactor test utilities, and improve docs
Browse files Browse the repository at this point in the history
Introduced a test for the RPC server's `echoHandler` functionality, refactored `TestContext` with enhanced methods (e.g., `Expect`, `Receiver`), and added new error handling utilities. Refined documentation, including update of README with performance benchmarks, and removed unnecessary linters from configuration files.
  • Loading branch information
ehsannm committed Jan 16, 2025
1 parent 03f4ba2 commit e5473e5
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 35 deletions.
7 changes: 3 additions & 4 deletions .fleet/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"editor.guides": [],
"go.imports.params": "",
"plugins": [
{
"type": "add",
Expand All @@ -18,5 +16,6 @@
"type": "add",
"pluginName": "fleet.shell"
}
]
}
],
"backend.maxHeapSizeMb": 8192
}
11 changes: 10 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

RonyKIT is a collection of tools designed to be both extendable and flexible, enabling the rapid development of a complete API/Edge server.
With the RonyKIT framework, you can create API handlers that support both RPC and REST style APIs without the need to rewrite the
endpoint layer multiple times.
endpoint layer multiple times. The philosphy of Ronykit is to define the most used building blocks of the software as proper minimal
viable interfaces and provide some standard implementations which make onboarding very quick and also let you to implement your own
if you have a very specific case. The declarative force of defining contracts in Ronykit makes extending the code much easier and
by the help of proper templating you can write the usecase and let the most boilerplates like stubs, tests, deployments etc to be
auto-generated.

If you are seeking a framework to develop your API/Edge server, you can directly use the `rony` package. This package provides an
intuitive framework, leveraging Go Generics to offer a clean and user-friendly API. This is the recommended approach for using
Expand All @@ -14,4 +18,9 @@ memory usage, you can directly utilize the `kit` package, which forms the core o
- For more information about the `kit` package, visit [kit](./kit/README.MD).
- For more information about the `stub` package, visit [stub](./stub/README.MD).

# Performance
Benchmarks may not provide precise measurements of a framework's performance; however, they can offer
insights into the overhead a framework might introduce to your actual business logic. You can see a
[benchmark](https://www.techempower.com/benchmarks/#hw=ph&test=json&section=data-r22&l=zijocf-cn3) as a reference to evaluate how Ronykit performs in comparison to other Go frameworks.

![Benchmark Results](./docs/benchmark.png)
2 changes: 1 addition & 1 deletion contrib/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand All @@ -22,6 +21,7 @@ linters:
- exhaustive
- depguard
- perfsprint
- recvcheck
fast: false
linters-settings:
# decorder:
Expand Down
2 changes: 1 addition & 1 deletion contrib/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func toPostmanItem(c desc.ParsedContract) *postman.Items {
}

var queryParams []*postman.QueryParam
if len(c.PathParams) > len(c.Request.Message.Fields) || c.Method == "GET" {
if len(c.PathParams) > len(c.Request.Message.Fields) || c.Method == http.MethodGet {
for _, p := range c.Request.Message.Fields {
found := false
for _, pp := range c.PathParams {
Expand Down
Binary file added docs/benchmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions example/ex-01-rpc/cmd/server/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"testing"

"github.com/clubpay/ronykit/example/ex-01-rpc/dto"
"github.com/clubpay/ronykit/kit"
"github.com/stretchr/testify/assert"
)

func TestEcho(t *testing.T) {
var (
reqDTO = &dto.EchoRequest{
RandomID: 2374,
Ok: false,
}
resDTO *dto.EchoResponse
errDTO *dto.ErrorMessage
)
err := kit.Expect(
kit.NewTestContext().SetHandler(echoHandler),
reqDTO, &resDTO, &errDTO,
)
if err != nil {
t.Fatal(err)
}
assert.Nil(t, errDTO)
assert.Equal(t, reqDTO.RandomID, resDTO.RandomID)
assert.Equal(t, reqDTO.Ok, resDTO.Ok)
}
3 changes: 3 additions & 0 deletions example/ex-01-rpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ require (
github.com/clubpay/ronykit/std/gateways/fasthttp v0.17.10
github.com/clubpay/ronykit/std/gateways/fastws v0.17.10
github.com/clubpay/ronykit/stub v0.17.10
github.com/stretchr/testify v1.9.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fasthttp/router v1.5.2 // indirect
github.com/fasthttp/websocket v1.5.10 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
Expand All @@ -31,6 +33,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/panjf2000/gnet/v2 v2.5.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rbretecher/go-postman-collection v0.9.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
Expand Down
1 change: 0 additions & 1 deletion kit/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
89 changes: 71 additions & 18 deletions kit/ctx_testkit.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package kit

import (
"errors"
"mime/multipart"
"sync"

"github.com/clubpay/ronykit/kit/utils"
)

var (
ErrExpectationsDontMatch = errors.New("expectations don't match")
ErrUnexpectedEnvelope = errors.New("unexpected envelope")
)

// TestContext is useful for writing end-to-end tests for your Contract handlers.
type TestContext struct {
ls localStore
handlers HandlerFuncChain
inMsg Message
inHdr EnvelopeHdr
clientIP string
expectFunc func(...*Envelope) error
ls localStore
handlers HandlerFuncChain
inMsg Message
inHdr EnvelopeHdr
clientIP string

expectFunc []func(e *Envelope) error
receiverFunc func(...*Envelope) error
}

func NewTestContext() *TestContext {
Expand Down Expand Up @@ -45,33 +53,53 @@ func (testCtx *TestContext) Input(m Message, hdr EnvelopeHdr) *TestContext {
}

func (testCtx *TestContext) Receiver(f func(out ...*Envelope) error) *TestContext {
testCtx.expectFunc = f
testCtx.receiverFunc = f

return testCtx
}

func (testCtx *TestContext) Expect(f func(e *Envelope) error) *TestContext {
testCtx.expectFunc = append(testCtx.expectFunc, f)

return testCtx
}

func Expect[IN, OUT, ERR Message](ctx *TestContext, in IN, out *OUT, err *ERR) error {
return ctx.
Input(in, EnvelopeHdr{}).
Expect(func(e *Envelope) error {
switch e.GetMsg().(type) {
default:
return ErrUnexpectedEnvelope
case OUT:
*out, _ = e.GetMsg().(OUT)
case ERR:
*err, _ = e.GetMsg().(ERR)
}

return nil
}).RunREST()
}

func (testCtx *TestContext) Run(stream bool) error {
ctx := newContext(&testCtx.ls)
conn := newTestConn()
conn.clientIP = testCtx.clientIP
conn.stream = stream
ctx.conn = conn
ctx.in = newEnvelope(ctx, conn, false)
ctx.in.
SetMsg(testCtx.inMsg).
SetHdrMap(testCtx.inHdr)
ctx.handlers = append(ctx.handlers, testCtx.handlers...)
ctx.Next()

return testCtx.expectFunc(conn.out...)
return testCtx.run(conn)
}

// RunREST simulates a REST request.
func (testCtx *TestContext) RunREST() error {
ctx := newContext(&testCtx.ls)
conn := newTestRESTConn()
conn.clientIP = testCtx.clientIP
conn.stream = false

return testCtx.run(conn)
}

func (testCtx *TestContext) run(conn Conn) error {
ctx := newContext(&testCtx.ls)
ctx.conn = conn
ctx.in = newEnvelope(ctx, conn, false)
ctx.in.
Expand All @@ -80,7 +108,32 @@ func (testCtx *TestContext) RunREST() error {
ctx.handlers = append(ctx.handlers, testCtx.handlers...)
ctx.Next()

return testCtx.expectFunc(conn.out...)
var out []*Envelope
switch conn := conn.(type) {
default:
panic("BUG! unknown conn type")
case *testRESTConn:
out = conn.out
case *testConn:
out = conn.out
}

if testCtx.receiverFunc != nil {
return testCtx.receiverFunc(out...)
}

if len(testCtx.expectFunc) > len(out) {
return ErrExpectationsDontMatch
}

for idx, o := range out {
err := testCtx.expectFunc[idx](o)
if err != nil {
return err
}
}

return nil
}

type testConn struct {
Expand Down
2 changes: 1 addition & 1 deletion rony/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand All @@ -21,6 +20,7 @@ linters:
- depguard
- perfsprint
- mnd
- recvcheck
fast: false
linters-settings:
errcheck:
Expand Down
2 changes: 1 addition & 1 deletion rony/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (cfg *serverConfig) allServiceDesc() []desc.ServiceDesc {
return svcs
}

func (cfg serverConfig) Gateway() kit.Gateway {
func (cfg *serverConfig) Gateway() kit.Gateway {
return fasthttp.MustNew(cfg.gatewayOpts...)
}

Expand Down
1 change: 0 additions & 1 deletion ronyup/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: false
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion std/clusters/rediscluster/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion std/gateways/fasthttp/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion std/gateways/fastws/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion std/gateways/silverhttp/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion stub/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down
1 change: 0 additions & 1 deletion testenv/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ linters:
enable-all: true
disable:
- wsl
- gomnd
- gochecknoglobals
- paralleltest
- gochecknoinits
Expand Down

0 comments on commit e5473e5

Please sign in to comment.