node_test.go 1020 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package cache
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. )
  7. func TestNodeCache(t *testing.T) {
  8. // Initialize cache for testing
  9. Init(context.Background())
  10. // Mock nodes data for testing
  11. mockNodes := []interface{}{
  12. map[string]interface{}{"id": 1, "name": "node1", "enabled": true},
  13. map[string]interface{}{"id": 2, "name": "node2", "enabled": true},
  14. }
  15. // Test setting cache
  16. SetCachedNodes(mockNodes)
  17. // Test getting from cache
  18. cached, found := GetCachedNodes()
  19. if !found {
  20. t.Error("Expected to find cached nodes")
  21. }
  22. if cached == nil {
  23. t.Error("Expected cached nodes to not be nil")
  24. }
  25. // Test invalidation
  26. InvalidateNodeCache()
  27. _, found = GetCachedNodes()
  28. if found {
  29. t.Error("Expected cache to be invalidated")
  30. }
  31. }
  32. func TestCacheConstants(t *testing.T) {
  33. if NodeCacheKey != "enabled_nodes" {
  34. t.Errorf("Expected NodeCacheKey to be 'enabled_nodes', got %s", NodeCacheKey)
  35. }
  36. if NodeCacheTTL != 10*time.Minute {
  37. t.Errorf("Expected NodeCacheTTL to be 10 minutes, got %v", NodeCacheTTL)
  38. }
  39. }