File: stats_test.go

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (94 lines) | stat: -rw-r--r-- 2,405 bytes parent folder | download | duplicates (2)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

package ocgremlin

import (
	"context"
	"strings"
	"testing"

	"github.com/facebook/ent/dialect/gremlin"
	"github.com/facebook/ent/dialect/gremlin/encoding/graphson"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	"go.opencensus.io/stats/view"
)

func TestStatsCollection(t *testing.T) {
	err := view.Register(
		RequestCountView,
		ResponseCountView,
		ResponseBytesView,
		RoundTripLatencyView,
	)
	require.NoError(t, err)

	req := gremlin.NewEvalRequest("g.E()")
	rsp := &gremlin.Response{RequestID: req.RequestID}
	rsp.Status.Code = gremlin.StatusSuccess
	rsp.Result.Data = graphson.RawMessage(
		`{"@type": "g:List", "@value": [{"@type": "g:Int32", "@value": 42}]}`,
	)

	transport := &mockTransport{}
	transport.On("RoundTrip", mock.Anything, mock.Anything).
		Return(rsp, nil).
		Once()
	defer transport.AssertExpectations(t)

	rt := &statsTransport{transport}
	_, _ = rt.RoundTrip(context.Background(), req)

	tests := []struct {
		name   string
		expect func(*testing.T, *view.Row)
	}{
		{
			name: "gremlin/request_count",
			expect: func(t *testing.T, row *view.Row) {
				count, ok := row.Data.(*view.CountData)
				require.True(t, ok)
				assert.Equal(t, int64(1), count.Value)
			},
		},
		{
			name: "gremlin/response_count",
			expect: func(t *testing.T, row *view.Row) {
				count, ok := row.Data.(*view.CountData)
				require.True(t, ok)
				assert.Equal(t, int64(1), count.Value)
			},
		},
		{
			name: "gremlin/response_bytes",
			expect: func(t *testing.T, row *view.Row) {
				data, ok := row.Data.(*view.DistributionData)
				require.True(t, ok)
				assert.EqualValues(t, len(rsp.Result.Data), data.Sum())
			},
		},
		{
			name: "gremlin/roundtrip_latency",
			expect: func(t *testing.T, row *view.Row) {
				data, ok := row.Data.(*view.DistributionData)
				require.True(t, ok)
				assert.NotZero(t, data.Sum())
			},
		},
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name[strings.Index(tt.name, "/")+1:], func(t *testing.T) {
			v := view.Find(tt.name)
			assert.NotNil(t, v)
			rows, err := view.RetrieveData(tt.name)
			require.NoError(t, err)
			require.Len(t, rows, 1)
			tt.expect(t, rows[0])
		})
	}
}