...

Source file src/github.com/go-kit/kit/sd/etcdv3/instancer_test.go

Documentation: github.com/go-kit/kit/sd/etcdv3

     1  package etcdv3
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/go-kit/kit/sd"
     8  	"github.com/go-kit/log"
     9  )
    10  
    11  var _ sd.Instancer = (*Instancer)(nil) // API check
    12  
    13  type testKV struct {
    14  	Key   []byte
    15  	Value []byte
    16  }
    17  
    18  type testResponse struct {
    19  	Kvs []testKV
    20  }
    21  
    22  var (
    23  	fakeResponse = testResponse{
    24  		Kvs: []testKV{
    25  			{
    26  				Key:   []byte("/foo/1"),
    27  				Value: []byte("1:1"),
    28  			},
    29  			{
    30  				Key:   []byte("/foo/2"),
    31  				Value: []byte("2:2"),
    32  			},
    33  		},
    34  	}
    35  )
    36  
    37  var _ sd.Instancer = &Instancer{} // API check
    38  
    39  func TestInstancer(t *testing.T) {
    40  	client := &fakeClient{
    41  		responses: map[string]testResponse{"/foo": fakeResponse},
    42  	}
    43  
    44  	s, err := NewInstancer(client, "/foo", log.NewNopLogger())
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	defer s.Stop()
    49  
    50  	if state := s.cache.State(); state.Err != nil {
    51  		t.Fatal(state.Err)
    52  	}
    53  }
    54  
    55  type fakeClient struct {
    56  	responses map[string]testResponse
    57  }
    58  
    59  func (c *fakeClient) GetEntries(prefix string) ([]string, error) {
    60  	response, ok := c.responses[prefix]
    61  	if !ok {
    62  		return nil, errors.New("key not exist")
    63  	}
    64  
    65  	entries := make([]string, len(response.Kvs))
    66  	for i, node := range response.Kvs {
    67  		entries[i] = string(node.Value)
    68  	}
    69  	return entries, nil
    70  }
    71  
    72  func (c *fakeClient) WatchPrefix(prefix string, ch chan struct{}) {
    73  }
    74  
    75  func (c *fakeClient) LeaseID() int64 {
    76  	return 0
    77  }
    78  
    79  func (c *fakeClient) Register(Service) error {
    80  	return nil
    81  }
    82  func (c *fakeClient) Deregister(Service) error {
    83  	return nil
    84  }
    85  

View as plain text