summaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/feed_test.go
blob: bd0b9544e371e9e4af0a9fb51eb4bdd34f5d01cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cmd

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

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vespa-engine/vespa/client/go/internal/mock"
)

type manualClock struct {
	t    time.Time
	tick time.Duration
}

func (c *manualClock) now() time.Time {
	t := c.t
	c.t = c.t.Add(c.tick)
	return t
}

func TestFeed(t *testing.T) {
	clock := &manualClock{tick: time.Second}
	cli, stdout, stderr := newTestCLI(t)
	httpClient := cli.httpClient.(*mock.HTTPClient)
	httpClient.ReadBody = true
	cli.now = clock.now

	td := t.TempDir()
	doc := []byte(`{
  "put": "id:ns:type::doc1",
  "fields": {"foo": "123"}
}`)
	jsonFile1 := filepath.Join(td, "docs1.jsonl")
	jsonFile2 := filepath.Join(td, "docs2.jsonl")
	require.Nil(t, os.WriteFile(jsonFile1, doc, 0644))
	require.Nil(t, os.WriteFile(jsonFile2, doc, 0644))

	httpClient.NextResponseString(200, `{"message":"OK"}`)
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	require.Nil(t, cli.Run("feed", jsonFile1, jsonFile2))

	assert.Equal(t, "", stderr.String())
	want := `{
  "feeder.seconds": 5.000,
  "feeder.ok.count": 2,
  "feeder.ok.rate": 0.400,
  "feeder.error.count": 0,
  "feeder.inflight.count": 0,
  "http.request.count": 2,
  "http.request.bytes": 50,
  "http.request.MBps": 0.000,
  "http.exception.count": 0,
  "http.response.count": 2,
  "http.response.bytes": 32,
  "http.response.MBps": 0.000,
  "http.response.error.count": 0,
  "http.response.latency.millis.min": 1000,
  "http.response.latency.millis.avg": 1000,
  "http.response.latency.millis.max": 1000,
  "http.response.code.counts": {
    "200": 2
  }
}
`
	assert.Equal(t, want, stdout.String())

	stdout.Reset()
	var stdinBuf bytes.Buffer
	stdinBuf.Write(doc)
	stdinBuf.Write(doc)
	cli.Stdin = &stdinBuf
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	require.Nil(t, cli.Run("feed", "-"))
	assert.Equal(t, want, stdout.String())
}