Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing tests for the go/streamlog package #15064

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions go/streamlog/streamlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package streamlog

import (
"bufio"
"bytes"
"fmt"
"io"
"net"
Expand All @@ -29,6 +30,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/servenv"
)

Expand Down Expand Up @@ -260,3 +263,124 @@ func TestFile(t *testing.T) {
t.Errorf("streamlog file: want %q got %q", want, got)
}
}

func TestShouldEmitLog(t *testing.T) {
origQueryLogFilterTag := queryLogFilterTag
origQueryLogRowThreshold := queryLogRowThreshold
defer func() {
SetQueryLogFilterTag(origQueryLogFilterTag)
SetQueryLogRowThreshold(origQueryLogRowThreshold)
}()

tests := []struct {
sql string
qLogFilterTag string
qLogRowThreshold uint64
rowsAffected uint64
rowsReturned uint64
ok bool
}{
{
sql: "queryLogThreshold smaller than affected and returned",
qLogFilterTag: "",
qLogRowThreshold: 2,
rowsAffected: 7,
rowsReturned: 7,
ok: true,
},
{
sql: "queryLogThreshold greater than affected and returned",
qLogFilterTag: "",
qLogRowThreshold: 27,
rowsAffected: 7,
rowsReturned: 17,
ok: false,
},
{
sql: "this doesn't contains queryFilterTag: TAG",
qLogFilterTag: "special tag",
qLogRowThreshold: 10,
rowsAffected: 7,
rowsReturned: 17,
ok: false,
},
{
sql: "this contains queryFilterTag: TAG",
qLogFilterTag: "TAG",
qLogRowThreshold: 0,
rowsAffected: 7,
rowsReturned: 17,
ok: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.sql, func(t *testing.T) {
SetQueryLogFilterTag(tt.qLogFilterTag)
SetQueryLogRowThreshold(tt.qLogRowThreshold)

require.Equal(t, tt.ok, ShouldEmitLog(tt.sql, tt.rowsAffected, tt.rowsReturned))
})
}
}

func TestGetFormatter(t *testing.T) {
tests := []struct {
name string
logger *StreamLogger[string]
params url.Values
val any
expectedErr string
expectedOutput string
}{
{
name: "unexpected value error",
logger: &StreamLogger[string]{
name: "test-logger",
},
params: url.Values{
"keys": []string{"key1", "key2"},
},
val: "temp val",
expectedOutput: "Error: unexpected value of type string in test-logger!",
expectedErr: "",
},
{
name: "mock formatter",
logger: &StreamLogger[string]{
name: "test-logger",
},
params: url.Values{
"keys": []string{"key1", "key2"},
},
val: &mockFormatter{err: fmt.Errorf("formatter error")},
expectedErr: "formatter error",
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var buffer bytes.Buffer
logFormatterFunc := GetFormatter[string](tt.logger)
err := logFormatterFunc(&buffer, tt.params, tt.val)
if tt.expectedErr == "" {
require.NoError(t, err)
require.Equal(t, tt.expectedOutput, buffer.String())
} else {
require.ErrorContains(t, err, tt.expectedErr)
}
})
}
}

type mockFormatter struct {
called bool
err error
}

func (mf *mockFormatter) Logf(w io.Writer, params url.Values) error {
mf.called = true
return mf.err
}
Loading