Skip to content

Commit 1d334a3

Browse files
committed
remove unsafe code from global config test
rearrange code from global config client Signed-off-by: lemonhx <lemonhx@lemonhx.tech>
1 parent b4a4311 commit 1d334a3

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

client/client.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1813,17 +1813,17 @@ func (c *client) WatchGlobalConfig(ctx context.Context) (chan []GlobalConfigItem
18131813
log.Error("[pd] panic in client `WatchGlobalConfig` cause by", zap.Any("error:", r))
18141814
return
18151815
}
1816-
close(receiver)
18171816
}()
18181817
for {
1819-
m, err := res.Recv()
1820-
if err != nil {
1821-
return
1822-
}
18231818
select {
18241819
case <-ctx.Done():
1820+
close(receiver)
18251821
return
18261822
default:
1823+
m, err := res.Recv()
1824+
if err != nil {
1825+
return
1826+
}
18271827
arr := make([]GlobalConfigItem, len(m.Changes))
18281828
for j, i := range m.Changes {
18291829
arr[j] = GlobalConfigItem{i.GetName(), i.GetValue(), nil}

tests/server/global_config/global_config_test.go

+4-17
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ package global_config_test
1616

1717
import (
1818
"context"
19-
"reflect"
2019
"strconv"
2120
"testing"
2221
"time"
23-
"unsafe"
2422

2523
. "github.com/pingcap/check"
2624
"github.com/pingcap/kvproto/pkg/pdpb"
@@ -201,20 +199,9 @@ func (s *GlobalConfigTestSuite) TestClientWatchCloseReceiverExternally(c *C) {
201199
}
202200

203201
func (s *GlobalConfigTestSuite) TestClientWatchTimeout(c *C) {
204-
ctx, _ := context.WithTimeout(s.server.Context(), time.Second)
202+
ctx, cancel := context.WithCancel(s.server.Context())
205203
wc, _ := s.client.WatchGlobalConfig(ctx)
206-
time.Sleep(2 * time.Second)
207-
closed := func(ch interface{}) bool {
208-
if reflect.TypeOf(ch).Kind() != reflect.Chan {
209-
panic("only channels!")
210-
}
211-
cptr := *(*uintptr)(unsafe.Pointer(
212-
unsafe.Pointer(uintptr(unsafe.Pointer(&ch)) + unsafe.Sizeof(uint(0))),
213-
))
214-
cptr += unsafe.Sizeof(uint(0)) * 2
215-
cptr += unsafe.Sizeof(unsafe.Pointer(uintptr(0)))
216-
cptr += unsafe.Sizeof(uint16(0))
217-
return *(*uint32)(unsafe.Pointer(cptr)) > 0
218-
}(wc)
219-
c.Assert(closed, Equals, true)
204+
cancel()
205+
_, opened := <-wc
206+
c.Assert(opened, Equals, false)
220207
}

0 commit comments

Comments
 (0)