Пример #1
0
func TestProduceFetch(t *testing.T) {
	t.Parallel()
	k, err := kafka.New()
	if err != nil {
		t.Fatal(err)
	}
	partitionCount := 2
	partition := int32(1)
	topic, err := k.NewRandomTopic(partitionCount)
	if err != nil {
		t.Fatal(err)
	}
	defer k.DeleteTopic(topic)
	leaderAddr := getLeader(t, k, topic, partition)
	b := broker.New(leaderAddr)
	defer b.Close()
	key, value := "test key", "test value"
	produceMessage(t, b, topic, partition, key, value)
	messages := fetchMessage(t, b, topic, partition, 0)
	if len(messages) != 1 {
		t.Fatalf("expect 1 message but got %v", messages)
	}
	if m := messages[0]; m[0] != key || m[1] != value {
		t.Fatalf("expect [%s %s] but got %v", key, value, m)
	}
}
Пример #2
0
func getTopicMetadata(t *testing.T, k *kafka.Cluster, topic string) *TopicMetadataResponse {
	b := broker.New(k.AnyBroker())
	defer b.Close()
	respMsg, err := Metadata(topic).Fetch(b)
	if err != nil {
		t.Fatal(err)
	}
	brokers := k.Brokers()
	for i := range brokers {
		if respMsg.Brokers[i].Addr() != brokers[i] {
			t.Fatalf("broker: expect %s but got %s", brokers[i], respMsg.Brokers[i].Addr())
		}
	}
	if len(respMsg.TopicMetadatas) != 1 {
		t.Fatalf("len(TopicMetadatas): expect 1 but got %d", len(respMsg.TopicMetadatas))
	}
	meta := &respMsg.TopicMetadatas[0]
	if meta.ErrorCode != NoError {
		t.Fatal(meta.ErrorCode)
	}
	if meta.TopicName != topic {
		t.Fatalf("topic: expect %s but got %s", topic, meta.TopicName)
	}
	return respMsg
}
Пример #3
0
func TestProduceSnappy(t *testing.T) {
	t.Parallel()
	k, err := kafka.New()
	if err != nil {
		t.Fatal(err)
	}
	partitionCount := 2
	partition := int32(1)
	topic := "topic1"
	err = k.NewTopic(topic, partitionCount)
	if err != nil {
		k.DeleteTopic(topic)
		t.Fatal(err)
	}
	defer k.DeleteTopic(topic)
	leaderAddr := getLeader(t, k, topic, partition)

	b := broker.New(leaderAddr)
	defer b.Close()
	var w wipro.Writer
	ms := MessageSet{
		{SizedMessage: SizedMessage{CRCMessage: CRCMessage{Message: Message{
			Value: []byte("hello"),
		}}}},
	}
	ms.Marshal(&w)
	rawMsg := w.B[4:] // size must be excluded
	compressedValue := encodeSnappy(rawMsg)
	fmt.Println(w.B)
	if err := (&Payload{
		Topic:     topic,
		Partition: partition,
		MessageSet: MessageSet{
			{
				SizedMessage: SizedMessage{CRCMessage: CRCMessage{Message: Message{
					Attributes: 2,
					Value:      compressedValue,
				}}}},
		},
		RequiredAcks: AckLocal,
		AckTimeout:   10 * time.Second,
	}).DoProduce(b); err != nil {
		t.Fatal(err)
	}
}