summaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/feed_test.go
blob: 1bf1ef6ab9ba9f2ebe4fd7605b3906e62186149a (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
package cmd

import (
	"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) {
	httpClient := &mock.HTTPClient{}
	clock := &manualClock{tick: time.Second}
	cli, stdout, stderr := newTestCLI(t)
	cli.httpClient = httpClient
	cli.now = clock.now

	td := t.TempDir()
	jsonFile := filepath.Join(td, "docs.jsonl")
	err := os.WriteFile(jsonFile, []byte(`{
  "put": "id:ns:type::doc1",
  "fields": {"foo": "123"}
}`), 0644)

	require.Nil(t, err)

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

	assert.Equal(t, "", stderr.String())
	assert.Equal(t, `{
  "feeder.seconds": 1.000,
  "feeder.ok.count": 1,
  "feeder.ok.rate": 1.000,
  "feeder.error.count": 0,
  "feeder.inflight.count": 0,
  "http.request.count": 1,
  "http.request.bytes": 25,
  "http.request.MBps": 0.000,
  "http.exception.count": 0,
  "http.response.count": 1,
  "http.response.bytes": 16,
  "http.response.MBps": 0.000,
  "http.response.error.count": 0,
  "http.response.latency.millis.min": 0,
  "http.response.latency.millis.avg": 0,
  "http.response.latency.millis.max": 0,
  "http.response.code.counts": {
    "200": 1
  }
}
`, stdout.String())
}