From bac61cd083dabe22b72bb201a334effe745ece7c Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Sat, 25 Oct 2025 01:04:41 +0900 Subject: [PATCH 1/3] internal/proto/peek_push_notification_test : Refactor test helpers to use fmt.Fprintf for buffers Replaced buf.WriteString(fmt.Sprintf(...)) with fmt.Fprintf or fmt.Fprint in test helper functions for improved clarity and efficiency. This change affects push notification and RESP3 test utilities. --- internal/proto/peek_push_notification_test.go | 21 ++++++++++--------- push/push_test.go | 12 +++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/internal/proto/peek_push_notification_test.go b/internal/proto/peek_push_notification_test.go index 58a794b849..8aaa9f8a7c 100644 --- a/internal/proto/peek_push_notification_test.go +++ b/internal/proto/peek_push_notification_test.go @@ -295,25 +295,26 @@ func createValidPushNotification(notificationName, data string) *bytes.Buffer { buf := &bytes.Buffer{} simpleOrString := rand.Intn(2) == 0 + defMsg := fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName) if data == "" { // Single element notification buf.WriteString(">1\r\n") if simpleOrString { - buf.WriteString(fmt.Sprintf("+%s\r\n", notificationName)) + fmt.Fprint(buf, defMsg) } else { - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)) + fmt.Fprint(buf, defMsg) } } else { // Two element notification buf.WriteString(">2\r\n") if simpleOrString { - buf.WriteString(fmt.Sprintf("+%s\r\n", notificationName)) - buf.WriteString(fmt.Sprintf("+%s\r\n", data)) + fmt.Fprintf(buf, "+%s\r\n", notificationName) + fmt.Fprintf(buf, "+%s\r\n", data) } else { - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)) - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)) + fmt.Fprint(buf, defMsg) + fmt.Fprint(buf, defMsg) } } @@ -333,14 +334,14 @@ func createPushNotificationWithArgs(notificationName string, args ...string) *by buf := &bytes.Buffer{} totalElements := 1 + len(args) - buf.WriteString(fmt.Sprintf(">%d\r\n", totalElements)) + fmt.Fprintf(buf, ">%d\r\n", totalElements) // Write notification name - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationName), notificationName) // Write arguments for _, arg := range args { - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(arg), arg)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(arg), arg) } return buf @@ -350,7 +351,7 @@ func createPushNotificationWithArgs(notificationName string, args ...string) *by func createSingleElementPushNotification(notificationName string) *bytes.Buffer { buf := &bytes.Buffer{} buf.WriteString(">1\r\n") - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationName), notificationName)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationName), notificationName) return buf } diff --git a/push/push_test.go b/push/push_test.go index 69126f3028..9b8c147f26 100644 --- a/push/push_test.go +++ b/push/push_test.go @@ -837,14 +837,14 @@ func createFakeRESP3PushNotification(notificationType string, args ...string) *b // RESP3 Push notification format: >\r\n\r\n totalElements := 1 + len(args) // notification type + arguments - buf.WriteString(fmt.Sprintf(">%d\r\n", totalElements)) + fmt.Fprintf(buf, ">%d\r\n", totalElements) // Write notification type as bulk string - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(notificationType), notificationType)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationType), notificationType) // Write arguments as bulk strings for _, arg := range args { - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(arg), arg)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(arg), arg) } return buf @@ -868,11 +868,11 @@ func createFakeRESP3Array(elements ...string) *bytes.Buffer { buf := &bytes.Buffer{} // RESP3 Array format: *\r\n\r\n - buf.WriteString(fmt.Sprintf("*%d\r\n", len(elements))) + fmt.Fprintf(buf, "*%d\r\n", len(elements)) // Write elements as bulk strings for _, element := range elements { - buf.WriteString(fmt.Sprintf("$%d\r\n%s\r\n", len(element), element)) + fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(element), element) } return buf @@ -881,7 +881,7 @@ func createFakeRESP3Array(elements ...string) *bytes.Buffer { // createFakeRESP3Error creates a fake RESP3 error func createFakeRESP3Error(message string) *bytes.Buffer { buf := &bytes.Buffer{} - buf.WriteString(fmt.Sprintf("-%s\r\n", message)) + fmt.Fprintf(buf, "-%s\r\n", message) return buf } From 62f8bdb0b284318aa4811b083f2eeace0c84ecd0 Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Sat, 25 Oct 2025 07:28:03 +0900 Subject: [PATCH 2/3] peek_push_notification_test: revert prev formatting --- internal/proto/peek_push_notification_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/proto/peek_push_notification_test.go b/internal/proto/peek_push_notification_test.go index 8aaa9f8a7c..6aa6dff841 100644 --- a/internal/proto/peek_push_notification_test.go +++ b/internal/proto/peek_push_notification_test.go @@ -302,7 +302,7 @@ func createValidPushNotification(notificationName, data string) *bytes.Buffer { // Single element notification buf.WriteString(">1\r\n") if simpleOrString { - fmt.Fprint(buf, defMsg) + fmt.Fprintf(buf, "+%s\r\n", notificationName) } else { fmt.Fprint(buf, defMsg) } From 46523627927893dd5fa27bcd956dfee9bb7856d4 Mon Sep 17 00:00:00 2001 From: Iliya Lyan <68940374+12ya@users.noreply.github.com> Date: Sat, 25 Oct 2025 07:35:58 +0900 Subject: [PATCH 3/3] all: replace buf.WriteString with fmt.FprintF for consistency --- internal/proto/peek_push_notification_test.go | 18 +++++++-------- internal/proto/reader_test.go | 3 ++- push/push_test.go | 22 +++++++++---------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/internal/proto/peek_push_notification_test.go b/internal/proto/peek_push_notification_test.go index 6aa6dff841..7d439e593e 100644 --- a/internal/proto/peek_push_notification_test.go +++ b/internal/proto/peek_push_notification_test.go @@ -95,7 +95,7 @@ func TestPeekPushNotificationName(t *testing.T) { t.Run("NotPushNotification", func(t *testing.T) { // Test with regular array instead of push notification buf := &bytes.Buffer{} - buf.WriteString("*2\r\n$6\r\nMOVING\r\n$4\r\ndata\r\n") + fmt.Fprint(buf, "*2\r\n$6\r\nMOVING\r\n$4\r\ndata\r\n") reader := NewReader(buf) _, err := reader.PeekPushNotificationName() @@ -112,7 +112,7 @@ func TestPeekPushNotificationName(t *testing.T) { t.Run("InsufficientData", func(t *testing.T) { // Test with buffer smaller than peek size - this might panic due to bounds checking buf := &bytes.Buffer{} - buf.WriteString(">") + fmt.Fprint(buf, ">") reader := NewReader(buf) func() { @@ -146,7 +146,7 @@ func TestPeekPushNotificationName(t *testing.T) { t.Run(fmt.Sprintf("Type_%c", respType), func(t *testing.T) { buf := &bytes.Buffer{} buf.WriteByte(respType) - buf.WriteString("test data that fills the buffer completely") + fmt.Fprint(buf, "test data that fills the buffer completely") reader := NewReader(buf) _, err := reader.PeekPushNotificationName() @@ -167,7 +167,7 @@ func TestPeekPushNotificationName(t *testing.T) { t.Run("ZeroLengthArray", func(t *testing.T) { // Create push notification with zero elements: >0\r\n buf := &bytes.Buffer{} - buf.WriteString(">0\r\npadding_data_to_fill_buffer_completely") + fmt.Fprint(buf, ">0\r\npadding_data_to_fill_buffer_completely") reader := NewReader(buf) _, err := reader.PeekPushNotificationName() @@ -209,7 +209,7 @@ func TestPeekPushNotificationName(t *testing.T) { for _, tc := range corruptedCases { t.Run(tc.name, func(t *testing.T) { buf := &bytes.Buffer{} - buf.WriteString(tc.data) + fmt.Fprint(buf, tc.data) reader := NewReader(buf) // Some corrupted data might not error but return unexpected results @@ -230,7 +230,7 @@ func TestPeekPushNotificationName(t *testing.T) { // Create buffer that is exactly 36 bytes (the peek window size) buf := &bytes.Buffer{} // ">1\r\n$4\r\nTEST\r\n" = 14 bytes, need 22 more - buf.WriteString(">1\r\n$4\r\nTEST\r\n1234567890123456789012") + fmt.Fprint(buf, ">1\r\n$4\r\nTEST\r\n1234567890123456789012") if buf.Len() != 36 { t.Errorf("Expected buffer length 36, got %d", buf.Len()) } @@ -300,7 +300,7 @@ func createValidPushNotification(notificationName, data string) *bytes.Buffer { if data == "" { // Single element notification - buf.WriteString(">1\r\n") + fmt.Fprint(buf, ">1\r\n") if simpleOrString { fmt.Fprintf(buf, "+%s\r\n", notificationName) } else { @@ -308,7 +308,7 @@ func createValidPushNotification(notificationName, data string) *bytes.Buffer { } } else { // Two element notification - buf.WriteString(">2\r\n") + fmt.Fprint(buf, ">2\r\n") if simpleOrString { fmt.Fprintf(buf, "+%s\r\n", notificationName) fmt.Fprintf(buf, "+%s\r\n", data) @@ -350,7 +350,7 @@ func createPushNotificationWithArgs(notificationName string, args ...string) *by // createSingleElementPushNotification creates a push notification with single element func createSingleElementPushNotification(notificationName string) *bytes.Buffer { buf := &bytes.Buffer{} - buf.WriteString(">1\r\n") + fmt.Fprint(buf, ">1\r\n") fmt.Fprintf(buf, "$%d\r\n%s\r\n", len(notificationName), notificationName) return buf } diff --git a/internal/proto/reader_test.go b/internal/proto/reader_test.go index 2d5f56c711..ace954fa19 100644 --- a/internal/proto/reader_test.go +++ b/internal/proto/reader_test.go @@ -2,6 +2,7 @@ package proto_test import ( "bytes" + "fmt" "io" "testing" @@ -86,7 +87,7 @@ func TestReader_ReadLine(t *testing.T) { func benchmarkParseReply(b *testing.B, reply string, wanterr bool) { buf := new(bytes.Buffer) for i := 0; i < b.N; i++ { - buf.WriteString(reply) + fmt.Fprint(buf, reply) } p := proto.NewReader(buf) b.ResetTimer() diff --git a/push/push_test.go b/push/push_test.go index 9b8c147f26..b001bfdd33 100644 --- a/push/push_test.go +++ b/push/push_test.go @@ -1117,7 +1117,7 @@ func TestProcessorWithFakeBuffer(t *testing.T) { // Create fake RESP3 push notification with no elements buf := &bytes.Buffer{} - buf.WriteString(">0\r\n") // Empty push notification + fmt.Fprint(buf, ">0\r\n") // Empty push notification reader := createReaderWithPrimedBuffer(buf) ctx := context.Background() @@ -1154,9 +1154,9 @@ func TestProcessorWithFakeBuffer(t *testing.T) { // Create fake RESP3 push notification with integer as first element buf := &bytes.Buffer{} - buf.WriteString(">2\r\n") // 2 elements - buf.WriteString(":123\r\n") // Integer instead of string - buf.WriteString("$4\r\ndata\r\n") // String data + fmt.Fprint(buf, ">2\r\n") // 2 elements + fmt.Fprint(buf, ":123\r\n") // Integer instead of string + fmt.Fprint(buf, "$4\r\ndata\r\n") // String data reader := proto.NewReader(buf) ctx := context.Background() @@ -1273,8 +1273,8 @@ func TestVoidProcessorWithFakeBuffer(t *testing.T) { // Create invalid RESP3 data buf := &bytes.Buffer{} - buf.WriteString(">1\r\n") // Push notification with 1 element - buf.WriteString("invalid\r\n") // Invalid format (should be $\r\n\r\n) + fmt.Fprint(buf, ">1\r\n") // Push notification with 1 element + fmt.Fprint(buf, "invalid\r\n") // Invalid format (should be $\r\n\r\n) reader := proto.NewReader(buf) ctx := context.Background() @@ -1332,9 +1332,9 @@ func TestProcessorErrorHandling(t *testing.T) { // Create buffer with corrupted RESP3 data buf := &bytes.Buffer{} - buf.WriteString(">2\r\n") // Says 2 elements - buf.WriteString("$6\r\nMOVING\r\n") // First element OK - buf.WriteString("corrupted") // Second element corrupted (no proper format) + fmt.Fprint(buf, ">2\r\n") // Says 2 elements + fmt.Fprint(buf, "$6\r\nMOVING\r\n") // First element OK + fmt.Fprint(buf, "corrupted") // Second element corrupted (no proper format) reader := proto.NewReader(buf) ctx := context.Background() @@ -1360,8 +1360,8 @@ func TestProcessorErrorHandling(t *testing.T) { // Create buffer with partial RESP3 data buf := &bytes.Buffer{} - buf.WriteString(">2\r\n") // Says 2 elements - buf.WriteString("$6\r\nMOVING\r\n") // First element OK + fmt.Fprint(buf, ">2\r\n") // Says 2 elements + fmt.Fprint(buf, "$6\r\nMOVING\r\n") // First element OK // Missing second element reader := proto.NewReader(buf)