2020-10-15 21:31:50 +08:00
|
|
|
package proxy
|
2020-10-15 16:32:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/apache/pulsar-client-go/pulsar"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-30 16:27:58 +08:00
|
|
|
pb "github.com/zilliztech/milvus-distributed/internal/proto/message"
|
|
|
|
"github.com/zilliztech/milvus-distributed/internal/util/typeutil"
|
2020-10-15 16:32:22 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimeTick(t *testing.T) {
|
|
|
|
client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
|
|
|
|
assert.Nil(t,err)
|
|
|
|
|
|
|
|
producer, err := client.CreateProducer(pulsar.ProducerOptions{Topic: "timesync"})
|
|
|
|
assert.Nil(t,err)
|
|
|
|
|
|
|
|
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
|
|
|
|
Topic: "timesync",
|
|
|
|
SubscriptionName: "timesync_group",
|
|
|
|
Type: pulsar.KeyShared,
|
|
|
|
SubscriptionInitialPosition: pulsar.SubscriptionPositionEarliest,
|
|
|
|
})
|
|
|
|
assert.Nil(t,err)
|
|
|
|
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 4*time.Second)
|
|
|
|
|
2020-10-30 16:27:58 +08:00
|
|
|
var curTs typeutil.Timestamp
|
2020-10-15 16:32:22 +08:00
|
|
|
curTs = 0
|
|
|
|
tt := timeTick{
|
|
|
|
interval: 200,
|
|
|
|
pulsarProducer: producer,
|
|
|
|
peer_id: 1,
|
|
|
|
ctx: ctx,
|
2020-10-30 16:27:58 +08:00
|
|
|
areRequestsDelivered: func(ts typeutil.Timestamp) bool { return true },
|
|
|
|
getTimestamp: func() (typeutil.Timestamp, error) {
|
2020-10-15 16:32:22 +08:00
|
|
|
curTs = curTs + 100
|
2020-10-30 16:27:58 +08:00
|
|
|
return curTs, nil
|
2020-10-15 16:32:22 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tt.Restart()
|
|
|
|
|
|
|
|
ctx2, _ := context.WithTimeout(context.Background(), time.Second*2)
|
|
|
|
isbreak := false
|
|
|
|
for {
|
|
|
|
if isbreak {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx2.Done():
|
|
|
|
isbreak = true
|
|
|
|
break
|
|
|
|
case cm, ok := <-consumer.Chan():
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("consumer closed")
|
|
|
|
}
|
|
|
|
consumer.AckID(cm.ID())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastTimestamp uint64 = 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case cm, ok := <-consumer.Chan():
|
|
|
|
if ok == false {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg := cm.Message
|
|
|
|
var tsm pb.TimeSyncMsg
|
|
|
|
if err := proto.Unmarshal(msg.Payload(), &tsm); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tsm.Timestamp <= lastTimestamp {
|
|
|
|
t.Fatalf("current = %d, last = %d", uint64(tsm.Timestamp), uint64(lastTimestamp))
|
|
|
|
}
|
|
|
|
t.Log("current = ", tsm.Timestamp)
|
|
|
|
lastTimestamp = tsm.Timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|