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

Fix flacky tests #901

Merged
merged 18 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
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
40 changes: 23 additions & 17 deletions internal/topic/topicwriterinternal/writer_reconnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopicwriter"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawydb"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xatomic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
Expand Down Expand Up @@ -360,14 +361,13 @@ func TestWriterImpl_WaitInit(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

go func() {
// wait until w.WaitInit starts
time.Sleep(time.Millisecond)
cancel()
w.onWriterChange(&SingleStreamWriter{})
}()

_, err := w.WaitInit(ctx)
require.ErrorIs(t, err, ctx.Err())

require.True(t, isClosed(w.firstInitResponseProcessedChan))
})

t.Run("contextDeadlineErrorBeforeStart", func(t *testing.T) {
Expand Down Expand Up @@ -426,50 +426,53 @@ func TestWriterImpl_Reconnect(t *testing.T) {
connectionError error
}

newStream := func(name string, onSendInitCallback func()) *MockRawTopicWriterStream {
newStream := func(name string) *MockRawTopicWriterStream {
strm := NewMockRawTopicWriterStream(mc)
initReq := testCreateInitRequest(w)

streamClosed := make(empty.Chan)
strm.EXPECT().CloseSend().Do(func() {
t.Logf("closed stream: %v", name)
close(streamClosed)
})

strm.EXPECT().Send(&initReq).Do(func(_ interface{}) {
if onSendInitCallback != nil {
onSendInitCallback()
}
t.Logf("sent init request stream: %v", name)
})

strm.EXPECT().Recv().Return(&rawtopicwriter.InitResult{
strm.EXPECT().Recv().Do(func() {
t.Logf("receive init response stream: %v", name)
}).Return(&rawtopicwriter.InitResult{
ServerMessageMetadata: rawtopiccommon.ServerMessageMetadata{Status: rawydb.StatusSuccess},
SessionID: name,
}, nil)

strm.EXPECT().Recv().Do(func() {
t.Logf("waiting close channel: %v", name)
xtest.WaitChannelClosed(t, streamClosed)
t.Logf("channel closed: %v", name)
}).Return(nil, errors.New("test stream closed")).MaxTimes(1)
return strm
}

strm2InitSent := make(empty.Chan)

strm2 := newStream("strm2", func() {
close(strm2InitSent)
})
strm2 := newStream("strm2")
strm2.EXPECT().Send(&rawtopicwriter.WriteRequest{
Messages: []rawtopicwriter.MessageData{
{SeqNo: 1},
},
Codec: rawtopiccommon.CodecRaw,
}).Do(func(_ *rawtopicwriter.WriteRequest) {
t.Logf("strm2 sent message and return retriable error")
}).Return(xerrors.Retryable(errors.New("retriable on strm2")))

strm3 := newStream("strm3", nil)
strm3 := newStream("strm3")
strm3.EXPECT().Send(&rawtopicwriter.WriteRequest{
Messages: []rawtopicwriter.MessageData{
{SeqNo: 1},
},
Codec: rawtopiccommon.CodecRaw,
}).Do(func(_ *rawtopicwriter.WriteRequest) {
t.Logf("strm3 sent message and return unretriable error")
}).Return(errors.New("strm3"))

connectsResult := []connectionAttemptContext{
Expand All @@ -488,17 +491,20 @@ func TestWriterImpl_Reconnect(t *testing.T) {
},
}

connectionAttempt := 0
var connectionAttempt xatomic.Int64
w.cfg.Connect = func(ctx context.Context) (RawTopicWriterStream, error) {
res := connectsResult[connectionAttempt]
connectionAttempt++
attemptIndex := int(connectionAttempt.Add(1)) - 1
t.Logf("connect with attempt index: %v", attemptIndex)
res := connectsResult[attemptIndex]

return res.stream, res.connectionError
}

connectionLoopStopped := make(empty.Chan)
go func() {
defer close(connectionLoopStopped)
w.connectionLoop(ctx)
t.Log("connection loop stopped")
}()

err := w.Write(ctx, newTestMessages(1))
Expand Down
8 changes: 2 additions & 6 deletions tests/integration/basic_example_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,11 @@ func TestBasicExampleNative(t *testing.T) { //nolint:gocyclo
ydb.WithSessionPoolSizeLimit(limit),
ydb.WithConnectionTTL(5*time.Second),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.FromString(os.Getenv("YDB_LOG_SEVERITY_LEVEL"))),
log.WithColoring(),
),
newLoggerWithMinLevel(t, log.FromString(os.Getenv("YDB_LOG_SEVERITY_LEVEL"))),
trace.MatchDetails(`ydb\.(driver|table|discovery|retry|scheme).*`),
),
ydb.WithPanicCallback(func(e interface{}) {
_, _ = fmt.Fprintf(os.Stderr, "panic recovered:%v:\n%s", e, debug.Stack())
os.Exit(1)
t.Fatalf("panic recovered:%v:\n%s", e, debug.Stack())
}),
ydb.WithTraceTable(
*shutdownTrace.Compose(
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/connection_secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
Expand Down Expand Up @@ -53,9 +52,7 @@ func TestConnectionSecure(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func TestConnection(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/connection_with_compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestConnectionWithCompression(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/database_sql_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"database/sql"
"fmt"
"log"
"os"
"testing"

Expand Down Expand Up @@ -101,7 +100,7 @@ func TestDatabaseSqlContainers(t *testing.T) {
return rows.Scan(&testDatabaseSqlContainersVariantStruct{})
},
func() error {
return rows.Scan(&testDatabaseSqlContainersVariantTuple{})
return rows.Scan(&testDatabaseSqlContainersVariantTuple{test: t})
},
func() error {
var v int64
Expand Down Expand Up @@ -273,9 +272,10 @@ func (s *testDatabaseSqlContainersVariantStruct) Scan(res interface{}) error {
}

type testDatabaseSqlContainersVariantTuple struct {
a uint32
b string
c int64
a uint32
b string
c int64
test testing.TB
}

func (s *testDatabaseSqlContainersVariantTuple) Scan(res interface{}) error {
Expand Down Expand Up @@ -305,8 +305,8 @@ func (s *testDatabaseSqlContainersVariantTuple) Scan(res interface{}) error {
return fmt.Errorf("type '%T' is not a `types.Value` type", res)
}

func (*testDatabaseSqlContainersVariantTuple) UnmarshalYDB(res types.RawValue) error {
log.Printf("T: %s", res.Type())
func (s *testDatabaseSqlContainersVariantTuple) UnmarshalYDB(res types.RawValue) error {
s.test.Logf("T: %s", res.Type())
name, index := res.Variant()
var x interface{}
switch index {
Expand All @@ -317,7 +317,7 @@ func (*testDatabaseSqlContainersVariantTuple) UnmarshalYDB(res types.RawValue) e
case 2:
x = res.Int64()
}
log.Printf(
s.test.Logf(
"(tuple variant): %s %s %q %d = %v",
res.Path(), res.Type(), name, index, x,
)
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ func TestDiscovery(t *testing.T) {
ydb.WithConnectionTTL(time.Second*1),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
log.WithColoring(),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|repeater).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
77 changes: 74 additions & 3 deletions tests/integration/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"bytes"
"context"
"database/sql"
"fmt"
"os"
"path"
"strings"
"testing"
"text/template"
"time"
Expand All @@ -17,6 +19,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand Down Expand Up @@ -90,9 +93,7 @@ func (scope *scopeT) Driver(opts ...ydb.Option) *ydb.Driver {
append(opts,
ydb.WithAccessTokenCredentials(token),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
scope.LoggerMinLevel(log.WARN),
trace.DetailsAll,
),
)...,
Expand Down Expand Up @@ -147,6 +148,18 @@ func (scope *scopeT) Folder() string {
}).(string)
}

func (scope *scopeT) Logger() *testLogger {
return scope.Cache(nil, nil, func() (res interface{}, err error) {
return newLogger(scope.t), nil
}).(*testLogger)
}

func (scope *scopeT) LoggerMinLevel(level log.Level) *testLogger {
return scope.Cache(level, nil, func() (res interface{}, err error) {
return newLoggerWithMinLevel(scope.t, level), nil
}).(*testLogger)
}

type tableNameParams struct {
tableName string
createTableQueryTemplate string
Expand Down Expand Up @@ -224,3 +237,61 @@ func (scope *scopeT) TableName(opts ...func(t *tableNameParams)) string {
func (scope *scopeT) TablePath(opts ...func(t *tableNameParams)) string {
return path.Join(scope.Folder(), scope.TableName(opts...))
}

// logger for tests
type testLogger struct {
test testing.TB
testName string
minLevel log.Level

m xsync.Mutex
closed bool
messages []string
}

func newLogger(t testing.TB) *testLogger {
return newLoggerWithMinLevel(t, 0)
}

func newLoggerWithMinLevel(t testing.TB, level log.Level) *testLogger {
logger := &testLogger{
test: t,
testName: t.Name(),
minLevel: level,
}
t.Cleanup(logger.flush)
return logger
}

func (t *testLogger) Log(ctx context.Context, msg string, fields ...log.Field) {
t.test.Helper()
lvl := log.LevelFromContext(ctx)
if lvl < t.minLevel {
return
}

names := log.NamesFromContext(ctx)

loggerName := strings.Join(names, ".")
values := make(map[string]string)
for _, field := range fields {
values[field.Key()] = field.String()
}
timeString := time.Now().UTC().Format("15:04:05.999999999") // RFC3339Nano without date and timezone
message := fmt.Sprintf("%s: %s [%s] %s: %v (%v)", t.testName, timeString, lvl, loggerName, msg, values)
t.m.WithLock(func() {
if t.closed {
_, _ = fmt.Fprintf(os.Stderr, "\nFINISHED TEST %q:\n%s\n\n", t.testName, message)
} else {
t.messages = append(t.messages, message)
}
})
}

func (t *testLogger) flush() {
t.m.WithLock(func() {
t.closed = true
message := "\n" + strings.Join(t.messages, "\n")
t.test.Log(message)
})
}
4 changes: 1 addition & 3 deletions tests/integration/ratelimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func TestRatelimiter(t *testing.T) {
),
ydb.WithBalancer(balancers.SingleConn()),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|ratelimiter|coordination).*`),
),
)
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/scripting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand All @@ -38,9 +37,7 @@ func TestScripting(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.TRACE),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent("scripting"),
Expand Down
Loading
Loading