1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-25 08:21:55 +03:00

fix(push): fix tests

This commit is contained in:
Nedyalko Dyakov
2025-07-16 19:28:25 +03:00
parent 84f788ed02
commit 11ecbaf87b
2 changed files with 29 additions and 32 deletions

View File

@ -8,46 +8,47 @@ import (
// Push notification error definitions // Push notification error definitions
// This file contains all error types and messages used by the push notification system // This file contains all error types and messages used by the push notification system
// Error reason constants
const (
// HandlerReasons
ReasonHandlerNil = "handler cannot be nil"
ReasonHandlerExists = "cannot overwrite existing handler"
ReasonHandlerProtected = "handler is protected"
// ProcessorReasons
ReasonPushNotificationsDisabled = "push notifications are disabled"
)
// Common error variables for reuse // Common error variables for reuse
var ( var (
// ErrHandlerNil is returned when attempting to register a nil handler // ErrHandlerNil is returned when attempting to register a nil handler
ErrHandlerNil = errors.New("handler cannot be nil") ErrHandlerNil = errors.New(ReasonHandlerNil)
) )
// Registry errors // Registry errors
// ErrHandlerExists creates an error for when attempting to overwrite an existing handler // ErrHandlerExists creates an error for when attempting to overwrite an existing handler
func ErrHandlerExists(pushNotificationName string) error { func ErrHandlerExists(pushNotificationName string) error {
return NewHandlerError("register", pushNotificationName, "cannot overwrite existing handler", nil) return NewHandlerError("register", pushNotificationName, ReasonHandlerExists, nil)
} }
// ErrProtectedHandler creates an error for when attempting to unregister a protected handler // ErrProtectedHandler creates an error for when attempting to unregister a protected handler
func ErrProtectedHandler(pushNotificationName string) error { func ErrProtectedHandler(pushNotificationName string) error {
return NewHandlerError("unregister", pushNotificationName, "handler is protected", nil) return NewHandlerError("unregister", pushNotificationName, ReasonHandlerProtected, nil)
} }
// VoidProcessor errors // VoidProcessor errors
// ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor // ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
func ErrVoidProcessorRegister(pushNotificationName string) error { func ErrVoidProcessorRegister(pushNotificationName string) error {
return NewProcessorError("void_processor", "register", pushNotificationName, "push notifications are disabled", nil) return NewProcessorError("void_processor", "register", pushNotificationName, ReasonPushNotificationsDisabled, nil)
} }
// ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor // ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
func ErrVoidProcessorUnregister(pushNotificationName string) error { func ErrVoidProcessorUnregister(pushNotificationName string) error {
return NewProcessorError("void_processor", "unregister", pushNotificationName, "push notifications are disabled", nil) return NewProcessorError("void_processor", "unregister", pushNotificationName, ReasonPushNotificationsDisabled, nil)
} }
// Error message constants for consistency
const (
// Error message templates
MsgHandlerNil = "handler cannot be nil"
MsgHandlerExists = "cannot overwrite existing handler for push notification: %s"
MsgProtectedHandler = "cannot unregister protected handler for push notification: %s"
MsgVoidProcessorRegister = "cannot register push notification handler '%s': push notifications are disabled (using void processor)"
MsgVoidProcessorUnregister = "cannot unregister push notification handler '%s': push notifications are disabled (using void processor)"
)
// Error type definitions for advanced error handling // Error type definitions for advanced error handling
// HandlerError represents errors related to handler operations // HandlerError represents errors related to handler operations
@ -124,7 +125,7 @@ func IsHandlerNilError(err error) bool {
// IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler // IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler
func IsHandlerExistsError(err error) bool { func IsHandlerExistsError(err error) bool {
if handlerErr, ok := err.(*HandlerError); ok { if handlerErr, ok := err.(*HandlerError); ok {
return handlerErr.Operation == "register" && handlerErr.Reason == "cannot overwrite existing handler" return handlerErr.Operation == "register" && handlerErr.Reason == ReasonHandlerExists
} }
return false return false
} }
@ -132,7 +133,7 @@ func IsHandlerExistsError(err error) bool {
// IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler // IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler
func IsProtectedHandlerError(err error) bool { func IsProtectedHandlerError(err error) bool {
if handlerErr, ok := err.(*HandlerError); ok { if handlerErr, ok := err.(*HandlerError); ok {
return handlerErr.Operation == "unregister" && handlerErr.Reason == "handler is protected" return handlerErr.Operation == "unregister" && handlerErr.Reason == ReasonHandlerProtected
} }
return false return false
} }
@ -140,7 +141,7 @@ func IsProtectedHandlerError(err error) bool {
// IsVoidProcessorError checks if an error is due to void processor operations // IsVoidProcessorError checks if an error is due to void processor operations
func IsVoidProcessorError(err error) bool { func IsVoidProcessorError(err error) bool {
if procErr, ok := err.(*ProcessorError); ok { if procErr, ok := err.(*ProcessorError); ok {
return procErr.ProcessorType == "void_processor" && procErr.Reason == "push notifications are disabled" return procErr.ProcessorType == "void_processor" && procErr.Reason == ReasonPushNotificationsDisabled
} }
return false return false
} }

View File

@ -1653,27 +1653,23 @@ func TestErrorHelperFunctions(t *testing.T) {
}) })
} }
// TestErrorConstants tests the error message constants // TestErrorConstants tests the error reason constants
func TestErrorConstants(t *testing.T) { func TestErrorConstants(t *testing.T) {
t.Run("ErrorMessageConstants", func(t *testing.T) { t.Run("ErrorReasonConstants", func(t *testing.T) {
if MsgHandlerNil != "handler cannot be nil" { if ReasonHandlerNil != "handler cannot be nil" {
t.Errorf("MsgHandlerNil should be 'handler cannot be nil', got: %s", MsgHandlerNil) t.Errorf("ReasonHandlerNil should be 'handler cannot be nil', got: %s", ReasonHandlerNil)
} }
if MsgHandlerExists != "cannot overwrite existing handler for push notification: %s" { if ReasonHandlerExists != "cannot overwrite existing handler" {
t.Errorf("MsgHandlerExists should be 'cannot overwrite existing handler for push notification: %%s', got: %s", MsgHandlerExists) t.Errorf("ReasonHandlerExists should be 'cannot overwrite existing handler', got: %s", ReasonHandlerExists)
} }
if MsgProtectedHandler != "cannot unregister protected handler for push notification: %s" { if ReasonHandlerProtected != "handler is protected" {
t.Errorf("MsgProtectedHandler should be 'cannot unregister protected handler for push notification: %%s', got: %s", MsgProtectedHandler) t.Errorf("ReasonHandlerProtected should be 'handler is protected', got: %s", ReasonHandlerProtected)
} }
if MsgVoidProcessorRegister != "cannot register push notification handler '%s': push notifications are disabled (using void processor)" { if ReasonPushNotificationsDisabled != "push notifications are disabled" {
t.Errorf("MsgVoidProcessorRegister constant mismatch, got: %s", MsgVoidProcessorRegister) t.Errorf("ReasonPushNotificationsDisabled should be 'push notifications are disabled', got: %s", ReasonPushNotificationsDisabled)
}
if MsgVoidProcessorUnregister != "cannot unregister push notification handler '%s': push notifications are disabled (using void processor)" {
t.Errorf("MsgVoidProcessorUnregister constant mismatch, got: %s", MsgVoidProcessorUnregister)
} }
}) })
} }