Update supported go versions
Client: go
With the release of go 1.23, update supported go versions to 1.22+1.23
according to our go support policy.
Also update the code to use the new range loop feature introduced in go
1.22 when appropriate.
Also fix a bug in TSSLServerSocket.Addr that it does not return the
listener address.
diff --git a/test/go/src/bin/stress/main.go b/test/go/src/bin/stress/main.go
index 3273d1b..89e87c4 100644
--- a/test/go/src/bin/stress/main.go
+++ b/test/go/src/bin/stress/main.go
@@ -133,7 +133,7 @@
if *clients != 0 {
ready.Add(*clients + 1)
done.Add(*clients)
- for i := 0; i < *clients; i++ {
+ for range *clients {
go client(protocolFactory)
}
ready.Done()
@@ -170,45 +170,45 @@
ready.Wait()
switch callType {
case echoVoid:
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoVoid(ctx)
atomic.AddInt64(&clicounter, 1)
}
case echoByte:
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoByte(ctx, 42)
atomic.AddInt64(&clicounter, 1)
}
case echoI32:
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoI32(ctx, 4242)
atomic.AddInt64(&clicounter, 1)
}
case echoI64:
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoI64(ctx, 424242)
atomic.AddInt64(&clicounter, 1)
}
case echoString:
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoString(ctx, "TestString")
atomic.AddInt64(&clicounter, 1)
}
case echiList:
l := []int8{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoList(ctx, l)
atomic.AddInt64(&clicounter, 1)
}
case echoSet:
s := []int8{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoSet(ctx, s)
atomic.AddInt64(&clicounter, 1)
}
case echoMap:
m := map[int8]int8{-10: 10, -9: 9, -8: 8, -7: 7, -6: 6, -5: 5, -4: 4, -3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
- for i := 0; i < *loop; i++ {
+ for range *loop {
client.EchoMap(ctx, m)
atomic.AddInt64(&clicounter, 1)
}
diff --git a/test/go/src/bin/testclient/main.go b/test/go/src/bin/testclient/main.go
index 95fcd47..a71223c 100644
--- a/test/go/src/bin/testclient/main.go
+++ b/test/go/src/bin/testclient/main.go
@@ -20,8 +20,10 @@
package main
import (
+ "bytes"
"context"
"flag"
+ "fmt"
t "log"
"reflect"
@@ -41,11 +43,15 @@
func main() {
flag.Parse()
- client, _, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl)
+ addr := *domain_socket
+ if addr == "" {
+ addr = fmt.Sprintf("%s:%d", *host, *port)
+ }
+ client, _, err := common.StartClient(addr, *transport, *protocol, *ssl)
if err != nil {
t.Fatalf("Unable to start client: ", err)
}
- for i := 0; i < *testloops; i++ {
+ for range *testloops {
callEverything(client)
}
}
@@ -127,17 +133,15 @@
}
binout := make([]byte, 256)
- for i := 0; i < 256; i++ {
+ for i := range binout {
binout[i] = byte(i)
}
bin, err := client.TestBinary(defaultCtx, binout)
if err != nil {
t.Fatalf("TestBinary failed with %v", err)
}
- for i := 0; i < 256; i++ {
- if binout[i] != bin[i] {
- t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i])
- }
+ if !bytes.Equal(binout, bin) {
+ t.Fatalf("Unexpected TestBinary() result expected % 02x, got % 02x ", binout, bin)
}
uout := thrift.Tuuid{
diff --git a/test/go/src/bin/testserver/main.go b/test/go/src/bin/testserver/main.go
index 60a764f..652fafa 100644
--- a/test/go/src/bin/testserver/main.go
+++ b/test/go/src/bin/testserver/main.go
@@ -41,7 +41,7 @@
func main() {
flag.Parse()
- processor, serverTransport, transportFactory, protocolFactory, err := common.GetServerParams(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler)
+ processor, serverTransport, transportFactory, protocolFactory, _, err := common.GetServerParams(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler)
if err != nil {
log.Fatalf("Unable to process server params: %v", err)
diff --git a/test/go/src/common/client.go b/test/go/src/common/client.go
index f9dfcaf..2383b82 100644
--- a/test/go/src/common/client.go
+++ b/test/go/src/common/client.go
@@ -37,14 +37,11 @@
}
func StartClient(
- host string,
- port int64,
- domain_socket string,
+ addr string,
transport string,
protocol string,
ssl bool,
) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
- hostPort := fmt.Sprintf("%s:%d", host, port)
cfg := &thrift.TConfiguration{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
@@ -70,13 +67,9 @@
protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "client:", thrift.StdLogger(nil))
}
if ssl {
- trans = thrift.NewTSSLSocketConf(hostPort, cfg)
+ trans = thrift.NewTSSLSocketConf(addr, cfg)
} else {
- if domain_socket != "" {
- trans = thrift.NewTSocketConf(domain_socket, nil)
- } else {
- trans = thrift.NewTSocketConf(hostPort, nil)
- }
+ trans = thrift.NewTSocketConf(addr, nil)
}
if err != nil {
return nil, nil, err
@@ -88,10 +81,9 @@
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
- trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client})
- fmt.Println(hostPort)
+ trans, err = thrift.NewTHttpClientWithOptions(fmt.Sprintf("https://%s/", addr), thrift.THttpClientOptions{Client: client})
} else {
- trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", hostPort))
+ trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/", addr))
}
case "framed":
trans = thrift.NewTFramedTransportConf(trans, cfg)
diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go
index a39519d..48203b6 100644
--- a/test/go/src/common/clientserver_test.go
+++ b/test/go/src/common/clientserver_test.go
@@ -22,6 +22,7 @@
import (
"context"
"errors"
+ "fmt"
"reflect"
"sync"
"testing"
@@ -42,10 +43,10 @@
}
var units = []test_unit{
- {"127.0.0.1", 9095, "", "", "binary", false},
- {"127.0.0.1", 9091, "", "", "compact", false},
- {"127.0.0.1", 9092, "", "", "binary", true},
- {"127.0.0.1", 9093, "", "", "compact", true},
+ {"127.0.0.1", 0, "", "", "binary", false},
+ {"127.0.0.1", 0, "", "", "compact", false},
+ {"127.0.0.1", 0, "", "", "binary", true},
+ {"127.0.0.1", 0, "", "", "compact", true},
}
func TestAllConnection(t *testing.T) {
@@ -61,29 +62,31 @@
}
func doUnit(t *testing.T, unit *test_unit) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- handler := NewMockThriftTest(ctrl)
+ t.Run(fmt.Sprintf("%v", *unit), func(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ defer ctrl.Finish()
+ handler := NewMockThriftTest(ctrl)
- processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
- if err != nil {
- t.Errorf("GetServerParams failed: %v", err)
- }
+ processor, serverTransport, transportFactory, protocolFactory, addr, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler)
+ if err != nil {
+ t.Errorf("GetServerParams failed: %v", err)
+ }
- server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
- if err = server.Listen(); err != nil {
- t.Errorf("Unable to start server: %v", err)
- return
- }
- go server.Serve()
- defer server.Stop()
- client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
- if err != nil {
- t.Errorf("Unable to start client: %v", err)
- return
- }
- defer trans.Close()
- callEverythingWithMock(t, client, handler)
+ server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
+ if err = server.Listen(); err != nil {
+ t.Errorf("Unable to start server: %v", err)
+ return
+ }
+ go server.Serve()
+ defer server.Stop()
+ client, trans, err := StartClient(addr, unit.transport, unit.protocol, unit.ssl)
+ if err != nil {
+ t.Errorf("Unable to start client: %v", err)
+ return
+ }
+ defer trans.Close()
+ callEverythingWithMock(t, client, handler)
+ })
}
var rmapmap = map[int32]map[int32]int32{
diff --git a/test/go/src/common/context_test.go b/test/go/src/common/context_test.go
index c6cbad8..0aa217f 100644
--- a/test/go/src/common/context_test.go
+++ b/test/go/src/common/context_test.go
@@ -40,12 +40,17 @@
}
func TestHttpContextTimeout(t *testing.T) {
- unit := test_unit{"127.0.0.1", 9096, "", "http", "binary", false}
+ const (
+ host = "127.0.0.1"
+ port = 9096
+ )
+ addr := fmt.Sprintf("%s:%d", host, port)
+ unit := test_unit{host, port, "", "http", "binary", false}
- server := &http.Server{Addr: unit.host + fmt.Sprintf(":%d", unit.port), Handler: slowHttpHandler{}}
+ server := &http.Server{Addr: addr, Handler: slowHttpHandler{}}
go server.ListenAndServe()
- client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
+ client, trans, err := StartClient(addr, unit.transport, unit.protocol, unit.ssl)
if err != nil {
t.Errorf("Unable to start client: %v", err)
return
diff --git a/test/go/src/common/server.go b/test/go/src/common/server.go
index f48389d..0af2702 100644
--- a/test/go/src/common/server.go
+++ b/test/go/src/common/server.go
@@ -46,10 +46,10 @@
ssl bool,
certPath string,
handler thrifttest.ThriftTest,
-) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {
+) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, string /* addr */, error) {
var err error
- hostPort := fmt.Sprintf("%s:%d", host, port)
+ hostPort := fmt.Sprintf("%s:0", host)
var cfg *thrift.TConfiguration = nil
var protocolFactory thrift.TProtocolFactory
@@ -65,30 +65,54 @@
case "header":
protocolFactory = thrift.NewTHeaderProtocolFactoryConf(nil)
default:
- return nil, nil, nil, nil, fmt.Errorf("invalid protocol specified %s", protocol)
+ return nil, nil, nil, nil, "", fmt.Errorf("invalid protocol specified %s", protocol)
}
if debugServerProtocol {
protocolFactory = thrift.NewTDebugProtocolFactoryWithLogger(protocolFactory, "server:", thrift.StdLogger(nil))
}
var serverTransport thrift.TServerTransport
+ var addr string
if ssl {
cfg := new(tls.Config)
if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
- return nil, nil, nil, nil, err
+ return nil, nil, nil, nil, "", err
} else {
cfg.Certificates = append(cfg.Certificates, cert)
}
- serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
+ transport, transportErr := thrift.NewTSSLServerSocket(hostPort, cfg)
+ if transportErr == nil {
+ listenErr := transport.Listen()
+ if listenErr == nil {
+ serverTransport = transport
+ addr = transport.Addr().String()
+ } else {
+ err = listenErr
+ }
+ } else {
+ err = transportErr
+ }
} else {
if domain_socket != "" {
serverTransport, err = thrift.NewTServerSocket(domain_socket)
+ addr = domain_socket
} else {
- serverTransport, err = thrift.NewTServerSocket(hostPort)
+ transport, transportErr := thrift.NewTServerSocket(hostPort)
+ if transportErr == nil {
+ listenErr := transport.Listen()
+ if listenErr == nil {
+ serverTransport = transport
+ addr = transport.Addr().String()
+ } else {
+ err = listenErr
+ }
+ } else {
+ err = transportErr
+ }
}
}
if err != nil {
- return nil, nil, nil, nil, err
+ return nil, nil, nil, nil, "", err
}
var transportFactory thrift.TTransportFactory
@@ -107,9 +131,9 @@
case "":
transportFactory = thrift.NewTTransportFactory()
default:
- return nil, nil, nil, nil, fmt.Errorf("invalid transport specified %s", transport)
+ return nil, nil, nil, nil, "", fmt.Errorf("invalid transport specified %s", transport)
}
processor := thrifttest.NewThriftTestProcessor(handler)
- return processor, serverTransport, transportFactory, protocolFactory, nil
+ return processor, serverTransport, transportFactory, protocolFactory, addr, nil
}