@@ -4,10 +4,86 @@ import (
4
4
"bufio"
5
5
"bytes"
6
6
"context"
7
+ "errors"
8
+ "net"
7
9
"reflect"
10
+ "strconv"
8
11
"testing"
9
12
)
10
13
14
+ func TestConnCreateTopics (t * testing.T ) {
15
+ topic1 := makeTopic ()
16
+ topic2 := makeTopic ()
17
+
18
+ conn , err := DialContext (context .Background (), "tcp" , "localhost:9092" )
19
+ if err != nil {
20
+ t .Fatal (err )
21
+ }
22
+
23
+ defer func () {
24
+ err := conn .Close ()
25
+ if err != nil {
26
+ t .Fatalf ("failed to close connection: %v" , err )
27
+ }
28
+ }()
29
+
30
+ controller , _ := conn .Controller ()
31
+
32
+ controllerConn , err := Dial ("tcp" , net .JoinHostPort (controller .Host , strconv .Itoa (controller .Port )))
33
+ if err != nil {
34
+ t .Fatal (err )
35
+ }
36
+ defer controllerConn .Close ()
37
+
38
+ err = controllerConn .CreateTopics (TopicConfig {
39
+ Topic : topic1 ,
40
+ NumPartitions : 1 ,
41
+ ReplicationFactor : 1 ,
42
+ })
43
+ if err != nil {
44
+ t .Fatalf ("unexpected error creating topic: %s" , err .Error ())
45
+ }
46
+
47
+ err = controllerConn .CreateTopics (TopicConfig {
48
+ Topic : topic1 ,
49
+ NumPartitions : 1 ,
50
+ ReplicationFactor : 1 ,
51
+ })
52
+
53
+ // Duplicate topic should not return an error
54
+ if err != nil {
55
+ t .Fatalf ("unexpected error creating duplicate topic topic: %v" , err )
56
+ }
57
+
58
+ err = controllerConn .CreateTopics (
59
+ TopicConfig {
60
+ Topic : topic1 ,
61
+ NumPartitions : 1 ,
62
+ ReplicationFactor : 1 ,
63
+ },
64
+ TopicConfig {
65
+ Topic : topic2 ,
66
+ NumPartitions : 1 ,
67
+ ReplicationFactor : 1 ,
68
+ },
69
+ TopicConfig {
70
+ Topic : topic2 ,
71
+ NumPartitions : 1 ,
72
+ ReplicationFactor : 1 ,
73
+ },
74
+ )
75
+
76
+ if err == nil {
77
+ t .Fatal ("CreateTopics should have returned an error for invalid requests" )
78
+ }
79
+
80
+ if ! errors .Is (err , InvalidRequest ) {
81
+ t .Fatalf ("expected invalid request: %v" , err )
82
+ }
83
+
84
+ deleteTopic (t , topic1 )
85
+ }
86
+
11
87
func TestClientCreateTopics (t * testing.T ) {
12
88
const (
13
89
topic1 = "client-topic-1"
@@ -59,7 +135,6 @@ func TestClientCreateTopics(t *testing.T) {
59
135
},
60
136
},
61
137
})
62
-
63
138
if err != nil {
64
139
t .Fatal (err )
65
140
}
0 commit comments