File: client_test.go

package info (click to toggle)
golang-github-go-kit-kit 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, experimental, sid
  • size: 4,848 kB
  • sloc: sh: 65; makefile: 14
file content (157 lines) | stat: -rw-r--r-- 3,942 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
157
package zk

import (
	"bytes"
	"testing"
	"time"

	stdzk "github.com/samuel/go-zookeeper/zk"

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

func TestNewClient(t *testing.T) {
	var (
		acl            = stdzk.WorldACL(stdzk.PermRead)
		connectTimeout = 3 * time.Second
		sessionTimeout = 20 * time.Second
		payload        = [][]byte{[]byte("Payload"), []byte("Test")}
	)

	c, err := NewClient(
		[]string{"FailThisInvalidHost!!!"},
		log.NewNopLogger(),
	)
	if err == nil {
		t.Errorf("expected error, got nil")
	}

	hasFired := false
	calledEventHandler := make(chan struct{})
	eventHandler := func(event stdzk.Event) {
		if !hasFired {
			// test is successful if this function has fired at least once
			hasFired = true
			close(calledEventHandler)
		}
	}

	c, err = NewClient(
		[]string{"localhost"},
		log.NewNopLogger(),
		ACL(acl),
		ConnectTimeout(connectTimeout),
		SessionTimeout(sessionTimeout),
		Payload(payload),
		EventHandler(eventHandler),
	)
	if err != nil {
		t.Fatal(err)
	}
	defer c.Stop()

	clientImpl, ok := c.(*client)
	if !ok {
		t.Fatal("retrieved incorrect Client implementation")
	}
	if want, have := acl, clientImpl.acl; want[0] != have[0] {
		t.Errorf("want %+v, have %+v", want, have)
	}
	if want, have := connectTimeout, clientImpl.connectTimeout; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	if want, have := sessionTimeout, clientImpl.sessionTimeout; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
	if want, have := payload, clientImpl.rootNodePayload; bytes.Compare(want[0], have[0]) != 0 || bytes.Compare(want[1], have[1]) != 0 {
		t.Errorf("want %s, have %s", want, have)
	}

	select {
	case <-calledEventHandler:
	case <-time.After(100 * time.Millisecond):
		t.Errorf("event handler never called")
	}
}

func TestOptions(t *testing.T) {
	_, err := NewClient([]string{"localhost"}, log.NewNopLogger(), Credentials("valid", "credentials"))
	if err != nil && err != stdzk.ErrNoServer {
		t.Errorf("unexpected error: %v", err)
	}

	_, err = NewClient([]string{"localhost"}, log.NewNopLogger(), Credentials("nopass", ""))
	if want, have := err, ErrInvalidCredentials; want != have {
		t.Errorf("want %v, have %v", want, have)
	}

	_, err = NewClient([]string{"localhost"}, log.NewNopLogger(), ConnectTimeout(0))
	if err == nil {
		t.Errorf("expected connect timeout error")
	}

	_, err = NewClient([]string{"localhost"}, log.NewNopLogger(), SessionTimeout(0))
	if err == nil {
		t.Errorf("expected connect timeout error")
	}
}

func TestCreateParentNodes(t *testing.T) {
	payload := [][]byte{[]byte("Payload"), []byte("Test")}

	c, err := NewClient([]string{"localhost:65500"}, log.NewNopLogger())
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if c == nil {
		t.Fatal("expected new Client, got nil")
	}

	s, err := NewInstancer(c, "/validpath", log.NewNopLogger())
	if err != stdzk.ErrNoServer {
		t.Errorf("unexpected error: %v", err)
	}
	if s != nil {
		t.Error("expected failed new Instancer")
	}

	s, err = NewInstancer(c, "invalidpath", log.NewNopLogger())
	if err != stdzk.ErrInvalidPath {
		t.Errorf("unexpected error: %v", err)
	}
	_, _, err = c.GetEntries("/validpath")
	if err != stdzk.ErrNoServer {
		t.Errorf("unexpected error: %v", err)
	}

	c.Stop()

	err = c.CreateParentNodes("/validpath")
	if err != ErrClientClosed {
		t.Errorf("unexpected error: %v", err)
	}

	s, err = NewInstancer(c, "/validpath", log.NewNopLogger())
	if err != ErrClientClosed {
		t.Errorf("unexpected error: %v", err)
	}
	if s != nil {
		t.Error("expected failed new Instancer")
	}

	c, err = NewClient([]string{"localhost:65500"}, log.NewNopLogger(), Payload(payload))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if c == nil {
		t.Fatal("expected new Client, got nil")
	}

	s, err = NewInstancer(c, "/validpath", log.NewNopLogger())
	if err != stdzk.ErrNoServer {
		t.Errorf("unexpected error: %v", err)
	}
	if s != nil {
		t.Error("expected failed new Instancer")
	}
}