|
17 | 17 | package kafka |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "context" |
21 | 22 | "encoding/binary" |
22 | 23 | "encoding/json" |
@@ -563,3 +564,105 @@ func TestTransactionalAPI(t *testing.T) { |
563 | 564 |
|
564 | 565 | p.Close() |
565 | 566 | } |
| 567 | + |
| 568 | +// TestProducerDeliveryReportFields tests the `go.delivery.report.fields` config setting |
| 569 | +func TestProducerDeliveryReportFields(t *testing.T) { |
| 570 | + t.Run("none", func(t *testing.T) { |
| 571 | + runProducerDeliveryReportFieldTest(t, &ConfigMap{ |
| 572 | + "socket.timeout.ms": 10, |
| 573 | + "message.timeout.ms": 10, |
| 574 | + "go.delivery.report.fields": "", |
| 575 | + }, func(expected, actual *Message) { |
| 576 | + if len(actual.Key) > 0 { |
| 577 | + t.Errorf("key should not be set") |
| 578 | + } |
| 579 | + if len(actual.Value) > 0 { |
| 580 | + t.Errorf("value should not be set") |
| 581 | + } |
| 582 | + if s, ok := actual.Opaque.(*string); ok { |
| 583 | + if *s != *(expected.Opaque.(*string)) { |
| 584 | + t.Errorf("Opaque should be \"%v\", not \"%v\"", expected.Opaque, actual.Opaque) |
| 585 | + } |
| 586 | + } else { |
| 587 | + t.Errorf("opaque value should be a string, not \"%v\"", actual.Opaque) |
| 588 | + } |
| 589 | + }) |
| 590 | + }) |
| 591 | + t.Run("single", func(t *testing.T) { |
| 592 | + runProducerDeliveryReportFieldTest(t, &ConfigMap{ |
| 593 | + "socket.timeout.ms": 10, |
| 594 | + "message.timeout.ms": 10, |
| 595 | + "go.delivery.report.fields": "key", |
| 596 | + }, func(expected, actual *Message) { |
| 597 | + if !bytes.Equal(expected.Key, actual.Key) { |
| 598 | + t.Errorf("key should be \"%s\", not \"%s\"", expected.Key, actual.Key) |
| 599 | + } |
| 600 | + if len(actual.Value) > 0 { |
| 601 | + t.Errorf("value should not be set") |
| 602 | + } |
| 603 | + if s, ok := actual.Opaque.(*string); ok { |
| 604 | + if *s != *(expected.Opaque.(*string)) { |
| 605 | + t.Errorf("Opaque should be \"%v\", not \"%v\"", expected.Opaque, actual.Opaque) |
| 606 | + } |
| 607 | + } else { |
| 608 | + t.Errorf("opaque value should be a string, not \"%v\"", actual.Opaque) |
| 609 | + } |
| 610 | + }) |
| 611 | + }) |
| 612 | + t.Run("multiple", func(t *testing.T) { |
| 613 | + runProducerDeliveryReportFieldTest(t, &ConfigMap{ |
| 614 | + "socket.timeout.ms": 10, |
| 615 | + "message.timeout.ms": 10, |
| 616 | + "go.delivery.report.fields": "key,value", |
| 617 | + }, func(expected, actual *Message) { |
| 618 | + if !bytes.Equal(expected.Key, actual.Key) { |
| 619 | + t.Errorf("key should be \"%s\", not \"%s\"", expected.Key, actual.Key) |
| 620 | + } |
| 621 | + if !bytes.Equal(expected.Value, actual.Value) { |
| 622 | + t.Errorf("value should be \"%s\", not \"%s\"", expected.Value, actual.Value) |
| 623 | + } |
| 624 | + if s, ok := actual.Opaque.(*string); ok { |
| 625 | + if *s != *(expected.Opaque.(*string)) { |
| 626 | + t.Errorf("Opaque should be \"%v\", not \"%v\"", expected.Opaque, actual.Opaque) |
| 627 | + } |
| 628 | + } else { |
| 629 | + t.Errorf("opaque value should be a string, not \"%v\"", actual.Opaque) |
| 630 | + } |
| 631 | + }) |
| 632 | + }) |
| 633 | +} |
| 634 | + |
| 635 | +func runProducerDeliveryReportFieldTest(t *testing.T, config *ConfigMap, fn func(expected, actual *Message)) { |
| 636 | + p, err := NewProducer(config) |
| 637 | + if err != nil { |
| 638 | + t.Fatalf("%s", err) |
| 639 | + } |
| 640 | + |
| 641 | + topic1 := "gotest" |
| 642 | + |
| 643 | + myOpq := "My opaque" |
| 644 | + expected := &Message{ |
| 645 | + TopicPartition: TopicPartition{Topic: &topic1, Partition: 0}, |
| 646 | + Opaque: &myOpq, |
| 647 | + Value: []byte("ProducerChannel"), |
| 648 | + Key: []byte("This is my key"), |
| 649 | + } |
| 650 | + p.ProduceChannel() <- expected |
| 651 | + |
| 652 | + // We should expect a single message and possibly events |
| 653 | + for msgCnt := 0; msgCnt < 1; { |
| 654 | + ev := <-p.Events() |
| 655 | + switch e := ev.(type) { |
| 656 | + case *Message: |
| 657 | + msgCnt++ |
| 658 | + fn(expected, e) |
| 659 | + default: |
| 660 | + t.Logf("Ignored event %s", e) |
| 661 | + } |
| 662 | + } |
| 663 | + |
| 664 | + r := p.Flush(2000) |
| 665 | + if r > 0 { |
| 666 | + t.Errorf("Expected empty queue after Flush, still has %d", r) |
| 667 | + } |
| 668 | +} |
0 commit comments