From 50e92a018e317e7c807506b2b2fd586803ef62ca Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Sun, 14 Jan 2018 21:59:51 -0500 Subject: [PATCH] add test boilerplate for sharding client Former-commit-id: 7cbd81db9421e1517ad92829a2dbbdfcf5f38699 [formerly 76607161435699e18a970c82f9838d20e94f04d7] Former-commit-id: f6db97246aa70eda35e59a2a05fa55316289bd76 --- cmd/geth/shardingcmd.go | 2 -- sharding/client.go | 37 +++++++++++++++++++++++++--- sharding/client_test.go | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 sharding/client_test.go diff --git a/cmd/geth/shardingcmd.go b/cmd/geth/shardingcmd.go index 9923c9e284..9eca9a907e 100644 --- a/cmd/geth/shardingcmd.go +++ b/cmd/geth/shardingcmd.go @@ -1,7 +1,6 @@ package main import ( - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/sharding" "github.com/ethereum/go-ethereum/cmd/utils" @@ -20,7 +19,6 @@ var ( ) func shardingClient(ctx *cli.Context) error { - log.Info("hello world!") c := sharding.MakeShardingClient(ctx) if err := c.Start(); err != nil { return err diff --git a/sharding/client.go b/sharding/client.go index 2392bf63bd..d73ab25d9a 100644 --- a/sharding/client.go +++ b/sharding/client.go @@ -1,29 +1,58 @@ package sharding import ( + "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/node" + "github.com/ethereum/go-ethereum/rpc" cli "gopkg.in/urfave/cli.v1" ) +const ( + // TODO: Can this be referenced from main.clientIdentifier? + clientIdentifier = "geth" // Client identifier to advertise over the network +) + type Client struct { + endpoint string + client *rpc.Client } func MakeShardingClient(ctx *cli.Context) *Client { - // TODO: Setup client - return &Client{} + endpoint := "" + if ctx.GlobalIsSet(utils.DataDirFlag.Name) { + endpoint = ctx.GlobalString(utils.DataDirFlag.Name) + } + + return &Client{ + endpoint: endpoint, + } } func (c *Client) Start() error { log.Info("Starting sharding client") - // TODO: Dial to RPC - // TODO: Verify VMC + rpcClient, err := dialRPC(c.endpoint) + if err != nil { + return err + } + c.client = rpcClient + defer c.client.Close() if err := c.verifyVMC(); err != nil { return err } + // TODO: Wait to be selected? + return nil } func (c *Client) Wait() { // TODO: Blocking lock } + +func dialRPC(endpoint string) (*rpc.Client, error) { + if endpoint == "" { + endpoint = node.DefaultIPCEndpoint(clientIdentifier) + } + return rpc.Dial(endpoint) +} diff --git a/sharding/client_test.go b/sharding/client_test.go new file mode 100644 index 0000000000..10badf2444 --- /dev/null +++ b/sharding/client_test.go @@ -0,0 +1,54 @@ +package sharding + +import ( + "flag" + "fmt" + "math/rand" + "os" + "testing" + + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/rpc" + cli "gopkg.in/urfave/cli.v1" +) + +func randomEndpoint() string { + return fmt.Sprintf("/tmp/go-ethereum-test-ipc-%d-%d", os.Getpid(), rand.Int63()) +} + +func newTestServer(endpoint string) (*rpc.Server, error) { + server := rpc.NewServer() + + l, err := rpc.CreateIPCListener(endpoint) + if err != nil { + return nil, err + } + go server.ServeListener(l) + + return server, nil +} + +func createContext() *cli.Context { + set := flag.NewFlagSet("test", 0) + set.String(utils.DataDirFlag.Name, "", "") + return cli.NewContext(nil, set, nil) +} + +func TestStart(t *testing.T) { + endpoint := randomEndpoint() + server, err := newTestServer(endpoint) + if err != nil { + t.Fatalf("Failed to create a test server: %v", err) + } + defer server.Stop() + + ctx := createContext() + if err := ctx.GlobalSet(utils.DataDirFlag.Name, endpoint); err != nil { + t.Fatalf("Failed to set global variable for flag %s. Error: %v", utils.DataDirFlag.Name, err) + } + + c := MakeShardingClient(ctx) + if err := c.Start(); err != nil { + t.Errorf("Failed to start server: %v", err) + } +}