// Run test suite using TCompactBinaryProtocol
func TestTCompactProtocol(t *testing.T) {
	RunSocketTestSuite(t,
		thrift.NewTCompactProtocolFactory(),
		thrift.NewTTransportFactory())
	RunSocketTestSuite(t,
		thrift.NewTCompactProtocolFactory(),
		thrift.NewTBufferedTransportFactory(8912))
	RunSocketTestSuite(t,
		thrift.NewTCompactProtocolFactory(),
		thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory()))
}
Пример #2
0
func main() {
	flag.Usage = Usage
	var client bool
	var server bool
	var protocol string
	var framed bool
	var useHttp bool
	var help bool

	flag.BoolVar(&client, "client", false, "Run client")
	flag.BoolVar(&server, "server", false, "Run server")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson)")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.BoolVar(&help, "help", false, "See usage string")
	flag.Parse()
	if help || (client && server) || !(client || server) {
		fmt.Print("flag.NArg() == ", flag.NArg(), "\n")
		flag.Usage()
	}
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	default:
		fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
		Usage()
		os.Exit(1)
	}
	transportFactory := thrift.NewTTransportFactory()
	if framed {
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	}

	if client {
		RunClient(transportFactory, protocolFactory)
	} else if server {
		RunServer(transportFactory, protocolFactory)
	} else {
		flag.Usage()
	}
	os.Exit(0)
}
Пример #3
0
func TestInitTwoServers(t *testing.T) {
	var err error
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	transportFactory := thrift.NewTTransportFactory()
	transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	addr = FindAvailableTCPServerPort()
	serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT)
	if err != nil {
		t.Fatal("Unable to create server socket", err)
	}
	server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)

	firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{})
	processor.RegisterProcessor("FirstService", firstProcessor)

	secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
	processor.RegisterProcessor("SecondService", secondProcessor)

	go server.Serve()
}
Пример #4
0
func main() {
	flag.Parse()
	if *memprofile != "" {
		runtime.MemProfileRate = 100
	}
	var ok bool
	if callType, ok = callTMap[*callName]; !ok {
		log.Fatal("Unknown service call", *callName)
	}
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	hostPort = fmt.Sprintf("%s:%d", *host, *port)
	var protocolFactory thrift.TProtocolFactory
	var transportFactory thrift.TTransportFactory

	if *compact {
		protocolFactory = thrift.NewTCompactProtocolFactory()
	} else {
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	}

	if *framed {
		transportFactory = thrift.NewTTransportFactory()
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	} else {
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	}

	if *runserver > 0 {
		serverTransport, err := thrift.NewTServerSocket(hostPort)
		if err != nil {
			log.Fatalf("Unable to create server socket: %s", err)
		}

		processor := stress.NewServiceProcessor(&handler{})
		server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
		if *clients == 0 {
			server.Serve()
		} else {
			go server.Serve()
		}
	}
	//start clients
	if *clients != 0 {
		ready.Add(*clients + 1)
		done.Add(*clients)
		for i := 0; i < *clients; i++ {
			go client(protocolFactory)
		}
		ready.Done()
		ready.Wait()
		//run!
		startTime := time.Now()
		//wait for completion
		done.Wait()
		endTime := time.Now()
		duration := endTime.Sub(startTime)
		log.Printf("%d calls in %v (%f calls per second)\n", clicounter, duration, float64(clicounter)/duration.Seconds())
	}
	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.WriteHeapProfile(f)
		f.Close()
		return
	}
}
Пример #5
0
package main

import (
	"errors"
	"fmt"
	"os"

	"rpc"
	"thrift"
)

var (
	transportFactory    = thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory     = thrift.NewTBinaryProtocolFactoryDefault()
	errUserOrPwdInvalid = errors.New("userid or password invalid")
)

const (
	remoteAddr = "192.168.1.241:8000"
)

type rpcService struct {
}

func (rs *rpcService) FindAll(userid int64, password string, param map[string]string) (res []string, err error) {
	if userid != 1 || password != "123456" || len(param) == 0 {
		err = errUserOrPwdInvalid
		return
	}

	for k, v := range param {
Пример #6
0
func StartServer(
	host string,
	port int64,
	domain_socket string,
	transport string,
	protocol string,
	ssl bool,
	certPath string,
	handler thrifttest.ThriftTest) (srv *thrift.TSimpleServer, err error) {

	hostPort := fmt.Sprintf("%s:%d", host, port)

	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
	case "binary":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	default:
		return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
	}
	if debugServerProtocol {
		protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
	}

	var serverTransport thrift.TServerTransport
	if ssl {
		cfg := new(tls.Config)
		if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil {
			return nil, err
		} else {
			cfg.Certificates = append(cfg.Certificates, cert)
		}
		serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
	} else {
		if domain_socket != "" {
			serverTransport, err = thrift.NewTServerSocket(domain_socket)
		} else {
			serverTransport, err = thrift.NewTServerSocket(hostPort)
		}
	}
	if err != nil {
		return nil, err
	}

	var transportFactory thrift.TTransportFactory

	switch transport {
	case "http":
		return nil, fmt.Errorf("Http server transport is not supported")
		// trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort))
		// if err != nil {
		// 	return nil, err
		// }
	case "framed":
		transportFactory = thrift.NewTTransportFactory()
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	case "buffered":
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	case "zlib":
		transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
	case "":
		transportFactory = thrift.NewTTransportFactory()
	default:
		return nil, fmt.Errorf("Invalid transport specified %s", transport)
	}
	processor := thrifttest.NewThriftTestProcessor(handler)
	server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
	if err = server.Listen(); err != nil {
		return
	}
	go server.AcceptLoop()
	return server, nil
}
Пример #7
0
func TestBinaryProtocol(t *testing.T) {
	pf := thrift.NewTBinaryProtocolFactoryDefault()
	tf := thrift.NewTTransportFactory()

	addr, _ := net.ResolveTCPAddr("tcp", "localhost:8080")

	channel := thrift.NewTSocketAddr(addr)
	defer channel.Close()

	if openErr := channel.Open(); openErr != nil {
		t.Fatalf("Could not open channel due to '%q'.", openErr)
	}

	effectiveTransport := tf.GetTransport(channel)

	client := NewContainerOfEnumsTestServiceClientFactory(effectiveTransport, pf)

	var request *ContainerOfEnums = NewContainerOfEnums()

	var response *ContainerOfEnums
	var anomaly error

	if response, anomaly = client.Echo(request); anomaly != nil {
		t.Fatalf("Could not get response due to '%q'.", anomaly)
	}

	if request.First != response.First {
		t.Errorf("request.First (%q) != response.First (%q)", request.First, response.First)
	}

	if request.IsSetFirst() != response.IsSetFirst() {
		t.Errorf("request.IsSetFirst() (%q) != response.IsSetFirst() (%q).", request.IsSetFirst(), response.IsSetFirst())
	}

	if request.Second != response.Second {
		t.Errorf("request.Second (%q) != response.Second (%q).", request.Second, response.Second)
	}

	if request.IsSetSecond() != response.IsSetSecond() {
		t.Errorf("request.IsSetSecond() (%q) != response.IsSetSecond() (%q).", request.IsSetSecond(), response.IsSetSecond())
	}

	if request.Third != response.Third {
		t.Errorf("request.Third (%q) != response.Third (%q).", request.Third, response.Third)
	}

	if request.IsSetThird() != response.IsSetThird() {
		t.Errorf("request.IsSetThird() (%q) != response.IsSetThird() (%q).", request.IsSetThird(), response.IsSetThird())
	}

	if request.OptionalFourth != response.OptionalFourth {
		t.Errorf("request.OptionalFourth (%q) != response.OptionalFourth (%q).", request.OptionalFourth, response.OptionalFourth)
	}

	if request.IsSetOptionalFourth() != response.IsSetOptionalFourth() {
		t.Errorf("request.IsSetOptionalFourth() (%q) != response.IsSetOptionalFourth() (%q).", request.IsSetOptionalFourth(), response.IsSetOptionalFourth())
	}

	if request.OptionalFifth != response.OptionalFifth {
		t.Errorf("request.OptionalFifth (%q) != response.OptionalFifth (%q).", request.OptionalFifth, response.OptionalFifth)
	}

	if request.IsSetOptionalFifth() != response.IsSetOptionalFifth() {
		t.Errorf("request.IsSetOptionalFifth() (%q) != response.IsSetOptionalFifth() (%q).", request.IsSetOptionalFifth(), response.IsSetOptionalFifth())
	}

	if request.OptionalSixth != response.OptionalSixth {
		t.Errorf("request.OptionalSixth (%q) != response.OptionalSixth (%q).", request.OptionalSixth, response.OptionalSixth)
	}

	if request.IsSetOptionalSixth() != response.IsSetOptionalSixth() {
		t.Errorf("request.IsSetOptionalSixth() (%q) != response.IsSetOptionalSixth() (%q).", request.IsSetOptionalSixth(), response.IsSetOptionalSixth())
	}

	if request.DefaultSeventh != response.DefaultSeventh {
		t.Errorf("request.DefaultSeventh (%q) != response.DefaultSeventh (%q).", request.DefaultSeventh, response.DefaultSeventh)
	}

	if request.IsSetDefaultSeventh() != response.IsSetDefaultSeventh() {
		t.Errorf("request.IsSetDefaultSeventh() (%q) != response.IsSetDefaultSeventh() (%q).", request.IsSetDefaultSeventh(), response.IsSetDefaultSeventh())
	}

	if request.DefaultEighth != response.DefaultEighth {
		t.Errorf("request.DefaultEighth (%q) != response.DefaultEighth (%q).", request.DefaultEighth, response.DefaultEighth)
	}

	if request.IsSetDefaultEighth() != response.IsSetDefaultEighth() {
		t.Errorf("request.IsSetDefaultEighth() (%q) != response.IsSetDefaultEighth() (%q).", request.IsSetDefaultEighth(), response.IsSetDefaultEighth())
	}

	if request.DefaultNineth != response.DefaultNineth {
		t.Errorf("request.DefaultNineth (%q) != response.DefaultNineth (%q).", request.DefaultNineth, response.DefaultNineth)
	}

	if request.IsSetDefaultNineth() != response.IsSetDefaultNineth() {
		t.Errorf("request.IsSetDefaultNineth() (%q) != response.IsSetDefaultNineth() (%q).", request.IsSetDefaultNineth(), response.IsSetDefaultNineth())
	}

	request.First = UndefinedValues_Two
	request.Second = DefinedValues_Two
	request.Third = HeterogeneousValues_Two
	request.OptionalFourth = UndefinedValues_Three
	request.OptionalFifth = DefinedValues_Three
	request.OptionalSixth = HeterogeneousValues_Three

	if response, anomaly = client.Echo(request); anomaly != nil {
		t.Fatalf("Could not get response due to '%q'.", anomaly)
	}

	if request.First != response.First {
		t.Errorf("request.First (%q) != response.First (%q)", request.First, response.First)
	}

	if request.IsSetFirst() != response.IsSetFirst() {
		t.Errorf("request.IsSetFirst() (%q) != response.IsSetFirst() (%q).", request.IsSetFirst(), response.IsSetFirst())
	}

	if request.Second != response.Second {
		t.Errorf("request.Second (%q) != response.Second (%q).", request.Second, response.Second)
	}

	if request.IsSetSecond() != response.IsSetSecond() {
		t.Errorf("request.IsSetSecond() (%q) != response.IsSetSecond() (%q).", request.IsSetSecond(), response.IsSetSecond())
	}

	if request.Third != response.Third {
		t.Errorf("request.Third (%q) != response.Third (%q).", request.Third, response.Third)
	}

	if request.IsSetThird() != response.IsSetThird() {
		t.Errorf("request.IsSetThird() (%q) != response.IsSetThird() (%q).", request.IsSetThird(), response.IsSetThird())
	}

	if request.OptionalFourth != response.OptionalFourth {
		t.Errorf("request.OptionalFourth (%q) != response.OptionalFourth (%q).", request.OptionalFourth, response.OptionalFourth)
	}

	if request.IsSetOptionalFourth() != response.IsSetOptionalFourth() {
		t.Errorf("request.IsSetOptionalFourth() (%q) != response.IsSetOptionalFourth() (%q).", request.IsSetOptionalFourth(), response.IsSetOptionalFourth())
	}

	if request.OptionalFifth != response.OptionalFifth {
		t.Errorf("request.OptionalFifth (%q) != response.OptionalFifth (%q).", request.OptionalFifth, response.OptionalFifth)
	}

	if request.IsSetOptionalFifth() != response.IsSetOptionalFifth() {
		t.Errorf("request.IsSetOptionalFifth() (%q) != response.IsSetOptionalFifth() (%q).", request.IsSetOptionalFifth(), response.IsSetOptionalFifth())
	}

	if request.OptionalSixth != response.OptionalSixth {
		t.Errorf("request.OptionalSixth (%q) != response.OptionalSixth (%q).", request.OptionalSixth, response.OptionalSixth)
	}

	if request.IsSetOptionalSixth() != response.IsSetOptionalSixth() {
		t.Errorf("request.IsSetOptionalSixth() (%q) != response.IsSetOptionalSixth() (%q).", request.IsSetOptionalSixth(), response.IsSetOptionalSixth())
	}

	if request.DefaultSeventh != response.DefaultSeventh {
		t.Errorf("request.DefaultSeventh (%q) != response.DefaultSeventh (%q).", request.DefaultSeventh, response.DefaultSeventh)
	}

	if request.IsSetDefaultSeventh() != response.IsSetDefaultSeventh() {
		t.Errorf("request.IsSetDefaultSeventh() (%q) != response.IsSetDefaultSeventh() (%q).", request.IsSetDefaultSeventh(), response.IsSetDefaultSeventh())
	}

	if request.DefaultEighth != response.DefaultEighth {
		t.Errorf("request.DefaultEighth (%q) != response.DefaultEighth (%q).", request.DefaultEighth, response.DefaultEighth)
	}

	if request.IsSetDefaultEighth() != response.IsSetDefaultEighth() {
		t.Errorf("request.IsSetDefaultEighth() (%q) != response.IsSetDefaultEighth() (%q).", request.IsSetDefaultEighth(), response.IsSetDefaultEighth())
	}

	if request.DefaultNineth != response.DefaultNineth {
		t.Errorf("request.DefaultNineth (%q) != response.DefaultNineth (%q).", request.DefaultNineth, response.DefaultNineth)
	}

	if request.IsSetDefaultNineth() != response.IsSetDefaultNineth() {
		t.Errorf("request.IsSetDefaultNineth() (%q) != response.IsSetDefaultNineth() (%q).", request.IsSetDefaultNineth(), response.IsSetDefaultNineth())
	}
}
Пример #8
0
func GetServerParams(
	host string,
	port int64,
	domain_socket string,
	transport string,
	protocol string,
	ssl bool,
	certPath string,
	handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) {

	var err error
	hostPort := fmt.Sprintf("%s:%d", host, port)

	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
	case "binary":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
	default:
		return nil, nil, nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
	}
	if debugServerProtocol {
		protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:")
	}

	var serverTransport thrift.TServerTransport
	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
		} else {
			cfg.Certificates = append(cfg.Certificates, cert)
		}
		serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg)
	} else {
		if domain_socket != "" {
			serverTransport, err = thrift.NewTServerSocket(domain_socket)
		} else {
			serverTransport, err = thrift.NewTServerSocket(hostPort)
		}
	}
	if err != nil {
		return nil, nil, nil, nil, err
	}

	var transportFactory thrift.TTransportFactory

	switch transport {
	case "http":
		// there is no such factory, and we don't need any
		transportFactory = nil
	case "framed":
		transportFactory = thrift.NewTTransportFactory()
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	case "buffered":
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	case "zlib":
		transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression)
	case "":
		transportFactory = thrift.NewTTransportFactory()
	default:
		return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
	}
	processor := thrifttest.NewThriftTestProcessor(handler)

	return processor, serverTransport, transportFactory, protocolFactory, nil
}