mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
clientv3: test closing client cancels blocking dials
This commit is contained in:
parent
ea2aae464d
commit
eaa8b9e155
@ -16,6 +16,7 @@ package clientv3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -25,6 +26,47 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestDialCancel(t *testing.T) {
|
||||||
|
defer testutil.AfterTest(t)
|
||||||
|
|
||||||
|
// accept first connection so client is created with dial timeout
|
||||||
|
ln, err := net.Listen("unix", "dialcancel:12345")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ln.Close()
|
||||||
|
|
||||||
|
ep := "unix://dialcancel:12345"
|
||||||
|
cfg := Config{
|
||||||
|
Endpoints: []string{ep},
|
||||||
|
DialTimeout: 30 * time.Second}
|
||||||
|
c, err := New(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// connect to ipv4 blackhole so dial blocks
|
||||||
|
c.SetEndpoints("http://254.0.0.1:12345")
|
||||||
|
|
||||||
|
// issue Get to force redial attempts
|
||||||
|
go c.Get(context.TODO(), "abc")
|
||||||
|
|
||||||
|
// wait a little bit so client close is after dial starts
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
donec := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(donec)
|
||||||
|
c.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatalf("failed to close")
|
||||||
|
case <-donec:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDialTimeout(t *testing.T) {
|
func TestDialTimeout(t *testing.T) {
|
||||||
defer testutil.AfterTest(t)
|
defer testutil.AfterTest(t)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user