Files
mev-beta/orig/pkg/lifecycle/health_monitor_test.go
Administrator 803de231ba feat: create v2-prep branch with comprehensive planning
Restructured project for V2 refactor:

**Structure Changes:**
- Moved all V1 code to orig/ folder (preserved with git mv)
- Created docs/planning/ directory
- Added orig/README_V1.md explaining V1 preservation

**Planning Documents:**
- 00_V2_MASTER_PLAN.md: Complete architecture overview
  - Executive summary of critical V1 issues
  - High-level component architecture diagrams
  - 5-phase implementation roadmap
  - Success metrics and risk mitigation

- 07_TASK_BREAKDOWN.md: Atomic task breakdown
  - 99+ hours of detailed tasks
  - Every task < 2 hours (atomic)
  - Clear dependencies and success criteria
  - Organized by implementation phase

**V2 Key Improvements:**
- Per-exchange parsers (factory pattern)
- Multi-layer strict validation
- Multi-index pool cache
- Background validation pipeline
- Comprehensive observability

**Critical Issues Addressed:**
- Zero address tokens (strict validation + cache enrichment)
- Parsing accuracy (protocol-specific parsers)
- No audit trail (background validation channel)
- Inefficient lookups (multi-index cache)
- Stats disconnection (event-driven metrics)

Next Steps:
1. Review planning documents
2. Begin Phase 1: Foundation (P1-001 through P1-010)
3. Implement parsers in Phase 2
4. Build cache system in Phase 3
5. Add validation pipeline in Phase 4
6. Migrate and test in Phase 5

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 10:14:26 +01:00

107 lines
3.3 KiB
Go

package lifecycle
import (
"fmt"
"strings"
"testing"
"time"
)
type stubHealthNotifier struct {
failUntil int
attempts int
txHash string
}
func (s *stubHealthNotifier) NotifyHealthChange(moduleID string, oldHealth, newHealth ModuleHealth) error {
s.attempts++
if s.attempts <= s.failUntil {
return fmt.Errorf("notify failure %d for tx %s", s.attempts, s.txHash)
}
return nil
}
func (s *stubHealthNotifier) NotifySystemHealth(health OverallHealth) error {
return nil
}
func (s *stubHealthNotifier) NotifyAlert(alert HealthAlert) error {
return nil
}
func TestHealthMonitorNotifyWithRetrySuccess(t *testing.T) {
config := HealthMonitorConfig{
NotificationRetries: 3,
NotificationRetryDelay: time.Nanosecond,
}
hm := NewHealthMonitor(config)
hm.logger = nil
notifier := &stubHealthNotifier{failUntil: 2, txHash: "0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"}
hm.notifier = notifier
err := hm.notifyWithRetry(func() error {
return notifier.NotifyHealthChange("module", ModuleHealth{}, ModuleHealth{})
}, "notify failure", "module_id", "module")
if err != nil {
t.Fatalf("expected notification to eventually succeed, got %v", err)
}
if notifier.attempts != 3 {
t.Fatalf("expected 3 attempts, got %d", notifier.attempts)
}
if errs := hm.NotificationErrors(); len(errs) != 0 {
t.Fatalf("expected no recorded notification errors, got %d", len(errs))
}
if hm.aggregatedNotificationError() != nil {
t.Fatal("expected aggregated notification error to be nil")
}
}
func TestHealthMonitorNotifyWithRetryFailure(t *testing.T) {
config := HealthMonitorConfig{
NotificationRetries: 2,
NotificationRetryDelay: time.Nanosecond,
}
hm := NewHealthMonitor(config)
hm.logger = nil
txHash := "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
notifier := &stubHealthNotifier{failUntil: 3, txHash: txHash}
hm.notifier = notifier
err := hm.notifyWithRetry(func() error {
return notifier.NotifyHealthChange("module", ModuleHealth{}, ModuleHealth{})
}, "notify failure", "module_id", "module")
if err == nil {
t.Fatal("expected notification to fail after retries")
}
if notifier.attempts != 2 {
t.Fatalf("expected 2 attempts, got %d", notifier.attempts)
}
exported := hm.NotificationErrors()
if len(exported) != 1 {
t.Fatalf("expected 1 recorded notification error, got %d", len(exported))
}
copyErrs := hm.NotificationErrors()
if copyErrs[0] == nil {
t.Fatal("expected copy of notification errors to retain value")
}
if got := exported[0].Error(); !strings.Contains(got, txHash) {
t.Fatalf("recorded notification error should include tx hash, got %q", got)
}
details := hm.NotificationErrorDetails()
if len(details) != 1 {
t.Fatalf("expected notification error details entry, got %d", len(details))
}
if details[0].TxHash != txHash {
t.Fatalf("expected notification error detail to track tx hash %s, got %s", txHash, details[0].TxHash)
}
agg := hm.aggregatedNotificationError()
if agg == nil {
t.Fatal("expected aggregated notification error to be returned")
}
if got := agg.Error(); !strings.Contains(got, "notify failure") || !strings.Contains(got, "notify failure 1") || !strings.Contains(got, txHash) {
t.Fatalf("aggregated notification error should include failure details and tx hash, got %q", got)
}
}