Ver código fonte

Remove unused and racy "used" param from streamformatter

Also tests written
Docker-DCO-1.1-Signed-off-by: Alexandr Morozov <lk4d4math@gmail.com> (github: LK4D4)
Alexandr Morozov 11 anos atrás
pai
commit
1ae37cef91
2 arquivos alterados com 68 adições e 10 exclusões
  1. 1 10
      utils/streamformatter.go
  2. 67 0
      utils/streamformatter_test.go

+ 1 - 10
utils/streamformatter.go

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

+ 67 - 0
utils/streamformatter_test.go

@@ -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")
+	}
+}