influxdata/line-protocol

func (e *Encoder) Encode(m Metric) (int, error) returns wrong int value

Closed this issue · 4 comments

...
	}

	e.w.Write(e.pair)

	pairsLen += len(e.pair)
	firstField = false
...

e.w.Write(e.pair) error should be handled, also we should add its int value to totalWritten.

Simple test which shows that Encode function returns wrong int:

func TestEncoder_Encode(t *testing.T) {
	var b bytes.Buffer
	now :=  time.Now()
	e := NewEncoder(&b)
	i, _ := e.Encode(&metric{
		name:   "test",
		tags:   nil,
		fields: []*Field{
			{
				Key:   "test_key",
				Value: "test_value",
			},
		},
		tm:     now,
	})
	if i != len(b.Bytes()) {
		t.Fatalf("Expected i: %v, got: %v", i, len(b.Bytes()))
	}
}

Could not push a new branch to fix it. So leaving my lines of code here:
encoder.go - now handling error and adding i to totalWritten

		i, err = e.w.Write(e.pair)
		if err != nil {
			return 0, err
		}
		totalWritten += i

encoder_test.go - in TestEncoder we use variable i (instead of _) and compare it to len(buf.Bytes())

			i, err := serializer.Encode(tt.input)
			if tt.err != err {
				t.Fatalf("expected error %v, but got %v", tt.err, err)
			}
			if i != len(buf.Bytes()) {
				t.Fatalf("expected i: %v, but got: %v", len(buf.Bytes()), i)
			}

@Gleb-ho thank you! good catch. Yeah -- you shouldn't be able to push a new branch. Would you like to try to fork the project and submit a PR? I'd be happy to get it reviewed and try to usher it through the pipeline.

Here we are: #16

thank you @Gleb-ho! your PR was merged. @docmerlin, do you think this is closable?