2023-02-13 16:38:33 +08:00
|
|
|
// Licensed to the LF AI & Data foundation 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 msgdispatcher
|
|
|
|
|
|
|
|
import (
|
2023-09-08 09:51:17 +08:00
|
|
|
"context"
|
2023-02-13 16:38:33 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2023-09-08 09:51:17 +08:00
|
|
|
"time"
|
2023-02-13 16:38:33 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-16 14:58:34 +08:00
|
|
|
"go.uber.org/atomic"
|
2023-02-13 16:38:33 +08:00
|
|
|
|
2023-04-06 19:14:32 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/mq/msgstream/mqwrapper"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util/typeutil"
|
2023-02-13 16:38:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
client := NewClient(newMockFactory(), typeutil.ProxyRole, 1)
|
|
|
|
assert.NotNil(t, client)
|
2023-09-08 09:51:17 +08:00
|
|
|
_, err := client.Register(context.Background(), "mock_vchannel_0", nil, mqwrapper.SubscriptionPositionUnknown)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = client.Register(context.Background(), "mock_vchannel_1", nil, mqwrapper.SubscriptionPositionUnknown)
|
2023-02-13 16:38:33 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
client.Deregister("mock_vchannel_0")
|
2023-09-08 09:51:17 +08:00
|
|
|
client.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with timeout ctx", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
<-time.After(2 * time.Millisecond)
|
|
|
|
|
|
|
|
client := NewClient(newMockFactory(), typeutil.DataNodeRole, 1)
|
|
|
|
defer client.Close()
|
|
|
|
assert.NotNil(t, client)
|
|
|
|
_, err := client.Register(ctx, "mock_vchannel_1", nil, mqwrapper.SubscriptionPositionUnknown)
|
|
|
|
assert.Error(t, err)
|
2023-02-13 16:38:33 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_Concurrency(t *testing.T) {
|
2023-02-16 14:58:34 +08:00
|
|
|
client1 := NewClient(newMockFactory(), typeutil.ProxyRole, 1)
|
|
|
|
assert.NotNil(t, client1)
|
2023-02-13 16:38:33 +08:00
|
|
|
wg := &sync.WaitGroup{}
|
2023-02-16 14:58:34 +08:00
|
|
|
const total = 100
|
|
|
|
deregisterCount := atomic.NewInt32(0)
|
|
|
|
for i := 0; i < total; i++ {
|
2023-02-13 16:38:33 +08:00
|
|
|
vchannel := fmt.Sprintf("mock-vchannel-%d-%d", i, rand.Int())
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2023-09-08 09:51:17 +08:00
|
|
|
_, err := client1.Register(context.Background(), vchannel, nil, mqwrapper.SubscriptionPositionUnknown)
|
2023-02-16 14:58:34 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for j := 0; j < rand.Intn(2); j++ {
|
|
|
|
client1.Deregister(vchannel)
|
|
|
|
deregisterCount.Inc()
|
2023-02-13 16:38:33 +08:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2023-02-16 14:58:34 +08:00
|
|
|
expected := int(total - deregisterCount.Load())
|
2023-07-13 21:46:31 +08:00
|
|
|
|
2023-08-16 09:09:34 +08:00
|
|
|
c := client1.(*client)
|
|
|
|
c.managerMut.Lock()
|
|
|
|
n := len(c.managers)
|
|
|
|
c.managerMut.Unlock()
|
2023-07-13 21:46:31 +08:00
|
|
|
assert.Equal(t, expected, n)
|
2023-02-13 16:38:33 +08:00
|
|
|
}
|