github.com/hack0072008/kafka-go@v1.0.1/batch_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestBatchDontExpectEOF(t *testing.T) {
    12  	topic := makeTopic()
    13  
    14  	broker, err := (&Dialer{
    15  		Resolver: &net.Resolver{},
    16  	}).LookupLeader(context.Background(), "tcp", "localhost:9092", topic, 0)
    17  	if err != nil {
    18  		t.Fatal("failed to open a new kafka connection:", err)
    19  	}
    20  
    21  	nc, err := net.Dial("tcp", net.JoinHostPort(broker.Host, strconv.Itoa(broker.Port)))
    22  	if err != nil {
    23  		t.Fatalf("cannot connect to partition leader at %s:%d: %s", broker.Host, broker.Port, err)
    24  	}
    25  
    26  	conn := NewConn(nc, topic, 0)
    27  	defer conn.Close()
    28  
    29  	nc.(*net.TCPConn).CloseRead()
    30  
    31  	batch := conn.ReadBatch(1024, 8192)
    32  
    33  	if _, err := batch.ReadMessage(); err != io.ErrUnexpectedEOF {
    34  		t.Error("bad error when reading message:", err)
    35  	}
    36  
    37  	if err := batch.Close(); err != io.ErrUnexpectedEOF {
    38  		t.Error("bad error when closing the batch:", err)
    39  	}
    40  }