From c13dfe3ad941fc3967ea173234c00ab40ef906ad Mon Sep 17 00:00:00 2001
From: Weizhen Wang <wangweizhen@pingcap.com>
Date: Mon, 12 Dec 2022 11:24:51 +0800
Subject: [PATCH] domain: fix data race in the MockInfoCacheAndLoadInfoSchema
 (#39802)

close pingcap/tidb#39801
---
 ddl/concurrentddltest/BUILD.bazel |  2 +-
 domain/test_helper.go             |  2 +-
 executor/BUILD.bazel              |  2 +-
 executor/seqtest/BUILD.bazel      |  2 +-
 infoschema/cache.go               | 11 +++++++++--
 5 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/ddl/concurrentddltest/BUILD.bazel b/ddl/concurrentddltest/BUILD.bazel
index 61388f159390f..82e2adf1fe9c2 100644
--- a/ddl/concurrentddltest/BUILD.bazel
+++ b/ddl/concurrentddltest/BUILD.bazel
@@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
 
 go_test(
     name = "concurrentddltest_test",
-    timeout = "long",
+    timeout = "moderate",
     srcs = [
         "main_test.go",
         "switch_test.go",
diff --git a/domain/test_helper.go b/domain/test_helper.go
index e63e8dee7389b..e8c106c29d23b 100644
--- a/domain/test_helper.go
+++ b/domain/test_helper.go
@@ -26,7 +26,7 @@ import (
 
 // MockInfoCacheAndLoadInfoSchema only used in unit tests.
 func (do *Domain) MockInfoCacheAndLoadInfoSchema(is infoschema.InfoSchema) {
-	do.infoCache = infoschema.NewCache(16)
+	do.infoCache.Reset(16)
 	do.infoCache.Insert(is, 0)
 }
 
diff --git a/executor/BUILD.bazel b/executor/BUILD.bazel
index 0e2fbacf20f95..7b1a4d603f993 100644
--- a/executor/BUILD.bazel
+++ b/executor/BUILD.bazel
@@ -247,7 +247,7 @@ go_library(
 
 go_test(
     name = "executor_test",
-    timeout = "long",
+    timeout = "moderate",
     srcs = [
         "adapter_test.go",
         "admin_test.go",
diff --git a/executor/seqtest/BUILD.bazel b/executor/seqtest/BUILD.bazel
index 64046c0852123..6d582ded16630 100644
--- a/executor/seqtest/BUILD.bazel
+++ b/executor/seqtest/BUILD.bazel
@@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
 
 go_test(
     name = "seqtest_test",
-    timeout = "moderate",
+    timeout = "short",
     srcs = [
         "main_test.go",
         "prepared_test.go",
diff --git a/infoschema/cache.go b/infoschema/cache.go
index 22ea012a9be28..34cc08eca2231 100644
--- a/infoschema/cache.go
+++ b/infoschema/cache.go
@@ -43,8 +43,15 @@ type InfoCache struct {
 }
 
 // NewCache creates a new InfoCache.
-func NewCache(capcity int) *InfoCache {
-	return &InfoCache{cache: make([]InfoSchema, 0, capcity)}
+func NewCache(capacity int) *InfoCache {
+	return &InfoCache{cache: make([]InfoSchema, 0, capacity)}
+}
+
+// Reset resets the cache.
+func (h *InfoCache) Reset(capacity int) {
+	h.mu.Lock()
+	defer h.mu.Unlock()
+	h.cache = make([]InfoSchema, 0, capacity)
 }
 
 // GetLatest gets the newest information schema.