Skip to content

Commit

Permalink
[bugfix] Fix wrong check eventime by default (#843)
Browse files Browse the repository at this point in the history
* Fix wrong check eventime by default

* Add license for new test

* Fix code style

* Add test for message without eventtime
  • Loading branch information
liangyuanpeng authored Sep 14, 2022
1 parent 649d992 commit b06e198
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
35 changes: 35 additions & 0 deletions pulsar/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,41 @@ func TestConsumerEventTime(t *testing.T) {
assert.Equal(t, "test", string(msg.Payload()))
}

func TestConsumerWithoutEventTime(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
})

assert.Nil(t, err)
defer client.Close()

topicName := "test-without-event-time"
ctx := context.Background()

producer, err := client.CreateProducer(ProducerOptions{
Topic: topicName,
})
assert.Nil(t, err)
defer producer.Close()

consumer, err := client.Subscribe(ConsumerOptions{
Topic: topicName,
SubscriptionName: "sub-1",
})
assert.Nil(t, err)
defer consumer.Close()

_, err = producer.Send(ctx, &ProducerMessage{
Payload: []byte("test"),
})
assert.Nil(t, err)

msg, err := consumer.Receive(ctx)
assert.Nil(t, err)
assert.Equal(t, int64(0), msg.EventTime().UnixNano())
assert.Equal(t, "test", string(msg.Payload()))
}

func TestConsumerFlow(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
Expand Down
30 changes: 30 additions & 0 deletions pulsar/message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package pulsar

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMessageZeroEventTime(t *testing.T) {
msg := &ProducerMessage{}
assert.Equal(t, false, msg.EventTime.UnixNano() == 0)
assert.Equal(t, true, msg.EventTime.IsZero())
}
2 changes: 1 addition & 1 deletion pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func (p *partitionProducer) internalSend(request *sendRequest) {
PayloadSize: proto.Int(len(payload)),
}

if msg.EventTime.UnixNano() != 0 {
if !msg.EventTime.IsZero() {
smm.EventTime = proto.Uint64(internal.TimestampMillis(msg.EventTime))
}

Expand Down

0 comments on commit b06e198

Please sign in to comment.