Skip to content

Commit

Permalink
server: fix err check (#22999)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjianke authored Mar 18, 2021
1 parent 9f388de commit 3cfb984
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 112 deletions.
27 changes: 17 additions & 10 deletions server/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,19 @@ func (ts *ConnTestSuite) TestInitialHandshake(c *C) {
c.Assert(err, IsNil)

expected := new(bytes.Buffer)
expected.WriteByte(0x0a) // Protocol
expected.WriteString(mysql.ServerVersion) // Version
expected.WriteByte(0x00) // NULL
binary.Write(expected, binary.LittleEndian, uint32(1)) // Connection ID
expected.Write([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}) // Salt
binary.Write(expected, binary.LittleEndian, uint16(defaultCapability&0xFFFF)) // Server Capability
expected.WriteByte(uint8(mysql.DefaultCollationID)) // Server Language
binary.Write(expected, binary.LittleEndian, mysql.ServerStatusAutocommit) // Server Status
binary.Write(expected, binary.LittleEndian, uint16((defaultCapability>>16)&0xFFFF)) // Extended Server Capability
expected.WriteByte(0x0a) // Protocol
expected.WriteString(mysql.ServerVersion) // Version
expected.WriteByte(0x00) // NULL
err = binary.Write(expected, binary.LittleEndian, uint32(1)) // Connection ID
c.Assert(err, IsNil)
expected.Write([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}) // Salt
err = binary.Write(expected, binary.LittleEndian, uint16(defaultCapability&0xFFFF)) // Server Capability
c.Assert(err, IsNil)
expected.WriteByte(uint8(mysql.DefaultCollationID)) // Server Language
err = binary.Write(expected, binary.LittleEndian, mysql.ServerStatusAutocommit) // Server Status
c.Assert(err, IsNil)
err = binary.Write(expected, binary.LittleEndian, uint16((defaultCapability>>16)&0xFFFF)) // Extended Server Capability
c.Assert(err, IsNil)
expected.WriteByte(0x15) // Authentication Plugin Length
expected.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) // Unused
expected.Write([]byte{0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x00}) // Salt
Expand Down Expand Up @@ -486,7 +490,10 @@ func (ts *ConnTestSuite) TestDispatchClientProtocol41(c *C) {
func (ts *ConnTestSuite) testDispatch(c *C, inputs []dispatchInput, capability uint32) {
store, err := mockstore.NewMockStore()
c.Assert(err, IsNil)
defer store.Close()
defer func() {
err := store.Close()
c.Assert(err, IsNil)
}()
dom, err := session.BootstrapSession(store)
c.Assert(err, IsNil)
defer dom.Close()
Expand Down
99 changes: 71 additions & 28 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,10 @@ func (ts *basicHTTPHandlerTestSuite) startServer(c *C) {
ts.port = getPortFromTCPAddr(server.listener.Addr())
ts.statusPort = getPortFromTCPAddr(server.statusListener.Addr())
ts.server = server
go server.Run()
go func() {
err := server.Run()
c.Assert(err, IsNil)
}()
ts.waitUntilServerOnline()
}

Expand All @@ -506,7 +509,10 @@ func (ts *basicHTTPHandlerTestSuite) stopServer(c *C) {
func (ts *basicHTTPHandlerTestSuite) prepareData(c *C) {
db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
dbt := &DBTest{c, db}

dbt.mustExec("create database tidb;")
Expand Down Expand Up @@ -536,9 +542,12 @@ partition by range (a)

txn2, err := dbt.db.Begin()
c.Assert(err, IsNil)
txn2.Exec("insert into tidb.pt values (42, '123')")
txn2.Exec("insert into tidb.pt values (256, 'b')")
txn2.Exec("insert into tidb.pt values (666, 'def')")
_, err = txn2.Exec("insert into tidb.pt values (42, '123')")
c.Assert(err, IsNil)
_, err = txn2.Exec("insert into tidb.pt values (256, 'b')")
c.Assert(err, IsNil)
_, err = txn2.Exec("insert into tidb.pt values (666, 'def')")
c.Assert(err, IsNil)
err = txn2.Commit()
c.Assert(err, IsNil)
dbt.mustExec("drop table if exists t")
Expand Down Expand Up @@ -673,7 +682,10 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {

db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
dbt := &DBTest{c, db}

defer func(originGC bool) {
Expand Down Expand Up @@ -704,7 +716,11 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
c.Assert(len(data), Equals, 0)

c.Assert(failpoint.Enable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount", `return(true)`), IsNil)
defer failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount")
defer func() {
err = failpoint.Disable("github.com/pingcap/tidb/infoschema/mockTiFlashStoreCount")
c.Assert(err, IsNil)
}()

dbt.mustExec("use tidb")
dbt.mustExec("alter table test set tiflash replica 2 location labels 'a','b';")

Expand Down Expand Up @@ -740,7 +756,8 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(len(data), Equals, 1)
c.Assert(data[0].ReplicaCount, Equals, uint64(2))
c.Assert(strings.Join(data[0].LocationLabels, ","), Equals, "a,b")
Expand All @@ -754,7 +771,8 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(len(data), Equals, 1)
c.Assert(data[0].ReplicaCount, Equals, uint64(2))
c.Assert(strings.Join(data[0].LocationLabels, ","), Equals, "a,b")
Expand All @@ -781,7 +799,8 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(len(data), Equals, 3)
c.Assert(data[0].ReplicaCount, Equals, uint64(2))
c.Assert(strings.Join(data[0].LocationLabels, ","), Equals, "a,b")
Expand All @@ -795,13 +814,15 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
req = fmt.Sprintf(`{"id":%d,"region_count":3,"flash_region_count":3}`, pid1)
resp, err = ts.postStatus("/tiflash/replica", "application/json", bytes.NewBuffer([]byte(req)))
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
resp, err = ts.fetchStatus("/tiflash/replica")
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(len(data), Equals, 3)
c.Assert(data[0].Available, Equals, false)
c.Assert(data[1].Available, Equals, true)
Expand All @@ -811,18 +832,21 @@ func (ts *HTTPHandlerTestSuite) TestTiFlashReplica(c *C) {
req = fmt.Sprintf(`{"id":%d,"region_count":3,"flash_region_count":3}`, pid0)
resp, err = ts.postStatus("/tiflash/replica", "application/json", bytes.NewBuffer([]byte(req)))
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
req = fmt.Sprintf(`{"id":%d,"region_count":3,"flash_region_count":3}`, pid2)
resp, err = ts.postStatus("/tiflash/replica", "application/json", bytes.NewBuffer([]byte(req)))
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
checkFunc = func() {
resp, err = ts.fetchStatus("/tiflash/replica")
c.Assert(err, IsNil)
decoder = json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(len(data), Equals, 3)
c.Assert(data[0].Available, Equals, true)
c.Assert(data[1].Available, Equals, true)
Expand Down Expand Up @@ -1050,7 +1074,10 @@ func (ts *HTTPHandlerTestSuite) TestGetSchema(c *C) {

db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
dbt := &DBTest{c, db}

dbt.mustExec("create database if not exists test;")
Expand Down Expand Up @@ -1098,7 +1125,8 @@ func (ts *HTTPHandlerTestSuite) TestAllHistory(c *C) {
store := domain.GetDomain(s.(sessionctx.Context)).Store()
txn, _ := store.Begin()
txnMeta := meta.NewMeta(txn)
txnMeta.GetAllHistoryDDLJobs()
_, err = txnMeta.GetAllHistoryDDLJobs()
c.Assert(err, IsNil)
data, _ := txnMeta.GetAllHistoryDDLJobs()
err = decoder.Decode(&jobs)

Expand Down Expand Up @@ -1163,7 +1191,10 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
// test check_mb4_value_in_utf8
db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
dbt := &DBTest{c, db}

dbt.mustExec("create database tidb_test;")
Expand All @@ -1179,7 +1210,8 @@ func (ts *HTTPHandlerTestSuite) TestPostSettings(c *C) {
c.Assert(err, IsNil)
_, err = txn1.Exec("insert t2 values (unhex('F0A48BAE'));")
c.Assert(err, NotNil)
txn1.Commit()
err = txn1.Commit()
c.Assert(err, IsNil)

// Disable CheckMb4ValueInUTF8.
form = make(url.Values)
Expand All @@ -1198,8 +1230,10 @@ func (ts *HTTPHandlerTestSuite) TestPprof(c *C) {
for retry := 0; retry < retryTime; retry++ {
resp, err := ts.fetchStatus("/debug/pprof/heap")
if err == nil {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
err = resp.Body.Close()
c.Assert(err, IsNil)
return
}
time.Sleep(time.Millisecond * 10)
Expand Down Expand Up @@ -1308,7 +1342,10 @@ func (ts *HTTPHandlerTestSuite) TestZipInfoForSQL(c *C) {

db, err := sql.Open("mysql", ts.getDSN())
c.Assert(err, IsNil, Commentf("Error connecting"))
defer db.Close()
defer func() {
err := db.Close()
c.Assert(err, IsNil)
}()
dbt := &DBTest{c, db}

dbt.mustExec("use test")
Expand Down Expand Up @@ -1398,31 +1435,37 @@ func (ts *HTTPHandlerTestSuite) TestTestHandler(c *C) {

resp, err = ts.fetchStatus("/test/gc/gc")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)

resp, err = ts.fetchStatus("/test/gc/resolvelock")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)

resp, err = ts.fetchStatus("/test/gc/resolvelock?safepoint=a")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)

resp, err = ts.fetchStatus("/test/gc/resolvelock?physical=1")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)

resp, err = ts.fetchStatus("/test/gc/resolvelock?physical=true")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusBadRequest)

resp, err = ts.fetchStatus("/test/gc/resolvelock?safepoint=10000&physical=true")
c.Assert(err, IsNil)
resp.Body.Close()
err = resp.Body.Close()
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
}
Loading

0 comments on commit 3cfb984

Please sign in to comment.