github.com/teng231/kafclient@v1.2.9/pubsub_test.go (about) 1 package kafclient 2 3 import ( 4 "log" 5 "strings" 6 "time" 7 8 "testing" 9 ) 10 11 type User struct { 12 Name string 13 Age int 14 } 15 16 /* 17 Test with consumer group: 18 * case 1: 2 consumer group 19 publisher: go test -run TestCGPush2Message 20 consumer: CG=cg1 go test -run TestCGHandleMessage | CG=cg2 go test -run TestCGHandleMessage 21 * case 2: 1 consumer group 22 publisher: go test -run TestCGPush2Message 23 consumer: CG=cg1 go test -run TestCGHandleMessage | CG=cg1 go test -run TestCGHandleMessage 24 25 Test with consumer and publisher 26 27 //**************** Test send message to topic using sync **********************/ 28 29 func TestNormalCaseSync(t *testing.T) { 30 ps := &Client{} 31 brokers := strings.Split("localhost:9092", ",") 32 33 ps.InitPublisher(brokers...) 34 log.Print("init done publiser") 35 for i := 1; i <= 30; i++ { 36 err := ps.Publish("topic-0", &User{ 37 Name: "hoa", 38 Age: i, 39 }) 40 log.Print(i, err) 41 time.Sleep(100 * time.Millisecond) 42 } 43 defer ps.Close() 44 err := ps.InitConsumerGroup("CG-0", brokers...) 45 if err != nil { 46 log.Print(err) 47 return 48 } 49 log.Print("init done consumer") 50 buff := make(chan Message, 20) 51 go func() { 52 for { 53 d := <-buff 54 user := &User{} 55 BodyParse(d.Body, user) 56 log.Print(d.Headers, d.Partition, user, " ", d.Partition) 57 d.Commit() 58 } 59 }() 60 err = ps.OnAsyncSubscribe([]*Topic{{ 61 "topic-0", false, nil, false, 62 }}, 1, buff) 63 log.Print(err) 64 } 65 66 func testPublishMessages(brokerURL, topic string, count int) { 67 ps := &Client{} 68 brokers := strings.Split(brokerURL, ",") 69 70 ps.InitPublisher(brokers...) 71 log.Print("init done publiser") 72 for i := 1; i <= count; i++ { 73 err := ps.Publish(topic, &User{ 74 Name: "hoa", 75 Age: i, 76 }) 77 log.Print(i, err) 78 } 79 ps.Close() 80 } 81 82 func TestPuslishMessages(t *testing.T) { 83 testPublishMessages("0.0.0.0:9092", "topic-0", 20) 84 } 85 86 // ***** Test send message with config ****/ 87 func testPuslishMessagesWithConfig(brokerURL, topic1, topic2 string) { 88 ps := &Client{} 89 brokers := strings.Split(brokerURL, ",") 90 91 ps.InitPublisher(brokers...) 92 config := &SenderConfig{ 93 Headers: map[string]string{"token": "abc123"}, 94 } 95 topic := &Topic{Name: topic1} 96 for i := 1; i <= 20; i++ { 97 err := ps.PublishWithConfig(topic, config, &User{ 98 Name: "nguoi dau tien", 99 Age: i, 100 }) 101 log.Print(i, err) 102 time.Sleep(100 * time.Millisecond) 103 } 104 // *************** Send message with specific partition *********** 105 topic = &Topic{Name: topic2, AutoCommit: true, Partition: ToPInt32(1)} 106 for i := 1; i <= 20; i++ { 107 err := ps.PublishWithConfig(topic, config, &User{ 108 Name: "nguoi thu 2", 109 Age: i, 110 }) 111 log.Print(err) 112 time.Sleep(100 * time.Millisecond) 113 } 114 } 115 func TestPuslishMessagesWithConfig(t *testing.T) { 116 testPuslishMessagesWithConfig("0.0.0.0:9092", "topic-1", "topic-2") 117 } 118 119 //**************** Test handle message when listen event auto commit **********************/ 120 func testSubscribeSimpleAutoCommit(brokerURL, group, topic1, topic2 string) { 121 ps := &Client{} 122 brokers := strings.Split(brokerURL, ",") 123 124 err := ps.InitConsumerGroup(group, brokers...) 125 if err != nil { 126 log.Print(err) 127 return 128 } 129 log.Print("init done consumer") 130 buff := make(chan Message, 5) 131 go func() { 132 for { 133 d := <-buff 134 user := &User{} 135 BodyParse(d.Body, user) 136 log.Print(d.Headers, d.Partition, user) 137 } 138 }() 139 err = ps.OnAsyncSubscribe([]*Topic{ 140 { 141 Name: topic1, AutoCommit: true, 142 }, { 143 Name: topic2, AutoCommit: true, Partition: ToPInt32(1), 144 }, 145 }, 1, buff) 146 log.Print(err) 147 } 148 func TestSubscribeSimpleAutoCommit(t *testing.T) { 149 testSubscribeSimpleAutoCommit("0.0.0.0:9092", "CG-0", "topic-1", "topic-2") 150 } 151 152 // ********** Test listen message when listen message and manual commit ****/ 153 func testSubscribeSimpleManualCommit(brokerURL, group, topic string) { 154 ps := &Client{} 155 brokers := strings.Split(brokerURL, ",") 156 157 err := ps.InitConsumerGroup(group, brokers...) 158 if err != nil { 159 log.Print(err) 160 return 161 } 162 log.Print("init done consumer") 163 buff := make(chan Message, 20) 164 go func() { 165 for { 166 d := <-buff 167 user := &User{} 168 BodyParse(d.Body, user) 169 log.Print(d.Headers, d.Partition, user) 170 if user.Age != 100 { 171 d.Commit() 172 } 173 } 174 }() 175 err = ps.OnAsyncSubscribe([]*Topic{{ 176 topic, false, nil, false, 177 }}, 1, buff) 178 log.Print(err) 179 } 180 func TestSubscribeSimpleManualCommit(t *testing.T) { 181 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-0", "topic-0") 182 } 183 184 func TestListTopic(t *testing.T) { 185 ps := &Client{} 186 brokers := strings.Split("0.0.0.0:9092", ",") 187 ts, err := ps.ListTopics(brokers...) 188 log.Print(ts, err) 189 } 190 191 // --------------------- Test full publish and subscribe --------------------- 192 193 func TestSimplePublishAndSubscibe(t *testing.T) { 194 lock := make(chan bool) 195 go t.Run("subscribe 1", func(t *testing.T) { 196 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-0", "topic-3") 197 }) 198 go t.Run("subscribe 2", func(t *testing.T) { 199 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-3") 200 }) 201 time.Sleep(5 * time.Second) 202 t.Run("publish to topic", func(t *testing.T) { 203 testPublishMessages("0.0.0.0:9092", "topic-3", 20) 204 }) 205 <-lock 206 } 207 208 // ------------------- Benchmark test ---------------- 209 func BenchmarkPublishMessages100z(b *testing.B) { 210 testPublishMessages("0.0.0.0:9092", "topic-4", 100) 211 } 212 213 func BenchmarkPublishMessages10000z(b *testing.B) { 214 testPublishMessages("0.0.0.0:9092", "topic-5", 10000) 215 } 216 217 func BenchmarkSubscribeSimpleManualCommit(b *testing.B) { 218 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-5") 219 } 220 221 func TestSimplePublishAndSubscibeResub(t *testing.T) { 222 lock := make(chan bool) 223 go t.Run("subscribe 1", func(t *testing.T) { 224 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-0", "topic-3") 225 }) 226 go t.Run("subscribe 2", func(t *testing.T) { 227 testSubscribeSimpleManualCommit("0.0.0.0:9092", "CG-1", "topic-3") 228 }) 229 time.Sleep(5 * time.Second) 230 t.Run("publish to topic", func(t *testing.T) { 231 testPublishMessages("0.0.0.0:9092", "topic-3", 20) 232 }) 233 <-lock 234 }