forked from DataDog/kafka-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopics_test.go
94 lines (79 loc) · 2.47 KB
/
topics_test.go
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
package main
import (
"testing"
"github.com/DataDog/kafka-kit/v4/cmd/autothrottle/internal/throttlestore"
"github.com/DataDog/kafka-kit/v4/kafkaadmin/stub"
)
func TestAddReplica(t *testing.T) {
ttr := make(topicThrottledReplicas)
// Try to add an invalid type.
err := ttr.addReplica("test", "0", "invalid", "1001")
if err != errInvalidReplicaType {
t.Errorf("Expected 'errInvalidReplicaType' error")
}
types := []replicaType{"leaders", "followers"}
// Add valid types; error unexpected.
for _, typ := range types {
err := ttr.addReplica("test", "0", replicaType(typ), "1001")
if err != nil {
t.Fatal(err)
}
}
// For each type {leaders, followers}, ensure that we have one follower entry.
for _, typ := range types {
gotLen := len(ttr["test"][typ])
if gotLen != 1 {
t.Errorf("Expected len 1 for ttr[test][%s], got %d", typ, gotLen)
}
}
// Spot check the content.
if ttr["test"]["leaders"][0] != "0:1001" {
t.Errorf("Expected output '0:1001', got '%s'", ttr["test"]["leaders"][0])
}
}
func TestGetTopicsWithThrottledBrokers(t *testing.T) {
rtf := &ThrottleManager{
kafkaNativeMode: true,
ka: stub.NewClient(),
}
// Minimally populate the ThrottleManager.
rtf.brokerOverrides = throttlestore.BrokerOverrides{
1001: throttlestore.BrokerThrottleOverride{
ID: 1001,
ReassignmentParticipant: false,
Config: throttlestore.ThrottleOverrideConfig{
Rate: 50,
},
},
// Topics that include this broker shouldn't be included; the
// BrokerThrottleOverride.Filter called in getTopicsWithThrottledBrokers
// excludes any topics mapped to brokers where ReassignmentParticipant
// == true.
1002: throttlestore.BrokerThrottleOverride{
ID: 1002,
ReassignmentParticipant: true,
Config: throttlestore.ThrottleOverrideConfig{
Rate: 50,
},
},
}
// Call.
topicThrottledBrokers, _ := rtf.getTopicsWithThrottledBrokers()
expected := topicThrottledReplicas{
"test1": throttled{"followers": brokerIDs{"0:1001"}},
}
if len(topicThrottledBrokers) != len(expected) {
t.Fatalf("Expected len %d, got %d", len(expected), len(topicThrottledBrokers))
}
for topic := range expected {
output, exist := topicThrottledBrokers[topic]
if !exist {
t.Fatalf("Expected topic '%s' in output", topic)
}
got := output["followers"][0]
expectedOut := expected[topic]["followers"][0]
if got != expectedOut {
t.Errorf("Expected followers '%s', got '%s'", expectedOut, got)
}
}
}