github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/p2p/server_stream_handle_test.go (about)

     1  // Copyright 2021 PingCAP, 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 p2p
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	proto "github.com/pingcap/tiflow/proto/p2p"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  var mockStreamMeta = &proto.StreamMeta{
    27  	SenderId:   "node-1",
    28  	ReceiverId: "node-2",
    29  	Epoch:      1,
    30  }
    31  
    32  func TestStreamHandleSend(t *testing.T) {
    33  	t.Parallel()
    34  	// We use a blocking channel to make the test case deterministic.
    35  	sendCh := make(chan proto.SendMessageResponse)
    36  	h := newStreamHandle(mockStreamMeta, sendCh)
    37  
    38  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    39  	defer cancel()
    40  
    41  	var wg sync.WaitGroup
    42  	wg.Add(1)
    43  	go func() {
    44  		defer wg.Done()
    45  		msg := <-sendCh
    46  		require.Equal(t, "test", msg.ErrorMessage)
    47  	}()
    48  
    49  	err := h.Send(ctx, proto.SendMessageResponse{
    50  		ErrorMessage: "test",
    51  	})
    52  	require.NoError(t, err)
    53  
    54  	wg.Wait()
    55  	cancel()
    56  	err = h.Send(ctx, proto.SendMessageResponse{})
    57  	require.Error(t, err, "should have been cancelled")
    58  	require.Regexp(t, "context canceled", err.Error())
    59  }
    60  
    61  func TestStreamHandleCloseWhileSending(t *testing.T) {
    62  	t.Parallel()
    63  	// We use a blocking channel to make the test case deterministic.
    64  	sendCh := make(chan proto.SendMessageResponse)
    65  	h := newStreamHandle(mockStreamMeta, sendCh)
    66  
    67  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    68  	defer cancel()
    69  
    70  	var wg sync.WaitGroup
    71  	wg.Add(1)
    72  	go func() {
    73  		defer wg.Done()
    74  		err := h.Send(ctx, proto.SendMessageResponse{})
    75  		require.Error(t, err)
    76  		require.Regexp(t, "ErrPeerMessageInternalSenderClosed", err.Error())
    77  	}()
    78  
    79  	time.Sleep(100 * time.Millisecond)
    80  	h.Close()
    81  
    82  	wg.Wait()
    83  	// Tests double close.
    84  	// Should not panic.
    85  	h.Close()
    86  }
    87  
    88  func TestStreamHandleCloseWhileNotSending(t *testing.T) {
    89  	t.Parallel()
    90  	// We use a blocking channel to make the test case deterministic.
    91  	sendCh := make(chan proto.SendMessageResponse)
    92  	h := newStreamHandle(mockStreamMeta, sendCh)
    93  
    94  	h.Close()
    95  
    96  	// Tests double close.
    97  	// Should not panic.
    98  	h.Close()
    99  }
   100  
   101  func TestGetStreamMeta(t *testing.T) {
   102  	t.Parallel()
   103  	h := newStreamHandle(mockStreamMeta, nil)
   104  	require.Equal(t, mockStreamMeta, h.GetStreamMeta())
   105  }