-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement automatic refresh functionality for TableMetaCache (#739
) * support auto refresh table mate cache * fix test * Refactor the code to address the issue of duplicate definitions * Refactor the code to address the issue of duplicate definitions * Refactor the code to address the issue of duplicate definitions * Refactor the code to address the issue of duplicate definitions --------- Co-authored-by: root <[email protected]> Co-authored-by: JayLiu <[email protected]>
- Loading branch information
1 parent
0b45672
commit af53317
Showing
10 changed files
with
413 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* 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 base | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
"github.com/go-sql-driver/mysql" | ||
"github.com/stretchr/testify/assert" | ||
"seata.apache.org/seata-go/pkg/datasource/sql/types" | ||
"seata.apache.org/seata-go/testdata" | ||
) | ||
|
||
var ( | ||
capacity int32 = 1024 | ||
EexpireTime = 15 * time.Minute | ||
tableMetaOnce sync.Once | ||
) | ||
|
||
type mockTrigger struct { | ||
} | ||
|
||
func (m *mockTrigger) LoadOne(ctx context.Context, dbName string, table string, conn *sql.Conn) (*types.TableMeta, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m *mockTrigger) LoadAll(ctx context.Context, dbName string, conn *sql.Conn, tables ...string) ([]types.TableMeta, error) { | ||
return nil, nil | ||
} | ||
|
||
func TestBaseTableMetaCache_refresh(t *testing.T) { | ||
type fields struct { | ||
lock sync.RWMutex | ||
expireDuration time.Duration | ||
capity int32 | ||
size int32 | ||
cache map[string]*entry | ||
cancel context.CancelFunc | ||
trigger trigger | ||
db *sql.DB | ||
cfg *mysql.Config | ||
} | ||
type args struct { | ||
ctx context.Context | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want types.TableMeta | ||
}{ | ||
{name: "test-1", | ||
fields: fields{ | ||
lock: sync.RWMutex{}, | ||
capity: capacity, | ||
size: 0, | ||
expireDuration: EexpireTime, | ||
cache: map[string]*entry{ | ||
"test": { | ||
value: types.TableMeta{}, | ||
lastAccess: time.Now(), | ||
}, | ||
}, | ||
cancel: cancel, | ||
trigger: &mockTrigger{}, | ||
cfg: &mysql.Config{}, | ||
db: &sql.DB{}, | ||
}, args: args{ctx: ctx}, | ||
want: testdata.MockWantTypesMeta("test")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
connStub := gomonkey.ApplyMethodFunc(tt.fields.db, "Conn", | ||
func(_ context.Context) (*sql.Conn, error) { | ||
return &sql.Conn{}, nil | ||
}) | ||
|
||
defer connStub.Reset() | ||
|
||
loadAllStub := gomonkey.ApplyMethodFunc(tt.fields.trigger, "LoadAll", | ||
func(_ context.Context, _ string, _ *sql.Conn, _ ...string) ([]types.TableMeta, error) { | ||
return []types.TableMeta{tt.want}, nil | ||
}) | ||
|
||
defer loadAllStub.Reset() | ||
|
||
c := &BaseTableMetaCache{ | ||
lock: tt.fields.lock, | ||
expireDuration: tt.fields.expireDuration, | ||
capity: tt.fields.capity, | ||
size: tt.fields.size, | ||
cache: tt.fields.cache, | ||
cancel: tt.fields.cancel, | ||
trigger: tt.fields.trigger, | ||
db: tt.fields.db, | ||
cfg: tt.fields.cfg, | ||
} | ||
go c.refresh(tt.args.ctx) | ||
time.Sleep(time.Second * 3) | ||
|
||
assert.Equal(t, c.cache["test"].value, tt.want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.