File: client_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.13.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,784 kB
  • sloc: sh: 22; makefile: 11
file content (156 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package consul

import (
	"context"
	"errors"
	"io"
	"reflect"
	"testing"

	stdconsul "github.com/hashicorp/consul/api"

	"github.com/go-kit/kit/endpoint"
)

func TestClientRegistration(t *testing.T) {
	c := newTestClient(nil)

	services, _, err := c.Service(testRegistration.Name, "", true, &stdconsul.QueryOptions{})
	if err != nil {
		t.Error(err)
	}
	if want, have := 0, len(services); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	if err := c.Register(testRegistration); err != nil {
		t.Error(err)
	}

	if err := c.Register(testRegistration); err == nil {
		t.Errorf("want error, have %v", err)
	}

	services, _, err = c.Service(testRegistration.Name, "", true, &stdconsul.QueryOptions{})
	if err != nil {
		t.Error(err)
	}
	if want, have := 1, len(services); want != have {
		t.Errorf("want %d, have %d", want, have)
	}

	if err := c.Deregister(testRegistration); err != nil {
		t.Error(err)
	}

	if err := c.Deregister(testRegistration); err == nil {
		t.Errorf("want error, have %v", err)
	}

	services, _, err = c.Service(testRegistration.Name, "", true, &stdconsul.QueryOptions{})
	if err != nil {
		t.Error(err)
	}
	if want, have := 0, len(services); want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}

type testClient struct {
	entries []*stdconsul.ServiceEntry
}

func newTestClient(entries []*stdconsul.ServiceEntry) *testClient {
	return &testClient{
		entries: entries,
	}
}

var _ Client = &testClient{}

func (c *testClient) Service(service, tag string, _ bool, opts *stdconsul.QueryOptions) ([]*stdconsul.ServiceEntry, *stdconsul.QueryMeta, error) {
	var results []*stdconsul.ServiceEntry

	for _, entry := range c.entries {
		if entry.Service.Service != service {
			continue
		}
		if tag != "" {
			tagMap := map[string]struct{}{}

			for _, t := range entry.Service.Tags {
				tagMap[t] = struct{}{}
			}

			if _, ok := tagMap[tag]; !ok {
				continue
			}
		}

		results = append(results, entry)
	}

	return results, &stdconsul.QueryMeta{LastIndex: opts.WaitIndex}, nil
}

func (c *testClient) Register(r *stdconsul.AgentServiceRegistration) error {
	toAdd := registration2entry(r)

	for _, entry := range c.entries {
		if reflect.DeepEqual(*entry, *toAdd) {
			return errors.New("duplicate")
		}
	}

	c.entries = append(c.entries, toAdd)
	return nil
}

func (c *testClient) Deregister(r *stdconsul.AgentServiceRegistration) error {
	toDelete := registration2entry(r)

	var newEntries []*stdconsul.ServiceEntry
	for _, entry := range c.entries {
		if reflect.DeepEqual(*entry, *toDelete) {
			continue
		}
		newEntries = append(newEntries, entry)
	}
	if len(newEntries) == len(c.entries) {
		return errors.New("not found")
	}

	c.entries = newEntries
	return nil
}

func registration2entry(r *stdconsul.AgentServiceRegistration) *stdconsul.ServiceEntry {
	return &stdconsul.ServiceEntry{
		Node: &stdconsul.Node{
			Node:    "some-node",
			Address: r.Address,
		},
		Service: &stdconsul.AgentService{
			ID:      r.ID,
			Service: r.Name,
			Tags:    r.Tags,
			Port:    r.Port,
			Address: r.Address,
		},
		// Checks ignored
	}
}

func testFactory(instance string) (endpoint.Endpoint, io.Closer, error) {
	return func(context.Context, interface{}) (interface{}, error) {
		return instance, nil
	}, nil, nil
}

var testRegistration = &stdconsul.AgentServiceRegistration{
	ID:      "my-id",
	Name:    "my-name",
	Tags:    []string{"my-tag-1", "my-tag-2"},
	Port:    12345,
	Address: "my-address",
}