overflow_gateway_service/server/rpc_test.go
snoop 76b6082f1c added
test
2017-06-21 11:50:14 +09:00

119 lines
2.1 KiB
Go

package server
import (
"testing"
"git.loafle.net/overflow/overflow_gateway_service/gateway"
"flag"
"github.com/golang/glog"
"time"
"fmt"
"os"
"net/http"
"bytes"
"io/ioutil"
"log"
"google.golang.org/grpc"
pb "git.loafle.net/overflow/overflow_api_service/grpc"
"golang.org/x/net/context"
)
func startRpcServerTest() <- chan error{
ch := make(chan error, 2)
go func() {
if err := RunRpc(); err != nil {
ch <- fmt.Errorf("cannot run grpc service: %v", err)
}
}()
go func() {
if err := RunGwRpc(); err != nil {
ch <- fmt.Errorf("cannot run gateway service: %v", err)
}
}()
return ch
}
func callGatewayRpc() string {
time.Sleep(2 * time.Second)
url := "http://localhost:8080/v1/overflow/services"
glog.Infoln("URL : ", url)
var jsonStr = []byte(`{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}`)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
if err != nil {
glog.Error(err)
}
req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Error(err)
}
defer resp.Body.Close()
log.Println("response Status:", resp.Status)
log.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
log.Println("response Body:", string(body))
return string(body)
}
func TestRpcServer(t *testing.T) {
flag.Parse()
defer glog.Flush()
gateway.InitServices()
errch := startRpcServerTest()
chStr := make(chan string , 1)
go func() {
chStr <- callGatewayRpc()
}()
select {
case err := <- errch:
log.Println(err)
os.Exit(1)
case status := <-chStr:
log.Println( status)
os.Exit(1)
}
}
func TestRPC(t *testing.T) {
conn, err := grpc.Dial(":9090", grpc.WithInsecure());
if err != nil {
t.Log(err)
}
client := pb.NewOverflowGatewayClient(conn)
si := pb.ServiceInput{}
si.MethodName = "create"
si.ServiceName = "member"
si.Param = make(map[string]string)
si.Param["1"] = "2222"
so, err := client.ExecServices(context.Background(), &si)
t.Log(so.ResultStr)
}