You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/udp/dispatcher_test.go

66 lines
1.5 KiB

8 years ago
package udp_test
import (
"context"
"sync/atomic"
"testing"
"time"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
8 years ago
. "v2ray.com/core/transport/internet/udp"
"v2ray.com/core/transport/ray"
. "v2ray.com/ext/assert"
8 years ago
)
type TestDispatcher struct {
OnDispatch func(ctx context.Context, dest net.Destination) (ray.InboundRay, error)
8 years ago
}
func (d *TestDispatcher) Dispatch(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
8 years ago
return d.OnDispatch(ctx, dest)
}
func TestSameDestinationDispatching(t *testing.T) {
assert := With(t)
8 years ago
ctx, cancel := context.WithCancel(context.Background())
link := ray.NewRay(ctx)
go func() {
for {
data, err := link.OutboundInput().ReadMultiBuffer()
8 years ago
if err != nil {
break
}
err = link.OutboundOutput().WriteMultiBuffer(data)
assert(err, IsNil)
8 years ago
}
}()
var count uint32
td := &TestDispatcher{
OnDispatch: func(ctx context.Context, dest net.Destination) (ray.InboundRay, error) {
8 years ago
atomic.AddUint32(&count, 1)
return link, nil
},
}
dest := net.UDPDestination(net.LocalHostIP, 53)
8 years ago
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()
assert(count, Equals, uint32(1))
assert(msgCount, Equals, uint32(6))
8 years ago
}