Skip to content

Commit

Permalink
add client metadata api
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjeffery committed Oct 30, 2016
1 parent aedbf89 commit d45bcd6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
42 changes: 32 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
package client

type Client struct {
}

func New() *Client {
return &Client{}
}
import (
"bytes"
"encoding/json"
"net/http"

func (c *Client) Metadata() {
"github.com/travisjeffery/jocko/server"
)

type Options struct {
Addr string
}

func (c *Client) Produce() {

type Client struct {
Options
c *http.Client
}

func (c *Client) Fetch() {
func New(options Options) *Client {
return &Client{
Options: options,
c: &http.Client{},
}
}

func (c *Client) Metadata(topics ...string) (metadata server.MetadataResponse, err error) {
req := server.MetadataRequest{Topics: topics}
b := new(bytes.Buffer)
if err = json.NewEncoder(b).Encode(req); err != nil {
return
}
res, err := c.c.Post(c.Addr+"/metadata", "application/json", b)
if err != nil {
return metadata, err
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&metadata); err != nil {
return metadata, err
}
return metadata, nil
}
69 changes: 69 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package client

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/travisjeffery/jocko/broker"
"github.com/travisjeffery/jocko/server"
)

type ClientTestSuite struct {
suite.Suite

broker *broker.Broker
server *server.Server
}

func (suite *ClientTestSuite) SetupSuite() {
dir := os.TempDir()
os.RemoveAll(dir)
logs := filepath.Join(dir, "logs")
assert.NoError(suite.T(), os.MkdirAll(logs, 0755))

data := filepath.Join(dir, "data")
assert.NoError(suite.T(), os.MkdirAll(data, 0755))

suite.broker = broker.New(broker.Options{
DataDir: data,
BindAddr: ":0",
LogDir: logs,
})
assert.NoError(suite.T(), suite.broker.Open())

_, err := suite.broker.WaitForLeader(10 * time.Second)
assert.NoError(suite.T(), err)

err = suite.broker.CreateTopic("my_topic", 2)
assert.NoError(suite.T(), err)

suite.server = server.New("localhost:3000", suite.broker)
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), suite.server)
assert.NoError(suite.T(), suite.server.Start())
}

func (suite *ClientTestSuite) TeardownSuite() {
suite.broker.Close()
suite.server.Close()
}

func (suite *ClientTestSuite) TestRefreshMetadata() {
c := New(Options{
Addr: "http://localhost:3000",
})
metadata, err := c.Metadata()
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), ":0", metadata.ControllerID)
assert.Equal(suite.T(), ":0", metadata.Brokers[0].ID)
assert.Equal(suite.T(), "my_topic", metadata.TopicMetadata[0].Topic)
assert.Equal(suite.T(), ":0", metadata.TopicMetadata[0].PartitionMetadata[0].Leader)
}

func TestClient(t *testing.T) {
suite.Run(t, new(ClientTestSuite))
}

0 comments on commit d45bcd6

Please sign in to comment.