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

"Refactor request parser and router dispatcher to include context parameter" #79

Merged
merged 3 commits into from
Jun 9, 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
8 changes: 6 additions & 2 deletions dispatch/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dispatch

import "github.com/ksysoev/wasabi"
import (
"context"

"github.com/ksysoev/wasabi"
)

// RequestMiddlewere is interface for request middleweres
type RequestMiddlewere func(next wasabi.RequestHandler) wasabi.RequestHandler
Expand All @@ -13,4 +17,4 @@ func (f RequestHandlerFunc) Handle(conn wasabi.Connection, req wasabi.Request) e
return f(conn, req)
}

type RequestParser func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request
type RequestParser func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request
2 changes: 1 addition & 1 deletion dispatch/router_dipatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *RouterDispatcher) AddBackend(backend wasabi.RequestHandler, routingKeys
// determining the appropriate backend, and handling the request using middleware.
// If an error occurs during handling, it is logged.
func (d *RouterDispatcher) Dispatch(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) {
req := d.parser(conn, msgType, data)
req := d.parser(conn, conn.Context(), msgType, data)

if req == nil {
return
Expand Down
33 changes: 25 additions & 8 deletions dispatch/router_dipatcher_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dispatch

import (
"context"
"fmt"
"testing"

Expand All @@ -11,7 +12,7 @@ import (
func TestNewRouterDispatcher(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)

parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}

Expand All @@ -27,7 +28,7 @@ func TestNewRouterDispatcher(t *testing.T) {
}
func TestRouterDispatcher_AddBackend(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand Down Expand Up @@ -62,10 +63,14 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)

req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req }
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)

conn := mocks.NewMockConnection(t)
conn.EXPECT().Context().Return(context.Background())

data := []byte("test data")

// Test case 1: Request with existing routing key
Expand All @@ -80,10 +85,14 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) {
func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req }
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)

conn := mocks.NewMockConnection(t)
conn.EXPECT().Context().Return(context.Background())

data := []byte("test data")

// Test case 1: Request with existing routing key
Expand All @@ -99,10 +108,14 @@ func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) {

func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return nil }
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return nil
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)

conn := mocks.NewMockConnection(t)
conn.EXPECT().Context().Return(context.Background())

data := []byte("test data")

dispatcher.Dispatch(conn, wasabi.MsgTypeText, data)
Expand All @@ -111,10 +124,14 @@ func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) {
func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request { return req }
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)

conn := mocks.NewMockConnection(t)
conn.EXPECT().Context().Return(context.Background())

data := []byte("test data")

routingKey := "key1"
Expand All @@ -128,7 +145,7 @@ func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) {
}
func TestRouterDispatcher_Use(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -149,7 +166,7 @@ func TestRouterDispatcher_UseMiddleware(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
defaultBackend.EXPECT().Handle(mockConn, mockReq).Return(testError)

parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand Down
4 changes: 2 additions & 2 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func main() {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

Expand Down
4 changes: 2 additions & 2 deletions examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func main() {
return httpReq, nil
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})

channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))
Expand Down
2 changes: 1 addition & 1 deletion examples/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
},
)

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request {
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))
Expand Down
4 changes: 2 additions & 2 deletions tests/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestEcho(t *testing.T) {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})
ch := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

Expand Down
Loading