-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fastws] fix reading very big messages
- Loading branch information
Showing
9 changed files
with
208 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package testenv | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"ronykit/testenv/services" | ||
|
||
"github.com/clubpay/ronykit/kit/utils" | ||
|
||
"github.com/clubpay/ronykit/std/gateways/fastws" | ||
|
||
"github.com/clubpay/ronykit/kit" | ||
"github.com/clubpay/ronykit/kit/stub" | ||
. "github.com/smartystreets/goconvey/convey" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func TestFastWS(t *testing.T) { | ||
Convey("Kit with FastWS", t, func(c C) { | ||
testCases := map[string]func(t *testing.T, opt fx.Option) func(c C){ | ||
"Edger Server With Huge Websocket Payload": fastwsWithHugePayload, | ||
} | ||
for title, fn := range testCases { | ||
Convey(title, | ||
fn( | ||
t, invokeEdgeServerWithFastWS(8082, services.EchoService), | ||
), | ||
) | ||
} | ||
}) | ||
} | ||
|
||
func invokeEdgeServerWithFastWS(port int, desc ...kit.ServiceDescriptor) fx.Option { | ||
return fx.Invoke( | ||
func(lc fx.Lifecycle) { | ||
edge := kit.NewServer( | ||
kit.WithLogger(&stdLogger{}), | ||
kit.WithErrorHandler( | ||
func(ctx *kit.Context, err error) { | ||
fmt.Println("EdgeError: ", err) | ||
}, | ||
), | ||
kit.WithGateway( | ||
fastws.MustNew( | ||
fastws.WithPredicateKey("cmd"), | ||
fastws.Listen(fmt.Sprintf("tcp4://0.0.0.0:%d", port)), | ||
), | ||
), | ||
kit.WithServiceDesc(desc...), | ||
) | ||
|
||
lc.Append( | ||
fx.Hook{ | ||
OnStart: func(ctx context.Context) error { | ||
edge.Start(ctx) | ||
|
||
return nil | ||
}, | ||
OnStop: func(ctx context.Context) error { | ||
edge.Shutdown(ctx) | ||
|
||
return nil | ||
}, | ||
}, | ||
) | ||
}, | ||
) | ||
} | ||
|
||
func fastwsWithHugePayload(t *testing.T, opt fx.Option) func(c C) { | ||
ctx := context.Background() | ||
|
||
return func(c C) { | ||
Prepare( | ||
t, c, | ||
fx.Options( | ||
opt, | ||
), | ||
) | ||
|
||
time.Sleep(time.Second * 5) | ||
|
||
wsCtx := stub.New("localhost:8082"). | ||
Websocket( | ||
stub.WithPredicateKey("cmd"), | ||
) | ||
c.So(wsCtx.Connect(ctx, "/"), ShouldBeNil) | ||
|
||
req := &services.EchoRequest{Input: utils.RandomID(12000)} | ||
res := &services.EchoResponse{} | ||
err := wsCtx.BinaryMessage( | ||
ctx, "echo", req, res, | ||
func(ctx context.Context, msg kit.Message, hdr stub.Header, err error) { | ||
c.So(err, ShouldBeNil) | ||
c.So(msg.(*services.EchoResponse).Output, ShouldEqual, req.Input) //nolint:forcetypeassert | ||
}, | ||
) | ||
c.So(err, ShouldBeNil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package services | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/clubpay/ronykit/kit" | ||
"github.com/clubpay/ronykit/kit/desc" | ||
"github.com/clubpay/ronykit/std/gateways/fastws" | ||
) | ||
|
||
type EchoRequest struct { | ||
Input string `json:"input"` | ||
} | ||
|
||
type EchoResponse struct { | ||
Output string `json:"output"` | ||
} | ||
|
||
var EchoService kit.ServiceDescriptor = desc.NewService("EchoService"). | ||
AddContract( | ||
desc.NewContract(). | ||
SetInput(&EchoRequest{}). | ||
SetOutput(&EchoResponse{}). | ||
Selector(fastws.RPC("echo")). | ||
SetHandler( | ||
contextMW(10*time.Second), | ||
func(ctx *kit.Context) { | ||
req, _ := ctx.In().GetMsg().(*EchoRequest) | ||
|
||
ctx.Out(). | ||
SetMsg(&EchoResponse{Output: req.Input}). | ||
Send() | ||
}), | ||
) |