github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/integration/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  	clientv3 "github.com/lfch/etcd-io/client/v3"
    22  	"github.com/lfch/etcd-io/client/v3/naming/endpoints"
    23  	"github.com/lfch/etcd-io/server/v3/proxy/grpcproxy"
    24  	integration2 "github.com/lfch/etcd-io/tests/v3/framework/integration"
    25  	"go.uber.org/zap/zaptest"
    26  )
    27  
    28  func TestRegister(t *testing.T) {
    29  	integration2.BeforeTest(t)
    30  
    31  	clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 1})
    32  	defer clus.Terminate(t)
    33  	cli := clus.Client(0)
    34  	paddr := clus.Members[0].GRPCURL()
    35  
    36  	testPrefix := "test-name"
    37  	wa := mustCreateWatcher(t, cli, testPrefix)
    38  
    39  	donec := grpcproxy.Register(zaptest.NewLogger(t), cli, testPrefix, paddr, 5)
    40  
    41  	ups := <-wa
    42  	if len(ups) != 1 {
    43  		t.Fatalf("len(ups) expected 1, got %d (%v)", len(ups), ups)
    44  	}
    45  	if ups[0].Endpoint.Addr != paddr {
    46  		t.Fatalf("ups[0].Addr expected %q, got %q", paddr, ups[0].Endpoint.Addr)
    47  	}
    48  
    49  	cli.Close()
    50  	clus.TakeClient(0)
    51  	select {
    52  	case <-donec:
    53  	case <-time.After(5 * time.Second):
    54  		t.Fatal("donec 'register' did not return in time")
    55  	}
    56  }
    57  
    58  func mustCreateWatcher(t *testing.T, c *clientv3.Client, prefix string) endpoints.WatchChannel {
    59  	em, err := endpoints.NewManager(c, prefix)
    60  	if err != nil {
    61  		t.Fatalf("failed to create endpoints.Manager: %v", err)
    62  	}
    63  	wc, err := em.NewWatchChannel(c.Ctx())
    64  	if err != nil {
    65  		t.Fatalf("failed to resolve %q (%v)", prefix, err)
    66  	}
    67  	return wc
    68  }