-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#3 added topic id partition logic and also its unit tests
- Loading branch information
Krishnakant C
authored and
Krishnakant C
committed
Sep 6, 2024
1 parent
bc8f635
commit 004b184
Showing
2 changed files
with
161 additions
and
0 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 |
---|---|---|
@@ -1 +1,71 @@ | ||
package common | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// TopicIdPartition represents a universally unique identifier with topic id for a topic partition. | ||
type TopicIdPartition struct { | ||
topicId Uuid | ||
topicPartition *TopicPartition // Changed to pointer | ||
} | ||
|
||
// NewTopicIdPartition creates a new TopicIdPartition instance. | ||
func NewTopicIdPartition(topicId Uuid, topicPartition *TopicPartition) (*TopicIdPartition, error) { | ||
if (topicId == Uuid{}) { | ||
return nil, errors.New("topicId cannot be nil") | ||
} | ||
if topicPartition == nil { | ||
return nil, errors.New("topicPartition cannot be nil") | ||
} | ||
return &TopicIdPartition{ | ||
topicId: topicId, | ||
topicPartition: topicPartition, | ||
}, nil | ||
} | ||
|
||
// NewTopicIdPartitionWithParams creates a new TopicIdPartition instance with topic name and partition id. | ||
func NewTopicIdPartitionWithParams(topicId Uuid, partition int, topic string) (*TopicIdPartition, error) { | ||
if (topicId == Uuid{}) { | ||
return nil, errors.New("topicId cannot be nil") | ||
} | ||
topicPartition := NewTopicPartition(topic, partition) | ||
return &TopicIdPartition{ | ||
topicId: topicId, | ||
topicPartition: topicPartition, | ||
}, nil | ||
} | ||
|
||
// TopicId returns the universally unique id representing this topic partition. | ||
func (tip *TopicIdPartition) TopicId() Uuid { | ||
return tip.topicId | ||
} | ||
|
||
// Topic returns the topic name or an empty string if it is unknown. | ||
func (tip *TopicIdPartition) Topic() string { | ||
return tip.topicPartition.Topic() | ||
} | ||
|
||
// Partition returns the partition id. | ||
func (tip *TopicIdPartition) Partition() int { | ||
return tip.topicPartition.Partition() | ||
} | ||
|
||
// TopicPartition returns the TopicPartition representing this instance. | ||
func (tip *TopicIdPartition) TopicPartition() *TopicPartition { | ||
return tip.topicPartition | ||
} | ||
|
||
// Equals checks if two TopicIdPartition instances are equal. | ||
func (tip *TopicIdPartition) Equals(other *TopicIdPartition) bool { | ||
if other == nil { | ||
return false | ||
} | ||
return tip.topicId == other.topicId && tip.topicPartition == other.topicPartition | ||
} | ||
|
||
// String returns a string representation of the TopicIdPartition. | ||
func (tip *TopicIdPartition) String() string { | ||
return fmt.Sprintf("%v:%s-%d", tip.topicId, tip.Topic(), tip.Partition()) | ||
} |
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,91 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
// Test for NewTopicIdPartition | ||
func TestNewTopicIdPartition(t *testing.T) { | ||
validUuid := NewUuid(0x123456789abcdef0, 0xfedcba9876543210) | ||
tp := NewTopicPartition("test-topic", 0) | ||
|
||
tip, err := NewTopicIdPartition(validUuid, tp) | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
if tip.TopicId() != validUuid { | ||
t.Errorf("Expected topicId %v, got %v", validUuid, tip.TopicId()) | ||
} | ||
if tip.Topic() != "test-topic" { | ||
t.Errorf("Expected topic %s, got %s", "test-topic", tip.Topic()) | ||
} | ||
if tip.Partition() != 0 { | ||
t.Errorf("Expected partition %d, got %d", 0, tip.Partition()) | ||
} | ||
} | ||
|
||
// Test for NewTopicIdPartition with nil TopicPartition | ||
func TestNewTopicIdPartition_NilTopicPartition(t *testing.T) { | ||
validUuid := NewUuid(0x123456789abcdef0, 0xfedcba9876543210) | ||
|
||
tip, err := NewTopicIdPartition(validUuid, nil) | ||
if tip != nil { | ||
t.Errorf("Expected nil, got %v", tip) | ||
} | ||
if err == nil { | ||
t.Fatal("Expected an error, got nil") | ||
} | ||
} | ||
|
||
// Test for NewTopicIdPartitionWithParams | ||
func TestNewTopicIdPartitionWithParams(t *testing.T) { | ||
validUuid := NewUuid(0x123456789abcdef0, 0xfedcba9876543210) | ||
tip, err := NewTopicIdPartitionWithParams(validUuid, 0, "test-topic") | ||
if err != nil { | ||
t.Fatalf("Expected no error, got %v", err) | ||
} | ||
if tip.Topic() != "test-topic" { | ||
t.Errorf("Expected topic %s, got %s", "test-topic", tip.Topic()) | ||
} | ||
if tip.Partition() != 0 { | ||
t.Errorf("Expected partition %d, got %d", 0, tip.Partition()) | ||
} | ||
} | ||
|
||
// Test for NewTopicIdPartitionWithParams with nil Uuid | ||
func TestNewTopicIdPartitionWithParams_NilUuid(t *testing.T) { | ||
tip, err := NewTopicIdPartitionWithParams(Uuid{}, 0, "test-topic") | ||
if tip != nil { | ||
t.Errorf("Expected nil, got %v", tip) | ||
} | ||
if err == nil { | ||
t.Fatal("Expected an error, got nil") | ||
} | ||
} | ||
|
||
// Test for Equals method | ||
func TestTopicIdPartition_Equals(t *testing.T) { | ||
validUuid := NewUuid(0x123456789abcdef0, 0xfedcba9876543210) | ||
tp1 := NewTopicPartition("test-topic", 0) | ||
tp2 := NewTopicPartition("test-topic", 0) | ||
|
||
tip1, _ := NewTopicIdPartition(validUuid, tp1) | ||
tip2, _ := NewTopicIdPartition(validUuid, tp2) | ||
|
||
if !tip1.Equals(tip2) { | ||
t.Errorf("Expected tips to be equal, but they are not") | ||
} | ||
} | ||
|
||
// Test for String method | ||
func TestTopicIdPartition_String(t *testing.T) { | ||
validUuid := NewUuid(0x123456789abcdef0, 0xfedcba9876543210) | ||
tp := NewTopicPartition("test-topic", 0) | ||
|
||
tip, _ := NewTopicIdPartition(validUuid, tp) | ||
expectedString := fmt.Sprintf("%s:test-topic-%d", validUuid.String(), 0) | ||
if tip.String() != expectedString { | ||
t.Errorf("Expected string %s, got %s", expectedString, tip.String()) | ||
} | ||
} |