@@ -17,6 +17,7 @@ package executor
17
17
import (
18
18
"strconv"
19
19
"strings"
20
+ "sync"
20
21
"testing"
21
22
22
23
"github.com/pingcap/tidb/parser/mysql"
@@ -77,3 +78,61 @@ func TestApplyCache(t *testing.T) {
77
78
require .NoError (t , err )
78
79
require .Nil (t , result )
79
80
}
81
+
82
+ func TestApplyCacheConcurrent (t * testing.T ) {
83
+ ctx := mock .NewContext ()
84
+ ctx .GetSessionVars ().MemQuotaApplyCache = 100
85
+ applyCache , err := newApplyCache (ctx )
86
+ require .NoError (t , err )
87
+
88
+ fields := []* types.FieldType {types .NewFieldType (mysql .TypeLonglong )}
89
+ value := make ([]* chunk.List , 2 )
90
+ key := make ([][]byte , 2 )
91
+ for i := 0 ; i < 2 ; i ++ {
92
+ value [i ] = chunk .NewList (fields , 1 , 1 )
93
+ srcChunk := chunk .NewChunkWithCapacity (fields , 1 )
94
+ srcChunk .AppendInt64 (0 , int64 (i ))
95
+ srcRow := srcChunk .GetRow (0 )
96
+ value [i ].AppendRow (srcRow )
97
+ key [i ] = []byte (strings .Repeat (strconv .Itoa (i ), 100 ))
98
+
99
+ // TODO: *chunk.List.GetMemTracker().BytesConsumed() is not accurate, fix it later.
100
+ require .Equal (t , int64 (100 ), applyCacheKVMem (key [i ], value [i ]))
101
+ }
102
+
103
+ applyCache .Set (key [0 ], value [0 ])
104
+ var wg sync.WaitGroup
105
+ wg .Add (2 )
106
+ var func1 = func () {
107
+ for i := 0 ; i < 100 ; i ++ {
108
+ for {
109
+ result , err := applyCache .Get (key [0 ])
110
+ require .NoError (t , err )
111
+ if result != nil {
112
+ applyCache .Set (key [1 ], value [1 ])
113
+ break
114
+ }
115
+ }
116
+ }
117
+ wg .Done ()
118
+ }
119
+ var func2 = func () {
120
+ for i := 0 ; i < 100 ; i ++ {
121
+ for {
122
+ result , err := applyCache .Get (key [1 ])
123
+ require .NoError (t , err )
124
+ if result != nil {
125
+ applyCache .Set (key [0 ], value [0 ])
126
+ break
127
+ }
128
+ }
129
+ }
130
+ wg .Done ()
131
+ }
132
+ go func1 ()
133
+ go func2 ()
134
+ wg .Wait ()
135
+ result , err := applyCache .Get (key [0 ])
136
+ require .NoError (t , err )
137
+ require .NotNil (t , result )
138
+ }
0 commit comments