mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-08 01:58:34 +08:00
Add unittest for timestamp allocator (#7367)
Signed-off-by: dragondriver <jiquan.long@zilliz.com>
This commit is contained in:
parent
84bddd0a03
commit
5b354313df
39
internal/proxy/mock.go
Normal file
39
internal/proxy/mock.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed 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 proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
||||||
|
|
||||||
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockTimestampAllocatorInterface struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tso *mockTimestampAllocatorInterface) AllocTimestamp(ctx context.Context, req *rootcoordpb.AllocTimestampRequest) (*rootcoordpb.AllocTimestampResponse, error) {
|
||||||
|
return &rootcoordpb.AllocTimestampResponse{
|
||||||
|
Status: &commonpb.Status{
|
||||||
|
ErrorCode: commonpb.ErrorCode_Success,
|
||||||
|
Reason: "",
|
||||||
|
},
|
||||||
|
Timestamp: uint64(time.Now().UnixNano()),
|
||||||
|
Count: req.Count,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMockTimestampAllocatorInterface() timestampAllocatorInterface {
|
||||||
|
return &mockTimestampAllocatorInterface{}
|
||||||
|
}
|
||||||
@ -18,20 +18,24 @@ import (
|
|||||||
|
|
||||||
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
"github.com/milvus-io/milvus/internal/proto/commonpb"
|
||||||
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
"github.com/milvus-io/milvus/internal/proto/rootcoordpb"
|
||||||
"github.com/milvus-io/milvus/internal/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TimestampAllocator struct {
|
// use timestampAllocatorInterface to keep TimestampAllocator testable
|
||||||
ctx context.Context
|
type timestampAllocatorInterface interface {
|
||||||
rootCoord types.RootCoord
|
AllocTimestamp(ctx context.Context, req *rootcoordpb.AllocTimestampRequest) (*rootcoordpb.AllocTimestampResponse, error)
|
||||||
peerID UniqueID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTimestampAllocator(ctx context.Context, rc types.RootCoord, peerID UniqueID) (*TimestampAllocator, error) {
|
type TimestampAllocator struct {
|
||||||
|
ctx context.Context
|
||||||
|
tso timestampAllocatorInterface
|
||||||
|
peerID UniqueID
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTimestampAllocator(ctx context.Context, tso timestampAllocatorInterface, peerID UniqueID) (*TimestampAllocator, error) {
|
||||||
a := &TimestampAllocator{
|
a := &TimestampAllocator{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
peerID: peerID,
|
peerID: peerID,
|
||||||
rootCoord: rc,
|
tso: tso,
|
||||||
}
|
}
|
||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
@ -48,7 +52,7 @@ func (ta *TimestampAllocator) Alloc(count uint32) ([]Timestamp, error) {
|
|||||||
Count: count,
|
Count: count,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := ta.rootCoord.AllocTimestamp(ctx, req)
|
resp, err := ta.tso.AllocTimestamp(ctx, req)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
58
internal/proxy/timestamp_test.go
Normal file
58
internal/proxy/timestamp_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed 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 proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"math/rand"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewTimestampAllocator(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
tso := newMockTimestampAllocatorInterface()
|
||||||
|
peerID := UniqueID(getUniqueIntGeneratorIns().get())
|
||||||
|
|
||||||
|
tsAllocator, err := NewTimestampAllocator(ctx, tso, peerID)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotNil(t, tsAllocator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimestampAllocator_Alloc(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
tso := newMockTimestampAllocatorInterface()
|
||||||
|
peerID := UniqueID(getUniqueIntGeneratorIns().get())
|
||||||
|
|
||||||
|
tsAllocator, err := NewTimestampAllocator(ctx, tso, peerID)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotNil(t, tsAllocator)
|
||||||
|
|
||||||
|
count := rand.Uint32()%100 + 1
|
||||||
|
ret, err := tsAllocator.Alloc(count)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, int(count), len(ret))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimestampAllocator_AllocOne(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
tso := newMockTimestampAllocatorInterface()
|
||||||
|
peerID := UniqueID(getUniqueIntGeneratorIns().get())
|
||||||
|
|
||||||
|
tsAllocator, err := NewTimestampAllocator(ctx, tso, peerID)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotNil(t, tsAllocator)
|
||||||
|
|
||||||
|
_, err = tsAllocator.AllocOne()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user