wireguard_test.go 1008 B

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