]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: add up/down stress test
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Feb 2021 16:43:41 +0000 (17:43 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Feb 2021 16:43:41 +0000 (17:43 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
device/device_test.go

index 6b443cec7884ba740704c205f5e70fef69b04433..b7837711fb5a9ab1d401e2d56823a1d9a0f84620 100644 (file)
@@ -7,9 +7,11 @@ package device
 
 import (
        "bytes"
+       "encoding/hex"
        "errors"
        "fmt"
        "io/ioutil"
+       "math/rand"
        "net"
        "runtime"
        "runtime/pprof"
@@ -196,6 +198,39 @@ func TestTwoDevicePing(t *testing.T) {
        })
 }
 
+func TestUpDown(t *testing.T) {
+       goroutineLeakCheck(t)
+       const itrials = 200
+       const otrials = 10
+
+       for n := 0; n < otrials; n++ {
+               pair := genTestPair(t)
+               for i := range pair {
+                       for k := range pair[i].dev.peers.keyMap {
+                               pair[i].dev.IpcSet(fmt.Sprintf("public_key=%s\npersistent_keepalive_interval=1\n", hex.EncodeToString(k[:])))
+                       }
+               }
+               var wg sync.WaitGroup
+               wg.Add(len(pair))
+               for i := range pair {
+                       go func(d *Device) {
+                               defer wg.Done()
+                               for i := 0; i < itrials; i++ {
+                                       d.Up()
+                                       time.Sleep(time.Duration(rand.Intn(int(time.Nanosecond * (0x10000 - 1)))))
+                                       d.Down()
+                                       time.Sleep(time.Duration(rand.Intn(int(time.Nanosecond * (0x10000 - 1)))))
+                               }
+                       }(pair[i].dev)
+               }
+               wg.Wait()
+               for i := range pair {
+                       pair[i].dev.Up()
+                       pair[i].dev.Close()
+               }
+       }
+}
+
 // TestConcurrencySafety does other things concurrently with tunnel use.
 // It is intended to be used with the race detector to catch data races.
 func TestConcurrencySafety(t *testing.T) {