Merge pull request #6150 from LK4D4/remove_used_from_streamformatter

Remove unused and racy "used" param from streamformatter
This commit is contained in:
Victor Vieux 2014-06-02 11:56:05 -07:00
commit 43b7af1dd1
2 changed files with 68 additions and 10 deletions

View file

@ -8,11 +8,10 @@ import (
type StreamFormatter struct {
json bool
used bool
}
func NewStreamFormatter(json bool) *StreamFormatter {
return &StreamFormatter{json, false}
return &StreamFormatter{json}
}
const streamNewline = "\r\n"
@ -20,7 +19,6 @@ const streamNewline = "\r\n"
var streamNewlineBytes = []byte(streamNewline)
func (sf *StreamFormatter) FormatStream(str string) []byte {
sf.used = true
if sf.json {
b, err := json.Marshal(&JSONMessage{Stream: str})
if err != nil {
@ -32,7 +30,6 @@ func (sf *StreamFormatter) FormatStream(str string) []byte {
}
func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte {
sf.used = true
str := fmt.Sprintf(format, a...)
if sf.json {
b, err := json.Marshal(&JSONMessage{ID: id, Status: str})
@ -45,7 +42,6 @@ func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []b
}
func (sf *StreamFormatter) FormatError(err error) []byte {
sf.used = true
if sf.json {
jsonError, ok := err.(*JSONError)
if !ok {
@ -63,7 +59,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgr
if progress == nil {
progress = &JSONProgress{}
}
sf.used = true
if sf.json {
b, err := json.Marshal(&JSONMessage{
@ -84,10 +79,6 @@ func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgr
return []byte(action + " " + progress.String() + endl)
}
func (sf *StreamFormatter) Used() bool {
return sf.used
}
func (sf *StreamFormatter) Json() bool {
return sf.json
}

View file

@ -0,0 +1,67 @@
package utils
import (
"encoding/json"
"errors"
"reflect"
"testing"
)
func TestFormatStream(t *testing.T) {
sf := NewStreamFormatter(true)
res := sf.FormatStream("stream")
if string(res) != `{"stream":"stream"}`+"\r\n" {
t.Fatalf("%q", res)
}
}
func TestFormatStatus(t *testing.T) {
sf := NewStreamFormatter(true)
res := sf.FormatStatus("ID", "%s%d", "a", 1)
if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
t.Fatalf("%q", res)
}
}
func TestFormatSimpleError(t *testing.T) {
sf := NewStreamFormatter(true)
res := sf.FormatError(errors.New("Error for formatter"))
if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
t.Fatalf("%q", res)
}
}
func TestFormatJSONError(t *testing.T) {
sf := NewStreamFormatter(true)
err := &JSONError{Code: 50, Message: "Json error"}
res := sf.FormatError(err)
if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
t.Fatalf("%q", res)
}
}
func TestFormatProgress(t *testing.T) {
sf := NewStreamFormatter(true)
progress := &JSONProgress{
Current: 15,
Total: 30,
Start: 1,
}
res := sf.FormatProgress("id", "action", progress)
msg := &JSONMessage{}
if err := json.Unmarshal(res, msg); err != nil {
t.Fatal(err)
}
if msg.ID != "id" {
t.Fatalf("ID must be 'id', got: %s", msg.ID)
}
if msg.Status != "action" {
t.Fatalf("Status must be 'action', got: %s", msg.Status)
}
if msg.ProgressMessage != progress.String() {
t.Fatalf("ProgressMessage must be %s, got: %s", progress.String(), msg.ProgressMessage)
}
if !reflect.DeepEqual(msg.Progress, progress) {
t.Fatal("Original progress not equals progress from FormatProgress")
}
}