github.com/v2fly/v2ray-core/v4@v4.45.2/common/mux/client_test.go (about) 1 package mux_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/golang/mock/gomock" 9 10 "github.com/v2fly/v2ray-core/v4/common" 11 "github.com/v2fly/v2ray-core/v4/common/errors" 12 "github.com/v2fly/v2ray-core/v4/common/mux" 13 "github.com/v2fly/v2ray-core/v4/common/net" 14 "github.com/v2fly/v2ray-core/v4/common/session" 15 "github.com/v2fly/v2ray-core/v4/testing/mocks" 16 "github.com/v2fly/v2ray-core/v4/transport" 17 "github.com/v2fly/v2ray-core/v4/transport/pipe" 18 ) 19 20 func TestIncrementalPickerFailure(t *testing.T) { 21 mockCtl := gomock.NewController(t) 22 defer mockCtl.Finish() 23 24 mockWorkerFactory := mocks.NewMuxClientWorkerFactory(mockCtl) 25 mockWorkerFactory.EXPECT().Create().Return(nil, errors.New("test")) 26 27 picker := mux.IncrementalWorkerPicker{ 28 Factory: mockWorkerFactory, 29 } 30 31 _, err := picker.PickAvailable() 32 if err == nil { 33 t.Error("expected error, but nil") 34 } 35 } 36 37 func TestClientWorkerEOF(t *testing.T) { 38 reader, writer := pipe.New(pipe.WithoutSizeLimit()) 39 common.Must(writer.Close()) 40 41 worker, err := mux.NewClientWorker(transport.Link{Reader: reader, Writer: writer}, mux.ClientStrategy{}) 42 common.Must(err) 43 44 time.Sleep(time.Millisecond * 500) 45 46 f := worker.Dispatch(context.Background(), nil) 47 if f { 48 t.Error("expected failed dispatching, but actually not") 49 } 50 } 51 52 func TestClientWorkerClose(t *testing.T) { 53 mockCtl := gomock.NewController(t) 54 defer mockCtl.Finish() 55 56 r1, w1 := pipe.New(pipe.WithoutSizeLimit()) 57 worker1, err := mux.NewClientWorker(transport.Link{ 58 Reader: r1, 59 Writer: w1, 60 }, mux.ClientStrategy{ 61 MaxConcurrency: 4, 62 MaxConnection: 4, 63 }) 64 common.Must(err) 65 66 r2, w2 := pipe.New(pipe.WithoutSizeLimit()) 67 worker2, err := mux.NewClientWorker(transport.Link{ 68 Reader: r2, 69 Writer: w2, 70 }, mux.ClientStrategy{ 71 MaxConcurrency: 4, 72 MaxConnection: 4, 73 }) 74 common.Must(err) 75 76 factory := mocks.NewMuxClientWorkerFactory(mockCtl) 77 gomock.InOrder( 78 factory.EXPECT().Create().Return(worker1, nil), 79 factory.EXPECT().Create().Return(worker2, nil), 80 ) 81 82 picker := &mux.IncrementalWorkerPicker{ 83 Factory: factory, 84 } 85 manager := &mux.ClientManager{ 86 Picker: picker, 87 } 88 89 tr1, tw1 := pipe.New(pipe.WithoutSizeLimit()) 90 ctx1 := session.ContextWithOutbound(context.Background(), &session.Outbound{ 91 Target: net.TCPDestination(net.DomainAddress("www.v2fly.org"), 80), 92 }) 93 common.Must(manager.Dispatch(ctx1, &transport.Link{ 94 Reader: tr1, 95 Writer: tw1, 96 })) 97 defer tw1.Close() 98 99 common.Must(w1.Close()) 100 101 time.Sleep(time.Millisecond * 500) 102 if !worker1.Closed() { 103 t.Error("worker1 is not finished") 104 } 105 106 tr2, tw2 := pipe.New(pipe.WithoutSizeLimit()) 107 ctx2 := session.ContextWithOutbound(context.Background(), &session.Outbound{ 108 Target: net.TCPDestination(net.DomainAddress("www.v2fly.org"), 80), 109 }) 110 common.Must(manager.Dispatch(ctx2, &transport.Link{ 111 Reader: tr2, 112 Writer: tw2, 113 })) 114 defer tw2.Close() 115 116 common.Must(w2.Close()) 117 }