GetRepo cleanup and testing, fixed unit tests

This commit is contained in:
Philipp Mieden 2019-05-27 12:11:16 +02:00
parent 0aed28b524
commit 8d85fc5f81
6 changed files with 33 additions and 8 deletions

View File

@ -68,6 +68,7 @@ func initTestServer(t testing.TB) (socketAddr, webserverAddr string) {
t.Fatal("test server crashed: ", err)
}
}()
socketClient, errClient := NewClient(socketAddr, 1, time.Duration(time.Millisecond*100))
if errClient != nil {
panic(errClient)
@ -79,6 +80,11 @@ func initTestServer(t testing.TB) (socketAddr, webserverAddr string) {
if err != nil {
continue
}
if len(r) == 0 {
t.Fatal("received empty JSON from GetRepo")
}
if r["dimension_foo"].Nodes["id-a"].Data["baz"].(float64) == float64(1) {
break
}
@ -149,10 +155,18 @@ func TestGetURIs(t *testing.T) {
func TestGetRepo(t *testing.T) {
testWithClients(t, func(c *Client) {
time.Sleep(time.Millisecond * 100)
r, err := c.GetRepo()
if err != nil {
t.Fatal(err)
}
if len(r) == 0 {
t.Fatal("received empty JSON from GetRepo")
}
if r["dimension_foo"].Nodes["id-a"].Data["baz"].(float64) != float64(1) {
t.Fatal("failed to drill deep for data")
}

View File

@ -111,6 +111,7 @@ func (c *socketTransport) call(handler server.Handler, request interface{}, resp
break
}
}
// unmarshal response
responseJSONErr := json.Unmarshal(responseBytes, &serverResponse{Reply: response})
if responseJSONErr != nil {

View File

@ -100,6 +100,9 @@ func (h *history) getCurrentFilename() string {
func (h *history) getCurrent(buf *bytes.Buffer) (err error) {
f, err := os.Open(h.getCurrentFilename())
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(buf, f)
return err

View File

@ -21,17 +21,18 @@ func TestHistoryCurrent(t *testing.T) {
var (
h = testHistory()
test = []byte("test")
b bytes.Buffer
)
err := h.add(test)
if err != nil {
t.Fatal("failed to add: ", err)
}
current, err := h.getCurrent()
err = h.getCurrent(&b)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(current, test) {
t.Fatal(fmt.Sprintf("expected %q, got %q", string(test), string(current)))
if !bytes.Equal(b.Bytes(), test) {
t.Fatal(fmt.Sprintf("expected %q, got %q", string(test), string(b.Bytes())))
}
}

View File

@ -221,12 +221,21 @@ func (repo *Repo) GetRepo() map[string]*content.RepoNode {
}
// WriteRepoBytes get the whole repo in all dimensions
// reads the JSON history file from the Filesystem and copies it directly in to the supplied buffer
// the result is wrapped as service response, e.g: {"reply": <contentData>}
func (repo *Repo) WriteRepoBytes(w io.Writer) {
f, err := os.Open(repo.history.getCurrentFilename())
if err != nil {
Log.Error("failed to serve Repo JSON", zap.Error(err))
}
w.Write([]byte("{\"reply\":"))
_, err = io.Copy(w, f)
if err != nil {
Log.Error("failed to serve Repo JSON", zap.Error(err))
}
w.Write([]byte("}"))
}
// Update - reload contents of repository with json from repo.server

View File

@ -31,6 +31,8 @@ func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, source strin
// handle and process
switch handler {
// case HandlerGetRepo: // This case is handled prior to handleRequest being called.
// since the resulting bytes are written directly in to the http.ResponseWriter / net.Connection
case HandlerGetURIs:
getURIRequest := &requests.URIs{}
processIfJSONIsOk(json.Unmarshal(jsonBytes, &getURIRequest), func() {
@ -51,11 +53,6 @@ func handleRequest(r *repo.Repo, handler Handler, jsonBytes []byte, source strin
processIfJSONIsOk(json.Unmarshal(jsonBytes, &updateRequest), func() {
reply = r.Update()
})
// case HandlerGetRepo:
// repoRequest := &requests.Repo{}
// processIfJSONIsOk(json.Unmarshal(jsonBytes, &repoRequest), func() {
// reply = r.GetRepo()
// })
default:
reply = responses.NewError(1, "unknown handler: "+string(handler))