From a44e0e0f4b1a3d18060dc97ffca9a11e83bb2c13 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Thu, 23 Jun 2016 20:36:59 -0400 Subject: [PATCH] add test example --- Makefile | 11 +++++++++++ circle.yml | 23 +++++++++++++++++++++++ test/data.json | 6 ++++++ test/main.go | 35 +++++++++++++++++++++++++++++++++++ test/test.sh | 23 +++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 Makefile create mode 100644 circle.yml create mode 100644 test/data.json create mode 100644 test/main.go create mode 100644 test/test.sh diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..6d72cf6ff --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +.PHONY: all test get_deps + +all: test + +test: + go test github.com/tendermint/go-rpc/... + cd ./test && bash test.sh + + +get_deps: + go get -t -d github.com/tendermint/go-rpc/... diff --git a/circle.yml b/circle.yml new file mode 100644 index 000000000..f022d5a6b --- /dev/null +++ b/circle.yml @@ -0,0 +1,23 @@ +machine: + environment: + GOPATH: /home/ubuntu/.go_workspace + REPO: $GOPATH/src/github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME + hosts: + circlehost: 127.0.0.1 + localhost: 127.0.0.1 + +checkout: + post: + - rm -rf $REPO + - mkdir -p $HOME/.go_workspace/src/github.com/$CIRCLE_PROJECT_USERNAME + - mv $HOME/$CIRCLE_PROJECT_REPONAME $REPO + # - git submodule sync + # - git submodule update --init # use submodules + +dependencies: + override: + - "cd $REPO && make get_deps" + +test: + override: + - "cd $REPO && make test" diff --git a/test/data.json b/test/data.json new file mode 100644 index 000000000..eac2e0dfe --- /dev/null +++ b/test/data.json @@ -0,0 +1,6 @@ +{ + "jsonrpc":"2.0", + "id":"", + "method":"hello_world", + "params":["my_world", 5] +} diff --git a/test/main.go b/test/main.go new file mode 100644 index 000000000..d14ab05f5 --- /dev/null +++ b/test/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "net/http" + + . "github.com/tendermint/go-common" + rpcserver "github.com/tendermint/go-rpc/server" +) + +var routes = map[string]*rpcserver.RPCFunc{ + "hello_world": rpcserver.NewRPCFunc(HelloWorld, "name,num"), +} + +func HelloWorld(name string, num int) (Result, error) { + return Result{fmt.Sprintf("hi %s %d", name, num)}, nil +} + +type Result struct { + Result string +} + +func main() { + mux := http.NewServeMux() + rpcserver.RegisterRPCFuncs(mux, routes) + _, err := rpcserver.StartHTTPServer("0.0.0.0:8008", mux) + if err != nil { + Exit(err.Error()) + } + + // Wait forever + TrapSignal(func() { + }) + +} diff --git a/test/test.sh b/test/test.sh new file mode 100644 index 000000000..3d1f599dd --- /dev/null +++ b/test/test.sh @@ -0,0 +1,23 @@ +#! /bin/bash +set -e + +go build -o server main.go +./server > /dev/null & +PID=$! +sleep 2 + + +R1=`curl -s 'http://localhost:8008/hello_world?name="my_world"&num=5'` + + +R2=`curl -s --data @data.json http://localhost:8008` + +kill -9 $PID + +if [[ "$R1" != "$R2" ]]; then + echo "responses are not identical:" + echo "R1: $R1" + echo "R2: $R2" + exit 1 +fi +echo "Success"