summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/zookeeper/fetcher_test.go
blob: 582d91c6f8ac4136cffef61ca6656caf8bdc5b9b (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package zookeeper

import (
	"testing"

	"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket"

	"github.com/stretchr/testify/assert"
)

func Test_clientFetch(t *testing.T) {
	c := &zookeeperFetcher{Client: &mockSocket{rowsNumResp: 10}}

	rows, err := c.fetch("whatever\n")
	assert.NoError(t, err)
	assert.Len(t, rows, 10)

	rows, err = c.fetch("whatever\n")
	assert.NoError(t, err)
	assert.Len(t, rows, 10)
}

func Test_clientFetchReadLineLimitExceeded(t *testing.T) {
	c := &zookeeperFetcher{Client: &mockSocket{rowsNumResp: limitReadLines + 1}}

	rows, err := c.fetch("whatever\n")
	assert.Error(t, err)
	assert.Len(t, rows, 0)
}

type mockSocket struct {
	rowsNumResp int
}

func (m *mockSocket) Connect() error {
	return nil
}

func (m *mockSocket) Disconnect() error {
	return nil
}

func (m *mockSocket) Command(command string, process socket.Processor) error {
	for i := 0; i < m.rowsNumResp; i++ {
		process([]byte(command))
	}
	return nil
}