File: describe_client_quotas_response_test.go

package info (click to toggle)
golang-github-ibm-sarama 1.46.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,072 kB
  • sloc: makefile: 40; sh: 30
file content (140 lines) | stat: -rw-r--r-- 4,485 bytes parent folder | download
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
//go:build !functional

package sarama

import (
	"testing"
	"time"

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

var (
	describeClientQuotasResponseError = []byte{
		0, 0, 0, 0, // ThrottleTime
		0, 35, // ErrorCode
		0, 41, 'C', 'u', 's', 't', 'o', 'm', ' ', 'e', 'n', 't', 'i', 't', 'y', ' ', 't', 'y', 'p', 'e', ' ', '\'', 'f', 'a', 'u', 'l', 't', 'y', '\'', ' ', 'n', 'o', 't', ' ', 's', 'u', 'p', 'p', 'o', 'r', 't', 'e', 'd',
		0, 0, 0, 0, // Entries
	}

	describeClientQuotasResponseSingleValue = []byte{
		0, 0, 0, 0, // ThrottleTime
		0, 0, // ErrorCode
		255, 255, // ErrorMsg (nil)
		0, 0, 0, 1, // Entries
		0, 0, 0, 1, // Entity
		0, 4, 'u', 's', 'e', 'r', // Entity type
		255, 255, // Entity name (nil)
		0, 0, 0, 1, // Values
		0, 18, 'p', 'r', 'o', 'd', 'u', 'c', 'e', 'r', '_', 'b', 'y', 't', 'e', '_', 'r', 'a', 't', 'e',
		65, 46, 132, 128, 0, 0, 0, 0, // 1000000
	}

	describeClientQuotasResponseComplexEntity = []byte{
		0, 0, 0, 0, // ThrottleTime
		0, 0, // ErrorCode
		255, 255, // ErrorMsg (nil)
		0, 0, 0, 2, // Entries
		0, 0, 0, 1, // Entity
		0, 4, 'u', 's', 'e', 'r', // Entity type
		255, 255, // Entity name (nil)
		0, 0, 0, 1, // Values
		0, 18, 'p', 'r', 'o', 'd', 'u', 'c', 'e', 'r', '_', 'b', 'y', 't', 'e', '_', 'r', 'a', 't', 'e',
		65, 46, 132, 128, 0, 0, 0, 0, // 1000000
		0, 0, 0, 1, // Entity
		0, 9, 'c', 'l', 'i', 'e', 'n', 't', '-', 'i', 'd', // Entity type
		0, 6, 's', 'a', 'r', 'a', 'm', 'a', // Entity name
		0, 0, 0, 1, // Values
		0, 18, 'c', 'o', 'n', 's', 'u', 'm', 'e', 'r', '_', 'b', 'y', 't', 'e', '_', 'r', 'a', 't', 'e',
		65, 46, 132, 128, 0, 0, 0, 0, // 1000000
	}

	describeClientQuotasResponseV1 = []byte{
		0x00, 0x00, 0x00, 0x80, // ThrottleTime (128ms)
		0x00, 0x00, // ErrorCode
		0x01, // ErrorMsg (nil)
		0x02, // Entries (2)
		0x02,
		0x05, 'u', 's', 'e', 'r', // Entity Type
		0x00, // Entity Name (nil)
		0x00, // Empty tagged fields
		0x02,
		// 0x13, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x65, 0x72, 0x5f, 0x62, 0x79, 0x74, 0x65, 0x5f, 0x72, 0x61, 0x74, 0x65,
		0x13, 'p', 'r', 'o', 'd', 'u', 'c', 'e', 'r', '_', 'b', 'y', 't', 'e', '_', 'r', 'a', 't', 'e',
		0x41, 0x2f, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, // 1024000
		0x00, // Empty tagged fields
		0x00, // Empty tagged fields
		0x00, // Empty tagged fields
	}
)

func TestDescribeClientQuotasResponse(t *testing.T) {
	// Response With Error
	errMsg := "Custom entity type 'faulty' not supported"
	res := &DescribeClientQuotasResponse{
		ThrottleTime: 0,
		ErrorCode:    ErrUnsupportedVersion,
		ErrorMsg:     &errMsg,
		Entries:      []DescribeClientQuotasEntry{},
	}
	testResponse(t, "Response With Error", res, describeClientQuotasResponseError)

	// Single Quota entry
	defaultUserComponent := QuotaEntityComponent{
		EntityType: QuotaEntityUser,
		MatchType:  QuotaMatchDefault,
	}
	entry := DescribeClientQuotasEntry{
		Entity: []QuotaEntityComponent{defaultUserComponent},
		Values: map[string]float64{"producer_byte_rate": 1000000},
	}
	res = &DescribeClientQuotasResponse{
		ThrottleTime: 0,
		ErrorCode:    ErrNoError,
		ErrorMsg:     nil,
		Entries:      []DescribeClientQuotasEntry{entry},
	}
	testResponse(t, "Single Value", res, describeClientQuotasResponseSingleValue)

	// Complex Quota entry
	saramaClientIDComponent := QuotaEntityComponent{
		EntityType: QuotaEntityClientID,
		MatchType:  QuotaMatchExact,
		Name:       "sarama",
	}
	userEntry := DescribeClientQuotasEntry{
		Entity: []QuotaEntityComponent{defaultUserComponent},
		Values: map[string]float64{"producer_byte_rate": 1000000},
	}
	clientEntry := DescribeClientQuotasEntry{
		Entity: []QuotaEntityComponent{saramaClientIDComponent},
		Values: map[string]float64{"consumer_byte_rate": 1000000},
	}
	res = &DescribeClientQuotasResponse{
		ThrottleTime: 0,
		ErrorCode:    ErrNoError,
		ErrorMsg:     nil,
		Entries:      []DescribeClientQuotasEntry{userEntry, clientEntry},
	}
	testResponse(t, "Complex Quota", res, describeClientQuotasResponseComplexEntity)
}

func TestDescribeClientQuotasResponseV1(t *testing.T) {
	res := &DescribeClientQuotasResponse{Version: 1}
	testVersionDecodable(t, "V1", res, describeClientQuotasResponseV1, 1)
	assert.Equal(t, res.ThrottleTime, time.Millisecond*128)
	assert.Len(t, res.Entries, 1)
	assert.Equal(t, []DescribeClientQuotasEntry{
		{
			Entity: []QuotaEntityComponent{
				{
					EntityType: "user",
					MatchType:  1,
				},
			},
			Values: map[string]float64{
				"producer_byte_rate": 1024000,
			},
		},
	}, res.Entries)
}