Skip to content

Commit

Permalink
store/gcworker: fix test data race (#27986)
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing authored Sep 14, 2021
1 parent efec5c3 commit 1bad364
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 2 additions & 5 deletions store/gcworker/gc_worker_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,9 @@ func createGCWorkerSuite(t *testing.T) (s *mockGCWorkerSuite, clean func()) {
}),
}

s.store, s.dom, clean = testkit.CreateMockStoreAndDomain(t, opts...)
s.tikvStore = s.store.(tikv.Storage)

s.tikvStore.GetOracle().Close()
s.oracle = &oracles.MockOracle{}
s.tikvStore.SetOracle(s.oracle)
s.store, s.dom, clean = testkit.CreateMockStoreWithOracle(t, s.oracle, opts...)
s.tikvStore = s.store.(tikv.Storage)

gcWorker, err := NewGCWorker(s.store, s.pdClient)
require.NoError(t, err)
Expand Down
15 changes: 15 additions & 0 deletions testkit/mockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikv"
)

// CreateMockStore return a new mock kv.Storage.
Expand All @@ -36,7 +38,11 @@ func CreateMockStore(t *testing.T, opts ...mockstore.MockTiKVStoreOption) (store
func CreateMockStoreAndDomain(t *testing.T, opts ...mockstore.MockTiKVStoreOption) (kv.Storage, *domain.Domain, func()) {
store, err := mockstore.NewMockStore(opts...)
require.NoError(t, err)
dom, clean := bootstrap(t, store)
return store, dom, clean
}

func bootstrap(t *testing.T, store kv.Storage) (*domain.Domain, func()) {
session.SetSchemaLease(0)
session.DisableStats4Test()
dom, err := session.BootstrapSession(store)
Expand All @@ -49,6 +55,15 @@ func CreateMockStoreAndDomain(t *testing.T, opts ...mockstore.MockTiKVStoreOptio
err := store.Close()
require.NoError(t, err)
}
return dom, clean
}

// CreateMockStoreWithOracle returns a new mock kv.Storage and *domain.Domain, providing the oracle for the store.
func CreateMockStoreWithOracle(t *testing.T, oracle oracle.Oracle, opts ...mockstore.MockTiKVStoreOption) (kv.Storage, *domain.Domain, func()) {
store, err := mockstore.NewMockStore(opts...)
require.NoError(t, err)
store.GetOracle().Close()
store.(tikv.Storage).SetOracle(oracle)
dom, clean := bootstrap(t, store)
return store, dom, clean
}

0 comments on commit 1bad364

Please sign in to comment.