From 260dd5462cb92aea737f6a30b6c3d23c593e2ebf Mon Sep 17 00:00:00 2001 From: Nikita Vasilev Date: Fri, 6 Oct 2023 20:29:21 +0300 Subject: [PATCH] Fix config related stats for a default config (#1202) --- writer.go | 14 ++++---- writer_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 7 deletions(-) diff --git a/writer.go b/writer.go index b74467bed..3c7af907a 100644 --- a/writer.go +++ b/writer.go @@ -891,13 +891,13 @@ func (w *Writer) Stats() WriterStats { Retries: stats.retries.snapshot(), BatchSize: stats.batchSize.snapshot(), BatchBytes: stats.batchSizeBytes.snapshot(), - MaxAttempts: int64(w.MaxAttempts), - WriteBackoffMin: w.WriteBackoffMin, - WriteBackoffMax: w.WriteBackoffMax, - MaxBatchSize: int64(w.BatchSize), - BatchTimeout: w.BatchTimeout, - ReadTimeout: w.ReadTimeout, - WriteTimeout: w.WriteTimeout, + MaxAttempts: int64(w.maxAttempts()), + WriteBackoffMin: w.writeBackoffMin(), + WriteBackoffMax: w.writeBackoffMax(), + MaxBatchSize: int64(w.batchSize()), + BatchTimeout: w.batchTimeout(), + ReadTimeout: w.readTimeout(), + WriteTimeout: w.writeTimeout(), RequiredAcks: int64(w.RequiredAcks), Async: w.Async, Topic: w.Topic, diff --git a/writer_test.go b/writer_test.go index b1f6246c8..6f894ecd3 100644 --- a/writer_test.go +++ b/writer_test.go @@ -179,6 +179,14 @@ func TestWriter(t *testing.T) { scenario: "test default configuration values", function: testWriterDefaults, }, + { + scenario: "test default stats values", + function: testWriterDefaultStats, + }, + { + scenario: "test stats values with override config", + function: testWriterOverrideConfigStats, + }, { scenario: "test write message with writer data", function: testWriteMessageWithWriterData, @@ -944,6 +952,84 @@ func testWriterDefaults(t *testing.T) { } } +func testWriterDefaultStats(t *testing.T) { + w := &Writer{} + defer w.Close() + + stats := w.Stats() + + if stats.MaxAttempts == 0 { + t.Error("Incorrect default MaxAttempts value") + } + + if stats.WriteBackoffMin == 0 { + t.Error("Incorrect default WriteBackoffMin value") + } + + if stats.WriteBackoffMax == 0 { + t.Error("Incorrect default WriteBackoffMax value") + } + + if stats.MaxBatchSize == 0 { + t.Error("Incorrect default MaxBatchSize value") + } + + if stats.BatchTimeout == 0 { + t.Error("Incorrect default BatchTimeout value") + } + + if stats.ReadTimeout == 0 { + t.Error("Incorrect default ReadTimeout value") + } + + if stats.WriteTimeout == 0 { + t.Error("Incorrect default WriteTimeout value") + } +} + +func testWriterOverrideConfigStats(t *testing.T) { + w := &Writer{ + MaxAttempts: 6, + WriteBackoffMin: 2, + WriteBackoffMax: 4, + BatchSize: 1024, + BatchTimeout: 16, + ReadTimeout: 24, + WriteTimeout: 32, + } + defer w.Close() + + stats := w.Stats() + + if stats.MaxAttempts != 6 { + t.Error("Incorrect MaxAttempts value") + } + + if stats.WriteBackoffMin != 2 { + t.Error("Incorrect WriteBackoffMin value") + } + + if stats.WriteBackoffMax != 4 { + t.Error("Incorrect WriteBackoffMax value") + } + + if stats.MaxBatchSize != 1024 { + t.Error("Incorrect MaxBatchSize value") + } + + if stats.BatchTimeout != 16 { + t.Error("Incorrect BatchTimeout value") + } + + if stats.ReadTimeout != 24 { + t.Error("Incorrect ReadTimeout value") + } + + if stats.WriteTimeout != 32 { + t.Error("Incorrect WriteTimeout value") + } +} + type staticBalancer struct { partition int }