github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/flowinfra/utils_test.go (about)

     1  // Copyright 2019 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 flowinfra
    12  
    13  import (
    14  	"context"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/rpc"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    20  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    21  	"github.com/cockroachdb/cockroach/pkg/util/stop"
    22  )
    23  
    24  // createDummyStream creates the server and client side of a FlowStream stream.
    25  // This can be use by tests to pretend that then have received a FlowStream RPC.
    26  // The stream can be used to send messages (ConsumerSignal's) on it (within a
    27  // gRPC window limit since nobody's reading from the stream), for example
    28  // Handshake messages.
    29  //
    30  // We do this by creating a mock server, dialing into it and capturing the
    31  // server stream. The server-side RPC call will be blocked until the caller
    32  // calls the returned cleanup function.
    33  func createDummyStream() (
    34  	serverStream execinfrapb.DistSQL_FlowStreamServer,
    35  	clientStream execinfrapb.DistSQL_FlowStreamClient,
    36  	cleanup func(),
    37  	err error,
    38  ) {
    39  	stopper := stop.NewStopper()
    40  	clock := hlc.NewClock(hlc.UnixNano, time.Nanosecond)
    41  	clusterID, mockServer, addr, err := execinfrapb.StartMockDistSQLServer(clock, stopper, execinfra.StaticNodeID)
    42  	if err != nil {
    43  		return nil, nil, nil, err
    44  	}
    45  
    46  	rpcContext := rpc.NewInsecureTestingContextWithClusterID(clock, stopper, clusterID)
    47  	conn, err := rpcContext.GRPCDialNode(addr.String(), execinfra.StaticNodeID,
    48  		rpc.DefaultClass).Connect(context.Background())
    49  	if err != nil {
    50  		return nil, nil, nil, err
    51  	}
    52  	client := execinfrapb.NewDistSQLClient(conn)
    53  	clientStream, err = client.FlowStream(context.Background())
    54  	if err != nil {
    55  		return nil, nil, nil, err
    56  	}
    57  	streamNotification := <-mockServer.InboundStreams
    58  	serverStream = streamNotification.Stream
    59  	cleanup = func() {
    60  		close(streamNotification.Donec)
    61  		stopper.Stop(context.Background())
    62  	}
    63  	return serverStream, clientStream, cleanup, nil
    64  }