github.com/metacubex/mihomo@v1.18.5/adapter/outbound/wireguard_test.go (about)

     1  //go:build with_gvisor
     2  
     3  package outbound
     4  
     5  import (
     6  	"context"
     7  	"runtime"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestWireGuardGC(t *testing.T) {
    13  	option := WireGuardOption{}
    14  	option.Server = "162.159.192.1"
    15  	option.Port = 2408
    16  	option.PrivateKey = "iOx7749AdqH3IqluG7+0YbGKd0m1mcEXAfGRzpy9rG8="
    17  	option.PublicKey = "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo="
    18  	option.Ip = "172.16.0.2"
    19  	option.Ipv6 = "2606:4700:110:8d29:be92:3a6a:f4:c437"
    20  	option.Reserved = []uint8{51, 69, 125}
    21  	wg, err := NewWireGuard(option)
    22  	if err != nil {
    23  		t.Error(err)
    24  	}
    25  	closeCh := make(chan struct{})
    26  	wg.closeCh = closeCh
    27  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    28  	defer cancel()
    29  	err = wg.init(ctx)
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  	// must do a small sleep before test GC
    34  	// because it maybe deadlocks if w.device.Close call too fast after w.device.Start
    35  	time.Sleep(10 * time.Millisecond)
    36  	wg = nil
    37  	runtime.GC()
    38  	select {
    39  	case <-closeCh:
    40  		return
    41  	case <-ctx.Done():
    42  		t.Error("timeout not GC")
    43  	}
    44  }