Repository: grpc/grpc-go
File: interop/client/client.go (Go)

Code before:
			SoakResponseSize:                 *soakResponseSize,
			PerIterationMaxAcceptableLatency: time.Duration(*soakPerIterationMaxAcceptableLatencyMs) * time.Millisecond,
			MinTimeBetweenRPCs:               time.Duration(*soakMinTimeMsBetweenRPCs) * time.Millisecond,
			OverallTimeoutSeconds:            *soakOverallTimeoutSeconds,
			ServerAddr:                       serverAddr,
			SoakNumThreads:                   *soakNumThreads,
			SoakIterations:                   *soakIterations,
			MaxFailures:                      *soakMaxFailures,
			SharedChannel:                    conn,
			MayCreateNewChannel:              interop.UseSharedChannel,
		}
		interop.DoSoakTest(ctxWithDeadline, rpcSoakConfig)
		logger.Infoln("RpcSoak done")
	case "channel_soak":
		channelSoakConfig := interop.SoakTestConfig{
			SoakRequestSize:                  *soakRequestSize,
			SoakResponseSize:                 *soakResponseSize,
			PerIterationMaxAcceptableLatency: time.Duration(*soakPerIterationMaxAcceptableLatencyMs) * time.Millisecond,
			MinTimeBetweenRPCs:               time.Duration(*soakMinTimeMsBetweenRPCs) * time.Millisecond,
			OverallTimeoutSeconds:            *soakOverallTimeoutSeconds,
			ServerAddr:                       serverAddr,
			SoakNumThreads:                   *soakNumThreads,
			SoakIterations:                   *soakIterations,
			MaxFailures:                      *soakMaxFailures,
			SharedChannel:                    conn,
			MayCreateNewChannel: func(currentChannel *grpc.ClientConn) (*grpc.ClientConn, testgrpc.TestServiceClient) {
				return interop.CreateNewChannel(currentChannel, serverAddr, opts)
			},
		}
		interop.DoSoakTest(ctxWithDeadline, channelSoakConfig)
		logger.Infoln("ChannelSoak done")
	case "orca_per_rpc":
		interop.DoORCAPerRPCTest(ctx, tc)
		logger.Infoln("ORCAPerRPC done")
	case "orca_oob":
		interop.DoORCAOOBTest(ctx, tc)
		logger.Infoln("ORCAOOB done")
	default:
		logger.Fatal("Unsupported test case: ", *testCase)
	}
}


Review comment (suggestion):
This feels awkward.  How about just a `ChannelForTest` function that is a closure returning `conn` in the shared case and creates a new connection in the non-shared case.  And the closure can return a cleanup function that is a nop in the shared case, or closes the connection in the non-shared case.

Code after:
		logger.Infoln("SpecialStatusMessage done")
	case "custom_metadata":
		interop.DoCustomMetadata(ctx, tc)
		logger.Infoln("CustomMetadata done")
	case "unimplemented_method":
		interop.DoUnimplementedMethod(ctx, conn)
		logger.Infoln("UnimplementedMethod done")
	case "unimplemented_service":
		interop.DoUnimplementedService(ctx, testgrpc.NewUnimplementedServiceClient(conn))
		logger.Infoln("UnimplementedService done")
	case "pick_first_unary":
		interop.DoPickFirstUnary(ctx, tc)
		logger.Infoln("PickFirstUnary done")
	case "rpc_soak":
		rpcSoakConfig := createBaseSoakConfig(serverAddr)
		rpcSoakConfig.ChannelForTest = func() (*grpc.ClientConn, func()) { return conn, func() {} }
		interop.DoSoakTest(ctxWithDeadline, rpcSoakConfig)
		logger.Infoln("RpcSoak done")
	case "channel_soak":
		channelSoakConfig := createBaseSoakConfig(serverAddr)
		channelSoakConfig.ChannelForTest = func() (*grpc.ClientConn, func()) {
			cc, err := grpc.NewClient(serverAddr, opts...)
			if err != nil {
				log.Fatalf("Failed to create shared channel: %v", err)
			}
			return cc, func() { cc.Close() }
		}
		interop.DoSoakTest(ctxWithDeadline, channelSoakConfig)
		logger.Infoln("ChannelSoak done")
	case "orca_per_rpc":
		interop.DoORCAPerRPCTest(ctx, tc)
		logger.Infoln("ORCAPerRPC done")
	case "orca_oob":
		interop.DoORCAOOBTest(ctx, tc)
		logger.Infoln("ORCAOOB done")
	default:
		logger.Fatal("Unsupported test case: ", *testCase)
	}
}