-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add consumer group deletion support
- Loading branch information
1 parent
1487371
commit 48ba56c
Showing
6 changed files
with
170 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package kadmin | ||
|
||
import tea "github.com/charmbracelet/bubbletea" | ||
|
||
type CGroupDeleter interface { | ||
DeleteCGroup(name string) tea.Msg | ||
} | ||
|
||
type CGroupDeletionStartedMsg struct { | ||
Deleted chan bool | ||
Err chan error | ||
} | ||
|
||
func (c *CGroupDeletionStartedMsg) AwaitCompletion() tea.Msg { | ||
select { | ||
case <-c.Deleted: | ||
return CGroupDeletedMsg{} | ||
case err := <-c.Err: | ||
return CGroupDeletionErrMsg{Err: err} | ||
} | ||
} | ||
|
||
type CGroupDeletionErrMsg struct { | ||
Err error | ||
} | ||
|
||
type CGroupDeletedMsg struct { | ||
} | ||
|
||
func (ka *SaramaKafkaAdmin) DeleteCGroup(name string) tea.Msg { | ||
errChan := make(chan error) | ||
deletedChan := make(chan bool) | ||
|
||
go ka.doDeleteCGroup(name, deletedChan, errChan) | ||
|
||
return CGroupDeletionStartedMsg{ | ||
Deleted: deletedChan, | ||
Err: errChan, | ||
} | ||
} | ||
|
||
func (ka *SaramaKafkaAdmin) doDeleteCGroup( | ||
name string, | ||
deletedChan chan bool, | ||
errChan chan error, | ||
) { | ||
err := ka.admin.DeleteConsumerGroup(name) | ||
if err != nil { | ||
errChan <- err | ||
return | ||
} | ||
deletedChan <- true | ||
} |
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,59 @@ | ||
package kadmin | ||
|
||
import ( | ||
"context" | ||
"github.com/IBM/sarama" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCGroupDeleter(t *testing.T) { | ||
t.Run("successful deletion", func(t *testing.T) { | ||
// create a topic | ||
topic := topicName() | ||
msg := ka.CreateTopic(TopicCreationDetails{ | ||
Name: topic, | ||
NumPartitions: 1, | ||
Properties: nil, | ||
}).(TopicCreationStartedMsg) | ||
|
||
switch msg := msg.AwaitCompletion().(type) { | ||
case TopicCreatedMsg: | ||
case TopicCreationErrMsg: | ||
t.Fatal("Unable to create topic", msg.Err) | ||
} | ||
|
||
// publish some data on the topic | ||
for i := 0; i < 10; i++ { | ||
ka.PublishRecord(&ProducerRecord{ | ||
Key: "key", | ||
Value: "value", | ||
Topic: topic, | ||
Partition: nil, | ||
}) | ||
} | ||
|
||
// consume from topic using test-group consumer group | ||
consumerGroup, err := sarama.NewConsumerGroupFromClient("test-group", kafkaClient()) | ||
if err != nil { | ||
t.Fatal("Unable to create Consumer Group.", err) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) // Adjust timeout as needed | ||
defer cancel() | ||
handler := testConsumer{ExpectedMsgCount: 10} | ||
consumerGroup.Consume(ctx, []string{topic}, &handler) | ||
err = consumerGroup.Close() | ||
if err != nil { | ||
t.Fatal("Unable to close group", err) | ||
} | ||
|
||
// delete the group | ||
cgroupDeletionStartedMsg := ka.DeleteCGroup("test-group").(CGroupDeletionStartedMsg) | ||
|
||
switch cgroupDeletionStartedMsg.AwaitCompletion().(type) { | ||
case CGroupDeletionErrMsg: | ||
t.Fatal("Failed to delete cgroup", msg.Err) | ||
case CGroupDeletedMsg: | ||
} | ||
}) | ||
} |
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
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