2019-12-05 18:50:32 +03:00
|
|
|
// +build !race
|
|
|
|
|
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReconnectTunnel(t *testing.T) {
|
2019-12-08 15:12:20 +03:00
|
|
|
// we manually override the tunnel.ReconnectTime value here
|
|
|
|
// this is so that we make the reconnects faster than the default 5s
|
|
|
|
ReconnectTime = 100 * time.Millisecond
|
|
|
|
|
2019-12-05 18:50:32 +03:00
|
|
|
// create a new tunnel client
|
|
|
|
tunA := NewTunnel(
|
2019-12-08 15:12:20 +03:00
|
|
|
Address("127.0.0.1:9098"),
|
|
|
|
Nodes("127.0.0.1:9099"),
|
2019-12-05 18:50:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// create a new tunnel server
|
|
|
|
tunB := NewTunnel(
|
2019-12-08 15:12:20 +03:00
|
|
|
Address("127.0.0.1:9099"),
|
2019-12-05 18:50:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// start tunnel
|
|
|
|
err := tunB.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunB.Close()
|
|
|
|
|
|
|
|
// start tunnel
|
|
|
|
err = tunA.Connect()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer tunA.Close()
|
|
|
|
|
|
|
|
wait := make(chan bool)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
// start tunnel listener
|
|
|
|
go testBrokenTunAccept(t, tunB, wait, &wg)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
// start tunnel sender
|
2019-12-08 15:12:20 +03:00
|
|
|
go testBrokenTunSend(t, tunA, wait, &wg, ReconnectTime)
|
2019-12-05 18:50:32 +03:00
|
|
|
|
|
|
|
// wait until done
|
|
|
|
wg.Wait()
|
|
|
|
}
|