@@ -24,6 +24,7 @@ import (
24
24
"encoding/json"
25
25
"fmt"
26
26
"io"
27
+ "math/rand"
27
28
"net"
28
29
"net/http"
29
30
"net/http/httptest"
@@ -1199,3 +1200,44 @@ func TestSetLabels(t *testing.T) {
1199
1200
// reset the global variable
1200
1201
config .GetGlobalConfig ().Labels = map [string ]string {}
1201
1202
}
1203
+
1204
+ func TestSetLabelsConcurrentWithGetLabel (t * testing.T ) {
1205
+ ts := createBasicHTTPHandlerTestSuite ()
1206
+
1207
+ ts .startServer (t )
1208
+ defer ts .stopServer (t )
1209
+
1210
+ testUpdateLabels := func () {
1211
+ labels := map [string ]string {}
1212
+ labels ["zone" ] = fmt .Sprintf ("z-%v" , rand .Intn (100000 ))
1213
+ buffer := bytes .NewBuffer ([]byte {})
1214
+ require .Nil (t , json .NewEncoder (buffer ).Encode (labels ))
1215
+ resp , err := ts .postStatus ("/labels" , "application/json" , buffer )
1216
+ require .NoError (t , err )
1217
+ require .NotNil (t , resp )
1218
+ defer func () {
1219
+ require .NoError (t , resp .Body .Close ())
1220
+ }()
1221
+ require .Equal (t , http .StatusOK , resp .StatusCode )
1222
+ newLabels := config .GetGlobalConfig ().Labels
1223
+ require .Equal (t , newLabels , labels )
1224
+ }
1225
+ done := make (chan struct {})
1226
+ go func () {
1227
+ for {
1228
+ select {
1229
+ case <- done :
1230
+ return
1231
+ default :
1232
+ config .GetGlobalConfig ().GetTiKVConfig ()
1233
+ }
1234
+ }
1235
+ }()
1236
+ for i := 0 ; i < 100 ; i ++ {
1237
+ testUpdateLabels ()
1238
+ }
1239
+ close (done )
1240
+
1241
+ // reset the global variable
1242
+ config .GetGlobalConfig ().Labels = map [string ]string {}
1243
+ }
0 commit comments