From 10c971db702c2418cb8f64a1ab2239a7cebe175b Mon Sep 17 00:00:00 2001 From: Xiang Date: Thu, 26 Oct 2017 10:05:01 -0700 Subject: [PATCH] clientv3/integration: add put blackhole test --- clientv3/integration/black_hole_test.go | 74 +++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 clientv3/integration/black_hole_test.go diff --git a/clientv3/integration/black_hole_test.go b/clientv3/integration/black_hole_test.go new file mode 100644 index 00000000000..1f4c074138f --- /dev/null +++ b/clientv3/integration/black_hole_test.go @@ -0,0 +1,74 @@ +// Copyright 2017 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !cluster_proxy + +package integration + +import ( + "context" + "testing" + "time" + + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/integration" + "github.com/coreos/etcd/pkg/testutil" +) + +func TestBlackholePutWithoutKeealiveEnabled(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{ + Size: 2, + GRPCKeepAliveMinTime: 1 * time.Millisecond, + SkipCreatingClient: true}, + ) // avoid too_many_pings + + defer clus.Terminate(t) + + ccfg := clientv3.Config{ + Endpoints: []string{clus.Members[0].GRPCAddr()}, + DialTimeout: 1 * time.Second, + } + cli, err := clientv3.New(ccfg) + if err != nil { + t.Fatal(err) + } + defer cli.Close() + + // wait for eps[0] to be pinned + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + _, err = cli.Get(ctx, "foo") + cancel() + if err != nil { + t.Fatal(err) + } + + cli.SetEndpoints(clus.Members[0].GRPCAddr(), clus.Members[1].GRPCAddr()) + clus.Members[0].Blackhole() + + // fail first due to blackhole, retry should succeed + // when gRPC supports better retry on non-delivered request, the first put can succeed. + ctx, c1 := context.WithTimeout(context.Background(), time.Second) + defer c1() + if _, err = cli.Put(ctx, "foo", "bar"); err != context.DeadlineExceeded { + t.Fatalf("err = %v, want %v", err, context.DeadlineExceeded) + } + + ctx, c2 := context.WithTimeout(context.Background(), time.Second) + defer c2() + if _, err = cli.Put(ctx, "foo", "bar"); err != nil { + t.Errorf("put failed with error %v", err) + } +}