Files
tasks/common/rpc/test/rpc_common_test.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

179 lines
4.3 KiB
Go

package rpc
import (
"context"
"crypto/tls"
"math/rand"
"net"
"strings"
"github.com/stretchr/testify/suite"
"go.temporal.io/server/api/testservice/v1"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/convert"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
var (
_ testservice.TestServiceServer = (*TestServiceServerHandler)(nil)
)
type (
// TestServiceServerHandler - gRPC handler interface for test service
TestServiceServerHandler struct {
testservice.UnimplementedTestServiceServer
}
)
type ServerUsageType int32
const (
Frontend ServerUsageType = iota
Internode
RemoteCluster
)
const (
localhostIPv4 = "127.0.0.1"
localhost = "localhost"
)
type TestFactory struct {
*rpc.RPCFactory
serverUsage ServerUsageType
}
// SendHello implements testservice.TestServiceServer
func (s *TestServiceServerHandler) SendHello(ctx context.Context, in *testservice.SendHelloRequest) (*testservice.SendHelloResponse, error) {
return &testservice.SendHelloResponse{Message: "Hello " + in.Name}, nil
}
var (
rpcTestCfgDefault = &config.RPC{
GRPCPort: 0,
MembershipPort: 7600,
BindOnIP: localhostIPv4,
}
cfg = &config.Config{
Services: map[string]config.Service{
"frontend": {
RPC: *rpcTestCfgDefault,
},
},
}
serverCfgInsecure = &config.Global{
Membership: config.Membership{
MaxJoinDuration: 5,
BroadcastAddress: localhostIPv4,
},
}
)
func startTestServiceServer(s *suite.Suite, factory *TestFactory) (*grpc.Server, string) {
var opts []grpc.ServerOption
var err error
if factory.serverUsage == Internode {
opts, err = factory.GetInternodeGRPCServerOptions()
} else {
opts, err = factory.GetFrontendGRPCServerOptions()
}
s.NoError(err)
server := grpc.NewServer(opts...)
testServiceHandler := &TestServiceServerHandler{}
testservice.RegisterTestServiceServer(server, testServiceHandler)
listener := factory.GetGRPCListener()
port := strings.Split(listener.Addr().String(), ":")[1]
s.NoError(err)
go func() {
err = server.Serve(listener)
}()
s.NoError(err)
return server, port
}
func runTestServerTest(s *suite.Suite, host string, serverFactory *TestFactory, clientFactory *TestFactory, isValid bool) {
server, port := startTestServiceServer(s, serverFactory)
defer server.Stop()
err := dialHello(s, host+":"+port, clientFactory, serverFactory.serverUsage)
if isValid {
s.NoError(err)
} else {
s.Error(err)
}
}
func runTestServerMultipleDials(
s *suite.Suite,
host string,
serverFactory *TestFactory,
clientFactory *TestFactory,
nDials int,
validator func(*credentials.TLSInfo, error),
) {
server, port := startTestServiceServer(s, serverFactory)
defer server.Stop()
for i := 0; i < nDials; i++ {
tlsInfo, err := dialTestServiceAndGetTLSInfo(s, host+":"+port, clientFactory, serverFactory.serverUsage)
validator(tlsInfo, err)
}
}
func dialHello(s *suite.Suite, hostport string, clientFactory *TestFactory, serverType ServerUsageType) error {
_, err := dialTestServiceAndGetTLSInfo(s, hostport, clientFactory, serverType)
return err
}
func dialTestServiceAndGetTLSInfo(
s *suite.Suite,
hostport string,
clientFactory *TestFactory,
serverType ServerUsageType,
) (*credentials.TLSInfo, error) {
logger := log.NewNoopLogger()
var cfg *tls.Config
var err error
switch serverType {
case Internode:
cfg, err = clientFactory.GetInternodeClientTlsConfig()
s.NoError(err)
case Frontend:
cfg, err = clientFactory.GetFrontendClientTlsConfig()
s.NoError(err)
case RemoteCluster:
host, _, err := net.SplitHostPort(hostport)
s.NoError(err)
cfg, err = clientFactory.GetRemoteClusterClientConfig(host)
s.NoError(err)
}
clientConn, err := rpc.Dial(hostport, cfg, logger)
s.NoError(err)
client := testservice.NewTestServiceClient(clientConn)
request := &testservice.SendHelloRequest{Name: convert.Uint64ToString(rand.Uint64())}
var reply *testservice.SendHelloResponse
peer := new(peer.Peer)
reply, err = client.SendHello(context.Background(), request, grpc.Peer(peer))
tlsInfo, _ := peer.AuthInfo.(credentials.TLSInfo)
if err == nil {
s.NotNil(reply)
s.True(strings.Contains(reply.Message, request.Name))
}
_ = clientConn.Close()
return &tlsInfo, err
}