@@ -4,10 +4,80 @@ 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
+ defer conn .Close ()
20
+ if err != nil {
21
+ panic (err )
22
+ }
23
+
24
+ controller , _ := conn .Controller ()
25
+
26
+ controllerConn , err := Dial ("tcp" , net .JoinHostPort (controller .Host , strconv .Itoa (controller .Port )))
27
+ if err != nil {
28
+ t .Fatal (err )
29
+ }
30
+ defer controllerConn .Close ()
31
+
32
+ err = controllerConn .CreateTopics (TopicConfig {
33
+ Topic : topic1 ,
34
+ NumPartitions : 1 ,
35
+ ReplicationFactor : 1 ,
36
+ })
37
+ if err != nil {
38
+ t .Fatalf ("unexpected error creating topic: %s" , err .Error ())
39
+ }
40
+
41
+ err = controllerConn .CreateTopics (TopicConfig {
42
+ Topic : topic1 ,
43
+ NumPartitions : 1 ,
44
+ ReplicationFactor : 1 ,
45
+ })
46
+
47
+ // Duplicate topic should not return an error
48
+ if err != nil {
49
+ t .Fatalf ("unexpected error creating duplicate topic topic: %v" , err )
50
+ }
51
+
52
+ err = controllerConn .CreateTopics (
53
+ TopicConfig {
54
+ Topic : topic1 ,
55
+ NumPartitions : 1 ,
56
+ ReplicationFactor : 1 ,
57
+ },
58
+ TopicConfig {
59
+ Topic : topic2 ,
60
+ NumPartitions : 1 ,
61
+ ReplicationFactor : 1 ,
62
+ },
63
+ TopicConfig {
64
+ Topic : topic2 ,
65
+ NumPartitions : 1 ,
66
+ ReplicationFactor : 1 ,
67
+ },
68
+ )
69
+
70
+ if err == nil {
71
+ t .Fatal ("CreateTopics should have returned an error for invalid requests" )
72
+ }
73
+
74
+ if ! errors .Is (err , InvalidRequest ) {
75
+ t .Fatalf ("expected invalid request: %v" , err )
76
+ }
77
+
78
+ deleteTopic (t , topic1 , topic2 )
79
+ }
80
+
11
81
func TestClientCreateTopics (t * testing.T ) {
12
82
const (
13
83
topic1 = "client-topic-1"
@@ -59,7 +129,6 @@ func TestClientCreateTopics(t *testing.T) {
59
129
},
60
130
},
61
131
})
62
-
63
132
if err != nil {
64
133
t .Fatal (err )
65
134
}
0 commit comments