-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1096 from RussellLuo/add-support-for-delete-groups
Add support for DeleteGroups
- Loading branch information
Showing
7 changed files
with
250 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package sarama | ||
|
||
type DeleteGroupsRequest struct { | ||
Groups []string | ||
} | ||
|
||
func (r *DeleteGroupsRequest) encode(pe packetEncoder) error { | ||
return pe.putStringArray(r.Groups) | ||
} | ||
|
||
func (r *DeleteGroupsRequest) decode(pd packetDecoder, version int16) (err error) { | ||
r.Groups, err = pd.getStringArray() | ||
return | ||
} | ||
|
||
func (r *DeleteGroupsRequest) key() int16 { | ||
return 42 | ||
} | ||
|
||
func (r *DeleteGroupsRequest) version() int16 { | ||
return 0 | ||
} | ||
|
||
func (r *DeleteGroupsRequest) requiredVersion() KafkaVersion { | ||
return V1_1_0_0 | ||
} | ||
|
||
func (r *DeleteGroupsRequest) AddGroup(group string) { | ||
r.Groups = append(r.Groups, group) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package sarama | ||
|
||
import "testing" | ||
|
||
var ( | ||
emptyDeleteGroupsRequest = []byte{0, 0, 0, 0} | ||
|
||
singleDeleteGroupsRequest = []byte{ | ||
0, 0, 0, 1, // 1 group | ||
0, 3, 'f', 'o', 'o', // group name: foo | ||
} | ||
|
||
doubleDeleteGroupsRequest = []byte{ | ||
0, 0, 0, 2, // 2 groups | ||
0, 3, 'f', 'o', 'o', // group name: foo | ||
0, 3, 'b', 'a', 'r', // group name: foo | ||
} | ||
) | ||
|
||
func TestDeleteGroupsRequest(t *testing.T) { | ||
var request *DeleteGroupsRequest | ||
|
||
request = new(DeleteGroupsRequest) | ||
testRequest(t, "no groups", request, emptyDeleteGroupsRequest) | ||
|
||
request = new(DeleteGroupsRequest) | ||
request.AddGroup("foo") | ||
testRequest(t, "one group", request, singleDeleteGroupsRequest) | ||
|
||
request = new(DeleteGroupsRequest) | ||
request.AddGroup("foo") | ||
request.AddGroup("bar") | ||
testRequest(t, "two groups", request, doubleDeleteGroupsRequest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package sarama | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type DeleteGroupsResponse struct { | ||
ThrottleTime time.Duration | ||
GroupErrorCodes map[string]KError | ||
} | ||
|
||
func (r *DeleteGroupsResponse) encode(pe packetEncoder) error { | ||
pe.putInt32(int32(r.ThrottleTime / time.Millisecond)) | ||
|
||
if err := pe.putArrayLength(len(r.GroupErrorCodes)); err != nil { | ||
return err | ||
} | ||
for groupID, errorCode := range r.GroupErrorCodes { | ||
if err := pe.putString(groupID); err != nil { | ||
return err | ||
} | ||
pe.putInt16(int16(errorCode)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *DeleteGroupsResponse) decode(pd packetDecoder, version int16) error { | ||
throttleTime, err := pd.getInt32() | ||
if err != nil { | ||
return err | ||
} | ||
r.ThrottleTime = time.Duration(throttleTime) * time.Millisecond | ||
|
||
n, err := pd.getArrayLength() | ||
if err != nil { | ||
return err | ||
} | ||
if n == 0 { | ||
return nil | ||
} | ||
|
||
r.GroupErrorCodes = make(map[string]KError, n) | ||
for i := 0; i < n; i++ { | ||
groupID, err := pd.getString() | ||
if err != nil { | ||
return err | ||
} | ||
errorCode, err := pd.getInt16() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r.GroupErrorCodes[groupID] = KError(errorCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *DeleteGroupsResponse) key() int16 { | ||
return 42 | ||
} | ||
|
||
func (r *DeleteGroupsResponse) version() int16 { | ||
return 0 | ||
} | ||
|
||
func (r *DeleteGroupsResponse) requiredVersion() KafkaVersion { | ||
return V1_1_0_0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package sarama | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
var ( | ||
emptyDeleteGroupsResponse = []byte{ | ||
0, 0, 0, 0, // does not violate any quota | ||
0, 0, 0, 0, // no groups | ||
} | ||
|
||
errorDeleteGroupsResponse = []byte{ | ||
0, 0, 0, 0, // does not violate any quota | ||
0, 0, 0, 1, // 1 group | ||
0, 3, 'f', 'o', 'o', // group name | ||
0, 31, // error ErrClusterAuthorizationFailed | ||
} | ||
|
||
noErrorDeleteGroupsResponse = []byte{ | ||
0, 0, 0, 0, // does not violate any quota | ||
0, 0, 0, 1, // 1 group | ||
0, 3, 'f', 'o', 'o', // group name | ||
0, 0, // no error | ||
} | ||
) | ||
|
||
func TestDeleteGroupsResponse(t *testing.T) { | ||
var response *DeleteGroupsResponse | ||
|
||
response = new(DeleteGroupsResponse) | ||
testVersionDecodable(t, "empty", response, emptyDeleteGroupsResponse, 0) | ||
if response.ThrottleTime != 0 { | ||
t.Error("Expected no violation") | ||
} | ||
if len(response.GroupErrorCodes) != 0 { | ||
t.Error("Expected no groups") | ||
} | ||
|
||
response = new(DeleteGroupsResponse) | ||
testVersionDecodable(t, "error", response, errorDeleteGroupsResponse, 0) | ||
if response.ThrottleTime != 0 { | ||
t.Error("Expected no violation") | ||
} | ||
if response.GroupErrorCodes["foo"] != ErrClusterAuthorizationFailed { | ||
t.Error("Expected error ErrClusterAuthorizationFailed, found:", response.GroupErrorCodes["foo"]) | ||
} | ||
|
||
response = new(DeleteGroupsResponse) | ||
testVersionDecodable(t, "no error", response, noErrorDeleteGroupsResponse, 0) | ||
if response.ThrottleTime != 0 { | ||
t.Error("Expected no violation") | ||
} | ||
if response.GroupErrorCodes["foo"] != ErrNoError { | ||
t.Error("Expected error ErrClusterAuthorizationFailed, found:", response.GroupErrorCodes["foo"]) | ||
} | ||
} |
Oops, something went wrong.