github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/edgec/ws_test.go (about) 1 package edgec_test 2 3 import ( 4 "context" 5 "flag" 6 "os" 7 "sync" 8 "testing" 9 "time" 10 11 "github.com/ronaksoft/rony" 12 "github.com/ronaksoft/rony/edgec" 13 "github.com/ronaksoft/rony/internal/testEnv" 14 "github.com/ronaksoft/rony/internal/testEnv/pb/service" 15 "github.com/ronaksoft/rony/log" 16 "github.com/ronaksoft/rony/registry" 17 "github.com/ronaksoft/rony/tools" 18 . "github.com/smartystreets/goconvey/convey" 19 ) 20 21 /* 22 Creation Time: 2020 - Jul - 17 23 Created by: (ehsan) 24 Maintainers: 25 1. Ehsan N. Moosa (E2) 26 Auditor: Ehsan N. Moosa (E2) 27 Copyright Ronak Software Group 2020 28 */ 29 30 func TestMain(m *testing.M) { 31 edgeServer := testEnv.EdgeServer("Adam", 8081, 1000) 32 rony.SetLogLevel(log.WarnLevel) 33 service.RegisterSample( 34 &service.Sample{ 35 ServerID: edgeServer.GetServerID(), 36 }, 37 edgeServer, 38 ) 39 40 edgeServer.Start() 41 42 flag.Parse() 43 code := m.Run() 44 edgeServer.Shutdown() 45 os.Exit(code) 46 } 47 48 func TestClient_Connect(t *testing.T) { 49 Convey("Websocket Client Tests", t, func(c C) { 50 Convey("One Connection With Concurrent Request", func(c C) { 51 wsc := edgec.NewWebsocket(edgec.WebsocketConfig{ 52 SeedHostPort: "127.0.0.1:8081", 53 Handler: func(m *rony.MessageEnvelope) { 54 c.Println("Received Uncaught Message", registry.C(m.Constructor)) 55 }, 56 }) 57 clnt := service.NewSampleClient("TestClient", wsc) 58 err := wsc.Start() 59 _, _ = c.Println(wsc.ConnInfo()) 60 c.So(err, ShouldBeNil) 61 62 wg := sync.WaitGroup{} 63 for i := 0; i < 200; i++ { 64 wg.Add(1) 65 go func() { 66 defer wg.Done() 67 x := tools.RandomInt64(0) 68 res, err := clnt.Echo(context.TODO(), &service.EchoRequest{Int: x}) 69 if err != nil { 70 c.Println(err.Error()) 71 } 72 // _ = res 73 c.So(err, ShouldBeNil) 74 c.So(res.Int, ShouldEqual, x) 75 }() 76 } 77 wg.Wait() 78 err = wsc.Close() 79 c.So(err, ShouldBeNil) 80 }) 81 Convey("One Connection With Slow Data-Rate", func(c C) { 82 wsc := edgec.NewWebsocket(edgec.WebsocketConfig{ 83 SeedHostPort: "127.0.0.1:8081", 84 }) 85 clnt := service.NewSampleClient("TestClient", wsc) 86 err := wsc.Start() 87 c.So(err, ShouldBeNil) 88 89 for i := 0; i < 5; i++ { 90 res, err := clnt.Echo(context.TODO(), &service.EchoRequest{Int: 123}) 91 c.So(err, ShouldBeNil) 92 c.So(res.Int, ShouldEqual, 123) 93 time.Sleep(time.Second * 2) 94 } 95 }) 96 Convey("One Connection With Slow Request", func(c C) { 97 wsc := edgec.NewWebsocket(edgec.WebsocketConfig{ 98 SeedHostPort: "127.0.0.1:8081", 99 }) 100 clnt := service.NewSampleClient("TestClient", wsc) 101 err := wsc.Start() 102 c.So(err, ShouldBeNil) 103 104 for i := 0; i < 5; i++ { 105 res, err := clnt.EchoDelay(context.TODO(), &service.EchoRequest{Int: 123}) 106 c.So(err, ShouldBeNil) 107 c.So(res.Int, ShouldEqual, 123) 108 } 109 }) 110 }) 111 }