go.etcd.io/etcd@v3.3.27+incompatible/proxy/grpcproxy/register_test.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grpcproxy
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/coreos/etcd/clientv3"
    22  	"github.com/coreos/etcd/clientv3/naming"
    23  	"github.com/coreos/etcd/integration"
    24  	"github.com/coreos/etcd/pkg/testutil"
    25  
    26  	gnaming "google.golang.org/grpc/naming"
    27  )
    28  
    29  func TestRegister(t *testing.T) {
    30  	defer testutil.AfterTest(t)
    31  
    32  	clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
    33  	defer clus.Terminate(t)
    34  	cli := clus.Client(0)
    35  	paddr := clus.Members[0].GRPCAddr()
    36  
    37  	testPrefix := "test-name"
    38  	wa := createWatcher(t, cli, testPrefix)
    39  	ups, err := wa.Next()
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	if len(ups) != 0 {
    44  		t.Fatalf("len(ups) expected 0, got %d (%v)", len(ups), ups)
    45  	}
    46  
    47  	donec := Register(cli, testPrefix, paddr, 5)
    48  
    49  	ups, err = wa.Next()
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	if len(ups) != 1 {
    54  		t.Fatalf("len(ups) expected 1, got %d (%v)", len(ups), ups)
    55  	}
    56  	if ups[0].Addr != paddr {
    57  		t.Fatalf("ups[0].Addr expected %q, got %q", paddr, ups[0].Addr)
    58  	}
    59  
    60  	cli.Close()
    61  	clus.TakeClient(0)
    62  	select {
    63  	case <-donec:
    64  	case <-time.After(5 * time.Second):
    65  		t.Fatal("donec 'register' did not return in time")
    66  	}
    67  }
    68  
    69  func createWatcher(t *testing.T, c *clientv3.Client, prefix string) gnaming.Watcher {
    70  	gr := &naming.GRPCResolver{Client: c}
    71  	watcher, err := gr.Resolve(prefix)
    72  	if err != nil {
    73  		t.Fatalf("failed to resolve %q (%v)", prefix, err)
    74  	}
    75  	return watcher
    76  }