1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-18 19:24:21 -04:00
v2fly/transport/internet/udp/dispatcher_test.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-02-04 17:40:10 -05:00
package udp_test
import (
"context"
"sync/atomic"
"testing"
"time"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2017-02-04 17:40:10 -05:00
. "v2ray.com/core/transport/internet/udp"
"v2ray.com/core/transport/ray"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2017-02-04 17:40:10 -05:00
)
type TestDispatcher struct {
OnDispatch func(ctx context.Context, dest net.Destination) (ray.InboundRay, error)
2017-02-04 17:40:10 -05:00
}
func (d *TestDispatcher) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
2017-02-04 17:40:10 -05:00
return d.OnDispatch(ctx, dest)
}
func TestSameDestinationDispatching(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-02-04 17:40:10 -05:00
ctx, cancel := context.WithCancel(context.Background())
link := ray.NewRay(ctx)
go func() {
for {
2017-11-09 16:33:15 -05:00
data, err := link.OutboundInput().ReadMultiBuffer()
2017-02-04 17:40:10 -05:00
if err != nil {
break
}
2017-11-09 16:33:15 -05:00
err = link.OutboundOutput().WriteMultiBuffer(data)
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-02-04 17:40:10 -05:00
}
}()
var count uint32
td := &TestDispatcher{
OnDispatch: func(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
2017-02-04 17:40:10 -05:00
atomic.AddUint32(&count, 1)
return link, nil
},
}
dest := net.UDPDestination(net.LocalHostIP, 53)
2017-02-04 17:40:10 -05:00
b := buf.New()
b.AppendBytes('a', 'b', 'c', 'd')
dispatcher := NewDispatcher(td)
var msgCount uint32
dispatcher.Dispatch(ctx, dest, b, func(payload *buf.Buffer) {
atomic.AddUint32(&msgCount, 1)
})
for i := 0; i < 5; i++ {
dispatcher.Dispatch(ctx, dest, b, func(payload *buf.Buffer) {})
}
time.Sleep(time.Second)
cancel()
2017-10-24 10:15:35 -04:00
assert(count, Equals, uint32(1))
assert(msgCount, Equals, uint32(6))
2017-02-04 17:40:10 -05:00
}