github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/tunnel/udp/tunnel_test.go (about) 1 package udpTunnel_test 2 3 import ( 4 "bufio" 5 "net" 6 "testing" 7 "time" 8 9 "github.com/ronaksoft/rony" 10 "github.com/ronaksoft/rony/internal/msg" 11 udpTunnel "github.com/ronaksoft/rony/internal/tunnel/udp" 12 "github.com/ronaksoft/rony/pools" 13 "github.com/ronaksoft/rony/tools" 14 . "github.com/smartystreets/goconvey/convey" 15 ) 16 17 /* 18 Creation Time: 2021 - Jan - 06 19 Created by: (ehsan) 20 Maintainers: 21 1. Ehsan N. Moosa (E2) 22 Auditor: Ehsan N. Moosa (E2) 23 Copyright Ronak Software Group 2020 24 */ 25 26 func TestNewTunnel(t *testing.T) { 27 Convey("Tunnel", t, func(c C) { 28 hostPort := "127.0.0.1:8080" 29 t, err := udpTunnel.New(udpTunnel.Config{ 30 ListenAddress: hostPort, 31 ExternalAddrs: []string{hostPort}, 32 }) 33 c.So(err, ShouldBeNil) 34 t.MessageHandler = func(conn rony.Conn, tm *msg.TunnelMessage) { 35 b, _ := tm.Marshal() 36 err := conn.WriteBinary(0, b) 37 c.So(err, ShouldBeNil) 38 } 39 t.Start() 40 defer t.Shutdown() 41 42 Convey("Send Data", func(c C) { 43 for i := 0; i < 10; i++ { 44 conn, err := net.Dial("udp", hostPort) 45 c.So(err, ShouldBeNil) 46 47 req := &msg.TunnelMessage{ 48 SenderID: []byte("SomeSender"), 49 SenderReplicaSet: tools.RandomUint64(1000), 50 Store: nil, 51 Envelope: nil, 52 } 53 res := &msg.TunnelMessage{} 54 out, _ := req.Marshal() 55 n, err := conn.Write(out) 56 c.So(err, ShouldBeNil) 57 c.So(n, ShouldEqual, len(out)) 58 p := make([]byte, 2048) 59 n, err = bufio.NewReader(conn).Read(p) 60 c.So(err, ShouldBeNil) 61 c.So(n, ShouldEqual, len(out)) 62 err = res.Unmarshal(p[:n]) 63 c.So(err, ShouldBeNil) 64 c.So(res.SenderID, ShouldResemble, req.SenderID) 65 c.So(res.SenderReplicaSet, ShouldEqual, req.SenderReplicaSet) 66 err = conn.Close() 67 c.So(err, ShouldBeNil) 68 time.Sleep(time.Second) 69 } 70 }) 71 Convey("Send Concurrent Connection", func(c C) { 72 wg := pools.AcquireWaitGroup() 73 for i := 0; i < 200; i++ { 74 wg.Add(1) 75 go func() { 76 defer wg.Done() 77 conn, err := net.Dial("udp", hostPort) 78 c.So(err, ShouldBeNil) 79 80 req := &msg.TunnelMessage{ 81 SenderID: []byte("SomeSender"), 82 SenderReplicaSet: tools.RandomUint64(1000), 83 Store: nil, 84 Envelope: nil, 85 } 86 res := &msg.TunnelMessage{} 87 out, _ := req.Marshal() 88 n, err := conn.Write(out) 89 c.So(err, ShouldBeNil) 90 c.So(n, ShouldEqual, len(out)) 91 p := make([]byte, 2048) 92 n, err = bufio.NewReader(conn).Read(p) 93 c.So(err, ShouldBeNil) 94 c.So(n, ShouldEqual, len(out)) 95 err = res.Unmarshal(p[:n]) 96 c.So(err, ShouldBeNil) 97 c.So(res.SenderID, ShouldResemble, req.SenderID) 98 c.So(res.SenderReplicaSet, ShouldEqual, req.SenderReplicaSet) 99 err = conn.Close() 100 c.So(err, ShouldBeNil) 101 }() 102 } 103 wg.Wait() 104 pools.ReleaseWaitGroup(wg) 105 }) 106 Convey("Send Concurrent Connection and Data", func(c C) { 107 wg := pools.AcquireWaitGroup() 108 for i := 0; i < 200; i++ { 109 wg.Add(1) 110 go func() { 111 defer wg.Done() 112 conn, err := net.Dial("udp", hostPort) 113 c.So(err, ShouldBeNil) 114 115 for j := 0; j < 10; j++ { 116 req := &msg.TunnelMessage{ 117 SenderID: []byte("SomeSender"), 118 SenderReplicaSet: tools.RandomUint64(1000), 119 Store: nil, 120 Envelope: nil, 121 } 122 res := &msg.TunnelMessage{} 123 out, _ := req.Marshal() 124 n, err := conn.Write(out) 125 c.So(err, ShouldBeNil) 126 c.So(n, ShouldEqual, len(out)) 127 p := make([]byte, 2048) 128 n, err = bufio.NewReader(conn).Read(p) 129 c.So(err, ShouldBeNil) 130 c.So(n, ShouldEqual, len(out)) 131 err = res.Unmarshal(p[:n]) 132 c.So(err, ShouldBeNil) 133 c.So(res.SenderID, ShouldResemble, req.SenderID) 134 c.So(res.SenderReplicaSet, ShouldEqual, req.SenderReplicaSet) 135 } 136 err = conn.Close() 137 c.So(err, ShouldBeNil) 138 }() 139 } 140 wg.Wait() 141 pools.ReleaseWaitGroup(wg) 142 }) 143 }) 144 } 145 146 func BenchmarkNew(b *testing.B) { 147 t, err := udpTunnel.New(udpTunnel.Config{ 148 ServerID: "", 149 ListenAddress: "127.0.0.1:2374", 150 MaxBodySize: 1000, 151 ExternalAddrs: nil, 152 }) 153 if err != nil { 154 b.Fatal(err) 155 } 156 157 tm := &msg.TunnelMessage{ 158 SenderID: []byte("Something"), 159 SenderReplicaSet: 123, 160 Store: nil, 161 Envelope: &rony.MessageEnvelope{ 162 Constructor: 2193123, 163 RequestID: 42342342, 164 Message: []byte("something"), 165 Auth: nil, 166 Header: nil, 167 }, 168 } 169 tmb, _ := tm.Marshal() 170 t.MessageHandler = func(conn rony.Conn, tm *msg.TunnelMessage) { 171 err := conn.WriteBinary(0, tmb) 172 if err != nil { 173 panic(err) 174 } 175 } 176 t.Start() 177 b.ReportAllocs() 178 b.RunParallel(func(pb *testing.PB) { 179 for pb.Next() { 180 c, err := net.Dial("udp", "127.0.0.1:2374") 181 if err != nil { 182 b.Fatal(err) 183 } 184 _, err = c.Write(tmb) 185 if err != nil { 186 b.Fatal(err) 187 } 188 _, err = c.Read(tmb) 189 if err != nil { 190 b.Fatal(err) 191 } 192 _ = c.Close() 193 } 194 }) 195 }