github.com/QuangHoangHao/kafka-go@v0.4.36/topics/list_topics_test.go (about) 1 package topics 2 3 import ( 4 "context" 5 "net" 6 "regexp" 7 "testing" 8 "time" 9 10 "github.com/QuangHoangHao/kafka-go" 11 ktesting "github.com/QuangHoangHao/kafka-go/testing" 12 ) 13 14 func TestListReNil(t *testing.T) { 15 _, err := ListRe(context.Background(), nil, nil) 16 if err == nil { 17 t.Fatal(err) 18 } 19 } 20 21 func TestListRe(t *testing.T) { 22 client, shutdown := newLocalClientWithTopic("TestTopics-A", 1) 23 defer shutdown() 24 clientCreateTopic(client, "TestTopics-B", 1) 25 26 allRegex := regexp.MustCompile("TestTopics-.*") 27 fooRegex := regexp.MustCompile("TestTopics-B") 28 29 // Get all the topics 30 topics, err := ListRe(context.Background(), client, allRegex) 31 if err != nil { 32 t.Fatal(err) 33 } 34 if len(topics) != 2 { 35 t.Error("the wrong number of topics were returned. ", len(topics)) 36 } 37 38 // Get one topic 39 topics, err = ListRe(context.Background(), client, fooRegex) 40 if err != nil { 41 t.Fatal(err) 42 } 43 if len(topics) != 1 { 44 t.Error("the wrong number of topics were returned. ", len(topics)) 45 } 46 } 47 48 func newLocalClientWithTopic(topic string, partitions int) (*kafka.Client, func()) { 49 client, shutdown := newLocalClient() 50 if err := clientCreateTopic(client, topic, partitions); err != nil { 51 shutdown() 52 panic(err) 53 } 54 return client, func() { 55 client.DeleteTopics(context.Background(), &kafka.DeleteTopicsRequest{ 56 Topics: []string{topic}, 57 }) 58 shutdown() 59 } 60 } 61 62 func clientCreateTopic(client *kafka.Client, topic string, partitions int) error { 63 _, err := client.CreateTopics(context.Background(), &kafka.CreateTopicsRequest{ 64 Topics: []kafka.TopicConfig{{ 65 Topic: topic, 66 NumPartitions: partitions, 67 ReplicationFactor: 1, 68 }}, 69 }) 70 if err != nil { 71 return err 72 } 73 74 // Topic creation seems to be asynchronous. Metadata for the topic partition 75 // layout in the cluster is available in the controller before being synced 76 // with the other brokers, which causes "Error:[3] Unknown Topic Or Partition" 77 // when sending requests to the partition leaders. 78 // 79 // This loop will wait up to 2 seconds polling the cluster until no errors 80 // are returned. 81 for i := 0; i < 20; i++ { 82 r, err := client.Fetch(context.Background(), &kafka.FetchRequest{ 83 Topic: topic, 84 Partition: 0, 85 Offset: 0, 86 }) 87 if err == nil && r.Error == nil { 88 break 89 } 90 time.Sleep(100 * time.Millisecond) 91 } 92 93 return nil 94 } 95 96 func newLocalClient() (*kafka.Client, func()) { 97 return newClient(kafka.TCP("localhost")) 98 } 99 100 func newClient(addr net.Addr) (*kafka.Client, func()) { 101 conns := &ktesting.ConnWaitGroup{ 102 DialFunc: (&net.Dialer{}).DialContext, 103 } 104 105 transport := &kafka.Transport{ 106 Dial: conns.Dial, 107 Resolver: kafka.NewBrokerResolver(nil), 108 } 109 110 client := &kafka.Client{ 111 Addr: addr, 112 Timeout: 5 * time.Second, 113 Transport: transport, 114 } 115 116 return client, func() { transport.CloseIdleConnections(); conns.Wait() } 117 }