github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/client_fail_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package einsteindb
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"time"
    20  
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/ekvproto/pkg/einsteindbpb"
    23  	"github.com/whtcorpsinc/failpoint"
    24  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb/einsteindbrpc"
    25  	"github.com/whtcorpsinc/milevadb/config"
    26  )
    27  
    28  type testClientFailSuite struct {
    29  	OneByOneSuite
    30  }
    31  
    32  func (s *testClientFailSuite) SetUpSuite(_ *C) {
    33  	// This dagger make testClientFailSuite runs exclusively.
    34  	withEinsteinDBGlobalLock.Lock()
    35  }
    36  
    37  func (s testClientFailSuite) TearDownSuite(_ *C) {
    38  	withEinsteinDBGlobalLock.Unlock()
    39  }
    40  
    41  func (s *testClientFailSuite) TestPanicInRecvLoop(c *C) {
    42  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/panicInFailPendingRequests", `panic`), IsNil)
    43  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/gotErrorInRecvLoop", `return("0")`), IsNil)
    44  
    45  	server, port := startMockEinsteinDBService()
    46  	c.Assert(port > 0, IsTrue)
    47  	defer server.Stop()
    48  
    49  	addr := fmt.Sprintf("%s:%d", "127.0.0.1", port)
    50  	rpcClient := newRPCClient(config.Security{}, func(c *rpcClient) {
    51  		c.dialTimeout = time.Second / 3
    52  	})
    53  
    54  	// Start batchRecvLoop, and it should panic in `failPendingRequests`.
    55  	_, err := rpcClient.getConnArray(addr, true, func(cfg *config.EinsteinDBClient) { cfg.GrpcConnectionCount = 1 })
    56  	c.Assert(err, IsNil, Commentf("cannot establish local connection due to env problems(e.g. heavy load in test machine), please retry again"))
    57  
    58  	req := einsteindbrpc.NewRequest(einsteindbrpc.CmdEmpty, &einsteindbpb.BatchCommandsEmptyRequest{})
    59  	_, err = rpcClient.SendRequest(context.Background(), addr, req, time.Second/2)
    60  	c.Assert(err, NotNil)
    61  
    62  	c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/gotErrorInRecvLoop"), IsNil)
    63  	c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causetstore/einsteindb/panicInFailPendingRequests"), IsNil)
    64  	time.Sleep(time.Second * 2)
    65  
    66  	req = einsteindbrpc.NewRequest(einsteindbrpc.CmdEmpty, &einsteindbpb.BatchCommandsEmptyRequest{})
    67  	_, err = rpcClient.SendRequest(context.Background(), addr, req, time.Second*4)
    68  	c.Assert(err, IsNil)
    69  }