github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/sockopt_test.go (about)

     1  package internet_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  
     9  	"github.com/imannamdari/v2ray-core/v5/common"
    10  	"github.com/imannamdari/v2ray-core/v5/common/buf"
    11  	"github.com/imannamdari/v2ray-core/v5/testing/servers/tcp"
    12  	. "github.com/imannamdari/v2ray-core/v5/transport/internet"
    13  )
    14  
    15  func TestTCPFastOpen(t *testing.T) {
    16  	tcpServer := tcp.Server{
    17  		MsgProcessor: func(b []byte) []byte {
    18  			return b
    19  		},
    20  	}
    21  	dest, err := tcpServer.StartContext(context.Background(), &SocketConfig{Tfo: SocketConfig_Enable})
    22  	common.Must(err)
    23  	defer tcpServer.Close()
    24  
    25  	ctx := context.Background()
    26  	dialer := DefaultSystemDialer{}
    27  	conn, err := dialer.Dial(ctx, nil, dest, &SocketConfig{
    28  		Tfo: SocketConfig_Enable,
    29  	})
    30  	common.Must(err)
    31  	defer conn.Close()
    32  
    33  	_, err = conn.Write([]byte("abcd"))
    34  	common.Must(err)
    35  
    36  	b := buf.New()
    37  	common.Must2(b.ReadFrom(conn))
    38  	if r := cmp.Diff(b.Bytes(), []byte("abcd")); r != "" {
    39  		t.Fatal(r)
    40  	}
    41  }