produceKeyedWithJava produces key:value messages via the Java console producer, which uses the DefaultPartitioner (murmur2) when a key is present.
(t *testing.T, topic string, messages []struct{ key, value string })
| 309 | // produceKeyedWithJava produces key:value messages via the Java console producer, |
| 310 | // which uses the DefaultPartitioner (murmur2) when a key is present. |
| 311 | func produceKeyedWithJava(t *testing.T, topic string, messages []struct{ key, value string }) { |
| 312 | t.Helper() |
| 313 | producerPath := fmt.Sprintf("/opt/kafka-%s/bin/kafka-console-producer.sh", FunctionalTestEnv.KafkaVersion) |
| 314 | args := []string{"compose", "exec", "-T", brokerContainer, producerPath} |
| 315 | if kafkaVersionAtLeast("2.5.0") { |
| 316 | args = append(args, "--bootstrap-server", brokerAddr) |
| 317 | } else { |
| 318 | args = append(args, "--broker-list", brokerAddr) |
| 319 | } |
| 320 | args = append(args, |
| 321 | "--topic", topic, |
| 322 | "--property", "parse.key=true", |
| 323 | "--property", "key.separator=:", |
| 324 | ) |
| 325 | cmd := exec.Command("docker", args...) |
| 326 | |
| 327 | stdin, err := cmd.StdinPipe() |
| 328 | require.NoError(t, err) |
| 329 | |
| 330 | stderr, err := cmd.StderrPipe() |
| 331 | require.NoError(t, err) |
| 332 | |
| 333 | var stderrOutput strings.Builder |
| 334 | var wg sync.WaitGroup |
| 335 | wg.Add(1) |
| 336 | go func() { |
| 337 | defer wg.Done() |
| 338 | s := bufio.NewScanner(stderr) |
| 339 | for s.Scan() { |
| 340 | stderrOutput.WriteString(s.Text() + "\n") |
| 341 | } |
| 342 | }() |
| 343 | |
| 344 | require.NoError(t, cmd.Start()) |
| 345 | |
| 346 | for _, msg := range messages { |
| 347 | _, err := fmt.Fprintf(stdin, "%s:%s\n", msg.key, msg.value) |
| 348 | if err != nil { |
| 349 | stdin.Close() |
| 350 | waitErr := cmd.Wait() |
| 351 | wg.Wait() |
| 352 | if waitErr != nil { |
| 353 | err = fmt.Errorf("failed to write message: %w; Java producer failed: %w; stderr: %s", err, waitErr, stderrOutput.String()) |
| 354 | } |
| 355 | require.NoError(t, err) |
| 356 | } |
| 357 | } |
| 358 | stdin.Close() |
| 359 | |
| 360 | err = cmd.Wait() |
| 361 | wg.Wait() |
| 362 | if err != nil { |
| 363 | t.Logf("Java producer stderr: %s", stderrOutput.String()) |
| 364 | require.NoError(t, err, "Java producer failed") |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // consumeKeyedFromPartition consumes up to count messages from a specific partition, |
no test coverage detected