Exemplo n.º 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)
	}
}
Exemplo n.º 2
0
func TestGroupCoordinator(t *testing.T) {
	t.Parallel()
	k, err := kafka.New()
	if err != nil {
		t.Fatal(err)
	}
	group := kafka.RandomGroup()
	coord := getCoord(t, k, group)
	fmt.Println(group, coord)
}
Exemplo n.º 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)
	}
}
Exemplo n.º 4
0
func TestTopicMetadata(t *testing.T) {
	t.Parallel()
	k, err := kafka.New()
	if err != nil {
		t.Fatal(err)
	}
	partitionCount := 2
	topic, err := k.NewRandomTopic(partitionCount)
	if err != nil {
		t.Fatal(err)
	}
	defer k.DeleteTopic(topic)
	respMsg := getTopicMetadata(t, k, topic)
	meta := &respMsg.TopicMetadatas[0]
	if len(meta.PartitionMetadatas) != partitionCount {
		t.Fatalf("partition count: expect %d but got %d", partitionCount, len(meta.PartitionMetadatas))
	}
	for _, pMeta := range meta.PartitionMetadatas {
		if pMeta.ErrorCode != NoError {
			t.Fatal(pMeta.ErrorCode)
		}
	}
}