github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/executor/server/metastore_test.go (about) 1 // Copyright 2022 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package server 15 16 import ( 17 "context" 18 "encoding/json" 19 "testing" 20 21 "github.com/golang/mock/gomock" 22 pb "github.com/pingcap/tiflow/engine/enginepb" 23 "github.com/pingcap/tiflow/engine/executor/server/mock" 24 "github.com/pingcap/tiflow/engine/pkg/client" 25 metaMock "github.com/pingcap/tiflow/engine/pkg/meta/mock" 26 metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func newMetastoreManagerForTesting(ctrl *gomock.Controller) (*metastoreManagerImpl, *mock.MockMetastoreCreator) { 31 mockCreator := mock.NewMockMetastoreCreator(ctrl) 32 return &metastoreManagerImpl{ 33 creator: mockCreator, 34 }, mockCreator 35 } 36 37 func TestMetastoreManagerBasics(t *testing.T) { 38 t.Parallel() 39 40 ctrl := gomock.NewController(t) 41 42 mockServerMasterClient := client.NewMockServerMasterClient(ctrl) 43 manager, mockCreator := newMetastoreManagerForTesting(ctrl) 44 ctx := context.Background() 45 46 var frameStoreParams metaModel.StoreConfig 47 frameStoreParams.SetEndpoints("127.0.0.1:3306") 48 frameParamBytes, err := json.Marshal(frameStoreParams) 49 require.NoError(t, err) 50 51 mockServerMasterClient.EXPECT(). 52 QueryMetaStore( 53 gomock.Any(), 54 gomock.Eq(&pb.QueryMetaStoreRequest{Tp: pb.StoreType_SystemMetaStore})). 55 Return(&pb.QueryMetaStoreResponse{ 56 Config: frameParamBytes, 57 }, nil).Times(1) 58 59 var businessStoreParams metaModel.StoreConfig 60 businessStoreParams.SetEndpoints("127.0.0.1:12345") 61 businessParamBytes, err := json.Marshal(businessStoreParams) 62 require.NoError(t, err) 63 64 mockServerMasterClient.EXPECT(). 65 QueryMetaStore( 66 gomock.Any(), 67 gomock.Eq(&pb.QueryMetaStoreRequest{Tp: pb.StoreType_AppMetaStore})). 68 Return(&pb.QueryMetaStoreResponse{ 69 Config: businessParamBytes, 70 }, nil).Times(1) 71 72 fakeFrameworkClientConn := metaMock.NewMockClientConn() 73 require.NoError(t, err) 74 fakeBusinessClientConn := metaMock.NewMockClientConn() 75 76 mockCreator. 77 EXPECT(). 78 CreateClientConnForFramework(gomock.Any(), gomock.Eq(frameStoreParams)). 79 Return(fakeFrameworkClientConn, nil) 80 mockCreator. 81 EXPECT(). 82 CreateClientConnForBusiness(gomock.Any(), gomock.Eq(businessStoreParams)). 83 Return(fakeBusinessClientConn, nil) 84 err = manager.Init(ctx, mockServerMasterClient) 85 require.NoError(t, err) 86 87 require.True(t, manager.IsInitialized()) 88 89 require.Equal(t, fakeFrameworkClientConn, manager.FrameworkClientConn()) 90 require.Equal(t, fakeBusinessClientConn, manager.BusinessClientConn()) 91 92 manager.Close() 93 94 require.Nil(t, manager.FrameworkClientConn()) 95 require.Nil(t, manager.BusinessClientConn()) 96 } 97 98 func TestMetastoreManagerUseBeforeInit(t *testing.T) { 99 t.Parallel() 100 101 ctrl := gomock.NewController(t) 102 103 manager, _ := newMetastoreManagerForTesting(ctrl) 104 require.False(t, manager.IsInitialized()) 105 106 require.Panics(t, func() { 107 manager.FrameworkClientConn() 108 }) 109 require.Panics(t, func() { 110 manager.BusinessClientConn() 111 }) 112 }