func testHappyPath(t *testing.T, proto thrift.TProtocolFactory, buffered, framed bool) {
	options := nats.DefaultOptions
	options.Servers = []string{fmt.Sprintf("nats://localhost:%d", port)}
	serverConn, err := options.Connect()
	if err != nil {
		t.Fatal("Failed to connect to gnatsd", err)
	}
	defer serverConn.Close()
	clientConn, err := options.Connect()
	if err != nil {
		t.Fatal("Failed to connect to gnatsd", err)
	}
	defer clientConn.Close()

	var transportFactory thrift.TTransportFactory
	if buffered {
		transportFactory = thrift.NewTBufferedTransportFactory(8192)
	} else {
		transportFactory = thrift.NewTTransportFactory()
	}

	if framed {
		transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
	}

	processor := tutorial.NewCalculatorProcessor(NewCalculatorHandler())
	server := thrift_nats.NewNATSServer(serverConn, subject, -1, -1, processor,
		transportFactory, proto)

	wait := make(chan bool)
	go func() {
		wait <- true
		if err := server.Serve(); err != nil {
			t.Fatal("Failed to start thrift server", err)
		}
	}()
	<-wait
	defer server.Stop()

	// Give the server a chance to start.
	time.Sleep(500 * time.Millisecond)

	transport, err := thrift_nats.NATSTransportFactory(clientConn, subject, time.Second, -1)
	if err != nil {
		t.Fatal("Failed to create NATS transport", err)
	}
	transport = transportFactory.GetTransport(transport)
	if err := transport.Open(); err != nil {
		t.Fatal("Failed to open transport", err)
	}
	defer transport.Close()

	client := tutorial.NewCalculatorClientFactory(transport, proto)
	if err := client.Ping(); err != nil {
		t.Fatal("Ping failed", err)
	}
	if err := client.Zip(); err != nil {
		t.Fatal("Zip failed", err)
	}
	sum, err := client.Add(1, 1)
	if err != nil {
		t.Fatal("Sum failed", err)
	}
	if sum != 2 {
		t.Fatalf("Expected Sum result 2, got %d", sum)
	}

	work := tutorial.NewWork()
	work.Op = tutorial.Operation_DIVIDE
	work.Num1 = 8
	work.Num2 = 4
	quotient, err := client.Calculate(1, work)
	if err != nil {
		t.Fatal("Calculate failed", err)
	}
	if sum != 2 {
		t.Fatalf("Expected Calculate result 4, got %d", quotient)
	}

	work.Op = tutorial.Operation_DIVIDE
	work.Num1 = 1
	work.Num2 = 0
	_, err = client.Calculate(2, work)
	switch v := err.(type) {
	case *tutorial.InvalidOperation:
		break
	default:
		t.Fatalf("Expected Calculate to return InvalidOperation, got %s", v)
	}

	log, err := client.GetStruct(1)
	if err != nil {
		t.Fatal("GetStruct failed", err)
	}
	if log.Value != "2" {
		t.Fatalf("Expected struct value `2`, got `%s`", log.Value)
	}

	log, err = client.GetStruct(2)
	if err != nil {
		t.Fatal("GetStruct failed", err)
	}
	if log != nil {
		t.Fatalf("Expected nil struct, got `%s`", log)
	}
}
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := tutorial.NewCalculatorClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "ping":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "Ping requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.Ping())
		fmt.Print("\n")
		break
	case "add":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "Add requires 2 args")
			flag.Usage()
		}
		tmp0, err7 := (strconv.Atoi(flag.Arg(1)))
		if err7 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		tmp1, err8 := (strconv.Atoi(flag.Arg(2)))
		if err8 != nil {
			Usage()
			return
		}
		argvalue1 := int32(tmp1)
		value1 := argvalue1
		fmt.Print(client.Add(value0, value1))
		fmt.Print("\n")
		break
	case "calculate":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "Calculate requires 2 args")
			flag.Usage()
		}
		tmp0, err9 := (strconv.Atoi(flag.Arg(1)))
		if err9 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		arg10 := flag.Arg(2)
		mbTrans11 := thrift.NewTMemoryBufferLen(len(arg10))
		defer mbTrans11.Close()
		_, err12 := mbTrans11.WriteString(arg10)
		if err12 != nil {
			Usage()
			return
		}
		factory13 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt14 := factory13.GetProtocol(mbTrans11)
		argvalue1 := tutorial.NewWork()
		err15 := argvalue1.Read(jsProt14)
		if err15 != nil {
			Usage()
			return
		}
		value1 := argvalue1
		fmt.Print(client.Calculate(value0, value1))
		fmt.Print("\n")
		break
	case "zip":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "Zip requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.Zip())
		fmt.Print("\n")
		break
	case "getStruct":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetStruct requires 1 args")
			flag.Usage()
		}
		tmp0, err16 := (strconv.Atoi(flag.Arg(1)))
		if err16 != nil {
			Usage()
			return
		}
		argvalue0 := int32(tmp0)
		value0 := argvalue0
		fmt.Print(client.GetStruct(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}