Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null replicas in RequestPartition #1270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alterpartitionreassignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *Client) AlterPartitionReassignments(
apiTopicMap[topic] = apiTopic
}

replicas := []int32{}
var replicas []int32
for _, brokerID := range assignment.BrokerIDs {
replicas = append(replicas, int32(brokerID))
}
Expand Down
59 changes: 59 additions & 0 deletions alterpartitionreassignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package kafka

import (
"context"
"net"
"testing"

"github.com/stretchr/testify/require"

"github.com/segmentio/kafka-go/protocol/alterpartitionreassignments"
ktesting "github.com/segmentio/kafka-go/testing"
)

Expand Down Expand Up @@ -117,3 +121,58 @@ func TestClientAlterPartitionReassignmentsMultiTopics(t *testing.T) {
)
}
}

type mockRoundTripper struct {
t *testing.T
expected alterpartitionreassignments.Request
}

var _ RoundTripper = &mockRoundTripper{}

func (m mockRoundTripper) RoundTrip(_ context.Context, _ net.Addr, req Request) (Response, error) {
switch msg := req.(type) {
case *alterpartitionreassignments.Request:
require.Equal(m.t, m.expected, *msg)
default:
m.t.Error(
"Unexpected req type",
"expected", "*alterpartitionreassignments.Request",
"got", msg,
)
}

return &alterpartitionreassignments.Response{}, nil
}

func TestClientAlterPartitionReassignmentsNilReplicas(t *testing.T) {
client := &Client{
Addr: TCP("nohost"),
Transport: mockRoundTripper{
t: t,
expected: alterpartitionreassignments.Request{
Topics: []alterpartitionreassignments.RequestTopic{
{
Name: "foobar",
Partitions: []alterpartitionreassignments.RequestPartition{
{
PartitionIndex: 0,
Replicas: nil,
},
},
},
},
},
},
}

_, err := client.AlterPartitionReassignments(context.Background(), &AlterPartitionReassignmentsRequest{
Assignments: []AlterPartitionReassignmentsRequestAssignment{
{
Topic: "foobar",
PartitionID: 0,
BrokerIDs: nil,
},
},
})
require.NoError(t, err)
}