1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/common/mux/client_test.go

117 lines
2.7 KiB
Go
Raw Normal View History

2018-10-24 16:37:11 -04:00
package mux_test
import (
2018-10-25 03:32:03 -04:00
"context"
2018-10-24 16:37:11 -04:00
"testing"
2018-10-25 03:32:03 -04:00
"time"
2018-10-24 16:37:11 -04:00
2018-10-25 03:32:03 -04:00
"github.com/golang/mock/gomock"
"v2ray.com/core/common"
2018-10-24 16:37:11 -04:00
"v2ray.com/core/common/errors"
"v2ray.com/core/common/mux"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
2018-10-25 03:32:03 -04:00
"v2ray.com/core/testing/mocks"
2018-11-03 08:11:37 -04:00
"v2ray.com/core/transport"
2018-10-25 03:32:03 -04:00
"v2ray.com/core/transport/pipe"
2018-10-24 16:37:11 -04:00
)
func TestIncrementalPickerFailure(t *testing.T) {
2018-10-25 03:32:03 -04:00
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
mockWorkerFactory := mocks.NewMuxClientWorkerFactory(mockCtl)
mockWorkerFactory.EXPECT().Create().Return(nil, errors.New("test"))
2018-10-24 16:37:11 -04:00
picker := mux.IncrementalWorkerPicker{
2018-10-25 03:32:03 -04:00
Factory: mockWorkerFactory,
2018-10-24 16:37:11 -04:00
}
_, err := picker.PickAvailable()
if err == nil {
t.Error("expected error, but nil")
}
}
2018-10-25 03:32:03 -04:00
func TestClientWorkerEOF(t *testing.T) {
reader, writer := pipe.New(pipe.WithoutSizeLimit())
common.Must(writer.Close())
2018-11-03 07:36:29 -04:00
worker, err := mux.NewClientWorker(transport.Link{Reader: reader, Writer: writer}, mux.ClientStrategy{})
2018-10-25 03:32:03 -04:00
common.Must(err)
time.Sleep(time.Millisecond * 500)
f := worker.Dispatch(context.Background(), nil)
if f {
t.Error("expected failed dispatching, but actually not")
}
}
func TestClientWorkerClose(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
r1, w1 := pipe.New(pipe.WithoutSizeLimit())
worker1, err := mux.NewClientWorker(transport.Link{
Reader: r1,
Writer: w1,
}, mux.ClientStrategy{
MaxConcurrency: 4,
MaxConnection: 4,
})
common.Must(err)
r2, w2 := pipe.New(pipe.WithoutSizeLimit())
worker2, err := mux.NewClientWorker(transport.Link{
Reader: r2,
Writer: w2,
}, mux.ClientStrategy{
MaxConcurrency: 4,
MaxConnection: 4,
})
common.Must(err)
factory := mocks.NewMuxClientWorkerFactory(mockCtl)
gomock.InOrder(
factory.EXPECT().Create().Return(worker1, nil),
factory.EXPECT().Create().Return(worker2, nil),
)
picker := &mux.IncrementalWorkerPicker{
Factory: factory,
}
manager := &mux.ClientManager{
Picker: picker,
}
tr1, tw1 := pipe.New(pipe.WithoutSizeLimit())
ctx1 := session.ContextWithOutbound(context.Background(), &session.Outbound{
Target: net.TCPDestination(net.DomainAddress("www.v2ray.com"), 80),
})
common.Must(manager.Dispatch(ctx1, &transport.Link{
Reader: tr1,
Writer: tw1,
}))
defer tw1.Close()
common.Must(w1.Close())
time.Sleep(time.Millisecond * 500)
if !worker1.Closed() {
t.Error("worker1 is not finished")
}
tr2, tw2 := pipe.New(pipe.WithoutSizeLimit())
ctx2 := session.ContextWithOutbound(context.Background(), &session.Outbound{
Target: net.TCPDestination(net.DomainAddress("www.v2ray.com"), 80),
})
common.Must(manager.Dispatch(ctx2, &transport.Link{
Reader: tr2,
Writer: tw2,
}))
defer tw2.Close()
common.Must(w2.Close())
}