github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/p2p/server_integration_test.go (about) 1 // Copyright 2022 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 "fmt" 19 "math" 20 "net" 21 "sync" 22 "testing" 23 "time" 24 25 "github.com/phayes/freeport" 26 p2pImpl "github.com/pingcap/tiflow/pkg/p2p" 27 "github.com/pingcap/tiflow/pkg/security" 28 "github.com/stretchr/testify/require" 29 "go.uber.org/atomic" 30 ) 31 32 func makeListenerForServerTests(t *testing.T) (l net.Listener, addr string) { 33 port := freeport.GetPort() 34 addr = fmt.Sprintf("127.0.0.1:%d", port) 35 l, err := net.Listen("tcp", addr) 36 require.NoError(t, err) 37 return 38 } 39 40 // read only 41 var clientConfigForUnitTesting = &p2pImpl.MessageClientConfig{ 42 SendChannelSize: 1, 43 BatchSendInterval: time.Second, 44 MaxBatchBytes: math.MaxInt64, 45 MaxBatchCount: math.MaxInt64, 46 RetryRateLimitPerSecond: 999.0, 47 ClientVersion: "v5.4.0", // a fake version 48 AdvertisedAddr: "fake-addr:8300", 49 MaxRecvMsgSize: 4 * 1024 * 1024, // 4MB 50 } 51 52 func TestMessageRPCServiceBasics(t *testing.T) { 53 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 54 defer cancel() 55 56 l, addr := makeListenerForServerTests(t) 57 messageSrvc, err := NewMessageRPCService("test-node-1", &security.Credential{} /* no TLS */) 58 require.NoError(t, err) 59 60 var wg sync.WaitGroup 61 wg.Add(1) 62 go func() { 63 defer wg.Done() 64 err := messageSrvc.Serve(ctx, l) 65 require.Error(t, err) 66 require.Regexp(t, ".*canceled.*", err.Error()) 67 }() 68 69 var called atomic.Bool 70 handlerManager := messageSrvc.MakeHandlerManager() 71 ok, err := handlerManager.RegisterHandler(ctx, "test-topic-1", &msgContent{}, func(sender NodeID, value MessageValue) error { 72 require.Equal(t, "test-client-1", sender) 73 require.IsType(t, &msgContent{}, value) 74 require.False(t, called.Swap(true)) 75 return nil 76 }) 77 require.NoError(t, err) 78 require.True(t, ok) 79 80 client := p2pImpl.NewGrpcMessageClient("test-client-1", clientConfigForUnitTesting) 81 wg.Add(1) 82 go func() { 83 defer wg.Done() 84 err := client.Run(ctx, "tcp", addr, "test-node-1", &security.Credential{} /* no TLS */) 85 require.Error(t, err) 86 require.Regexp(t, ".*canceled.*", err.Error()) 87 }() 88 89 _, err = client.SendMessage(ctx, "test-topic-1", &msgContent{}) 90 require.NoError(t, err) 91 require.Eventually(t, func() bool { 92 return called.Load() 93 }, 5*time.Second, 10*time.Millisecond) 94 95 cancel() 96 wg.Wait() 97 }