github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/raft_transport_unit_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package kvserver
    12  
    13  import (
    14  	"context"
    15  	"math/rand"
    16  	"net"
    17  	"sync"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/cockroachdb/cockroach/pkg/base"
    22  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    23  	"github.com/cockroachdb/cockroach/pkg/rpc"
    24  	"github.com/cockroachdb/cockroach/pkg/rpc/nodedialer"
    25  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    26  	"github.com/cockroachdb/cockroach/pkg/util"
    27  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    28  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    29  	"github.com/cockroachdb/cockroach/pkg/util/log"
    30  	"github.com/cockroachdb/cockroach/pkg/util/netutil"
    31  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    32  	"github.com/cockroachdb/cockroach/pkg/util/tracing"
    33  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    34  	"github.com/cockroachdb/errors"
    35  )
    36  
    37  func TestRaftTransportStartNewQueue(t *testing.T) {
    38  	defer leaktest.AfterTest(t)()
    39  	ctx := context.Background()
    40  
    41  	stopper := stop.NewStopper()
    42  	defer stopper.Stop(ctx)
    43  
    44  	st := cluster.MakeTestingClusterSettings()
    45  	rpcC := rpc.NewContext(log.AmbientContext{}, &base.Config{Insecure: true},
    46  		hlc.NewClock(hlc.UnixNano, 500*time.Millisecond), stopper, st)
    47  	rpcC.ClusterID.Set(context.Background(), uuid.MakeV4())
    48  
    49  	// mrs := &dummyMultiRaftServer{}
    50  
    51  	grpcServer := rpc.NewServer(rpcC)
    52  	// RegisterMultiRaftServer(grpcServer, mrs)
    53  
    54  	var addr net.Addr
    55  
    56  	resolver := func(roachpb.NodeID) (net.Addr, error) {
    57  		if addr == nil {
    58  			return nil, errors.New("no addr yet") // should not happen in this test
    59  		}
    60  		return addr, nil
    61  	}
    62  
    63  	tp := NewRaftTransport(
    64  		log.AmbientContext{Tracer: tracing.NewTracer()},
    65  		cluster.MakeTestingClusterSettings(),
    66  		nodedialer.New(rpcC, resolver),
    67  		grpcServer,
    68  		stopper,
    69  	)
    70  
    71  	ln, err := netutil.ListenAndServeGRPC(stopper, grpcServer, &util.UnresolvedAddr{NetworkField: "tcp", AddressField: "localhost:0"})
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	addr = ln.Addr()
    77  
    78  	defer func() {
    79  		if ln != nil {
    80  			_ = ln.Close()
    81  		}
    82  	}()
    83  
    84  	_, existingQueue := tp.getQueue(1, rpc.SystemClass)
    85  	if existingQueue {
    86  		t.Fatal("queue already exists")
    87  	}
    88  	timeout := time.Duration(rand.Int63n(int64(5 * time.Millisecond)))
    89  	log.Infof(ctx, "running test with a ctx cancellation of %s", timeout)
    90  	ctxBoom, cancel := context.WithTimeout(ctx, timeout)
    91  	defer cancel()
    92  
    93  	var wg sync.WaitGroup
    94  	wg.Add(1)
    95  	go func() {
    96  		<-time.After(timeout)
    97  		_ = ln.Close()
    98  		ln = nil
    99  		wg.Done()
   100  	}()
   101  	var stats raftTransportStats
   102  	tp.startProcessNewQueue(ctxBoom, 1, rpc.SystemClass, &stats)
   103  
   104  	wg.Wait()
   105  }