cckellogg commented on a change in pull request #168: 
[Issue-148][pulsar-client-go] add seek by messageID
URL: https://github.com/apache/pulsar-client-go/pull/168#discussion_r364345880
 
 

 ##########
 File path: pulsar/consumer_test.go
 ##########
 @@ -663,128 +539,144 @@ func TestConsumerNack(t *testing.T) {
        }
 
        // Failed messages should be resent
-
        // We should only receive the odd messages
-       for i := 1; i < N; i += 2 {
+       for i := 1; i < times; i += 2 {
                msg, err := consumer.Receive(ctx)
                assert.Nil(t, err)
-               assert.Equal(t, fmt.Sprintf("msg-content-%d", i), 
string(msg.Payload()))
+               assert.Equal(t, fmt.Sprintf("%s-%d", msgPayload, i), 
string(msg.Payload()))
 
                consumer.Ack(msg)
        }
 }
 
 func TestConsumerCompression(t *testing.T) {
-       client, err := NewClient(ClientOptions{
-               URL: lookupURL,
-       })
-
-       assert.Nil(t, err)
+       client := createClient(t)
        defer client.Close()
 
        topicName := newTopicName()
-       ctx := context.Background()
-
-       producer, err := client.CreateProducer(ProducerOptions{
+       producer := createProducer(t, client, ProducerOptions{
                Topic:           topicName,
                CompressionType: LZ4,
        })
-       assert.Nil(t, err)
        defer producer.Close()
-
-       consumer, err := client.Subscribe(ConsumerOptions{
+       consumer := createConsumer(t, client, ConsumerOptions{
                Topic:            topicName,
                SubscriptionName: "sub-1",
        })
-       assert.Nil(t, err)
+       defer consumer.Close()
 
-       const N = 100
+       times := 10
+       msgPayload := "hello"
 
-       for i := 0; i < N; i++ {
-               if _, err := producer.Send(ctx, &ProducerMessage{
-                       Payload: []byte(fmt.Sprintf("msg-content-%d", i)),
-               }); err != nil {
-                       t.Fatal(err)
-               }
-       }
+       ctx := context.Background()
+       send(t, producer, ctx, times, &ProducerMessage{
+               Payload: []byte(msgPayload),
+       })
 
-       for i := 0; i < N; i++ {
-               msg, err := consumer.Receive(ctx)
-               assert.Nil(t, err)
-               assert.Equal(t, fmt.Sprintf("msg-content-%d", i), 
string(msg.Payload()))
-               consumer.Ack(msg)
-       }
+       receive(t, consumer, ctx, times, func(t *testing.T, msg Message, i int) 
{
+               expectMsg := fmt.Sprintf("%s-%d", msgPayload, i)
+               assert.Equal(t, []byte(expectMsg), msg.Payload())
+       })
 }
 
 func TestConsumerCompressionWithBatches(t *testing.T) {
-       client, err := NewClient(ClientOptions{
-               URL: lookupURL,
-       })
-
-       assert.Nil(t, err)
+       client := createClient(t)
        defer client.Close()
 
        topicName := newTopicName()
-       ctx := context.Background()
-
-       producer, err := client.CreateProducer(ProducerOptions{
+       producer := createProducer(t, client, ProducerOptions{
                Topic:                   topicName,
                CompressionType:         ZLib,
                BatchingMaxPublishDelay: 1 * time.Minute,
        })
-       assert.Nil(t, err)
        defer producer.Close()
 
-       consumer, err := client.Subscribe(ConsumerOptions{
+       consumer := createConsumer(t, client, ConsumerOptions{
                Topic:            topicName,
                SubscriptionName: "sub-1",
        })
-       assert.Nil(t, err)
-
-       const N = 100
+       defer consumer.Close()
 
-       for i := 0; i < N; i++ {
-               producer.SendAsync(ctx, &ProducerMessage{
-                       Payload: []byte(fmt.Sprintf("msg-content-%d", i)),
-               }, nil)
-       }
+       times := 100
+       msgPayload := "hello"
 
+       ctx := context.Background()
+       send(t, producer, ctx, times, &ProducerMessage{
+               Payload: []byte(msgPayload),
+       })
        producer.Flush()
 
-       for i := 0; i < N; i++ {
-               msg, err := consumer.Receive(ctx)
-               assert.Nil(t, err)
-               assert.Equal(t, fmt.Sprintf("msg-content-%d", i), 
string(msg.Payload()))
-               consumer.Ack(msg)
-       }
+       receive(t, consumer, ctx, times, func(t *testing.T, msg Message, i int) 
{
+               expectMsg := fmt.Sprintf("%s-%d", msgPayload, i)
+               assert.Equal(t, []byte(expectMsg), msg.Payload())
+       })
 }
 
-func TestConsumerMetadata(t *testing.T) {
-       client, err := NewClient(ClientOptions{
-               URL: lookupURL,
+func TestConsumerSeek(t *testing.T) {
+       client := createClient(t)
+       defer client.Close()
+
+       topicName := newTopicName()
+       producer := createProducer(t, client, ProducerOptions{
+               Topic: topicName,
        })
-       if err != nil {
-               t.Fatal(err)
-       }
+       defer producer.Close()
+
+       consumer := createConsumer(t, client, ConsumerOptions{
+               Topic:            topicName,
+               SubscriptionName: "my-sub",
+       })
+       defer consumer.Close()
+
+       times := 10
+       msgPayload := "hello"
+
+       ctx := context.Background()
+       send(t, producer, ctx, times, &ProducerMessage{
+               Payload: []byte(msgPayload),
+       })
+
+       var seekID MessageID
+       receive(t, consumer, ctx, times, func(t *testing.T, msg Message, i int) 
{
+               log.Printf("receive: %s", msg.Payload())
+               expectMsg := fmt.Sprintf("%s-%d", msgPayload, i)
+               assert.Equal(t, []byte(expectMsg), msg.Payload())
+
+               if i == 4 {
+                       seekID = msg.ID()
+               }
+       })
+
+       // seek to fourth message, expected receive fourth message.
+       err := consumer.Seek(seekID)
+       assert.Nil(t, err)
+
+       time.Sleep(500 * time.Millisecond)
 
 Review comment:
   Do we need a sleep here? It's best to avoid sleeps in test if possible.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to