snapshot_cache_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //go:build unit
  2. package admin
  3. import (
  4. "sync"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestSnapshotCache_SetAndGet(t *testing.T) {
  11. c := newSnapshotCache(5 * time.Second)
  12. entry := c.Set("key1", map[string]string{"hello": "world"})
  13. require.NotEmpty(t, entry.ETag)
  14. require.NotNil(t, entry.Payload)
  15. got, ok := c.Get("key1")
  16. require.True(t, ok)
  17. require.Equal(t, entry.ETag, got.ETag)
  18. }
  19. func TestSnapshotCache_Expiration(t *testing.T) {
  20. c := newSnapshotCache(1 * time.Millisecond)
  21. c.Set("key1", "value")
  22. time.Sleep(5 * time.Millisecond)
  23. _, ok := c.Get("key1")
  24. require.False(t, ok, "expired entry should not be returned")
  25. }
  26. func TestSnapshotCache_GetEmptyKey(t *testing.T) {
  27. c := newSnapshotCache(5 * time.Second)
  28. _, ok := c.Get("")
  29. require.False(t, ok)
  30. }
  31. func TestSnapshotCache_GetMiss(t *testing.T) {
  32. c := newSnapshotCache(5 * time.Second)
  33. _, ok := c.Get("nonexistent")
  34. require.False(t, ok)
  35. }
  36. func TestSnapshotCache_NilReceiver(t *testing.T) {
  37. var c *snapshotCache
  38. _, ok := c.Get("key")
  39. require.False(t, ok)
  40. entry := c.Set("key", "value")
  41. require.Empty(t, entry.ETag)
  42. }
  43. func TestSnapshotCache_SetEmptyKey(t *testing.T) {
  44. c := newSnapshotCache(5 * time.Second)
  45. // Set with empty key should return entry but not store it
  46. entry := c.Set("", "value")
  47. require.NotEmpty(t, entry.ETag)
  48. _, ok := c.Get("")
  49. require.False(t, ok)
  50. }
  51. func TestSnapshotCache_DefaultTTL(t *testing.T) {
  52. c := newSnapshotCache(0)
  53. require.Equal(t, 30*time.Second, c.ttl)
  54. c2 := newSnapshotCache(-1 * time.Second)
  55. require.Equal(t, 30*time.Second, c2.ttl)
  56. }
  57. func TestSnapshotCache_ETagDeterministic(t *testing.T) {
  58. c := newSnapshotCache(5 * time.Second)
  59. payload := map[string]int{"a": 1, "b": 2}
  60. entry1 := c.Set("k1", payload)
  61. entry2 := c.Set("k2", payload)
  62. require.Equal(t, entry1.ETag, entry2.ETag, "same payload should produce same ETag")
  63. }
  64. func TestSnapshotCache_ETagFormat(t *testing.T) {
  65. c := newSnapshotCache(5 * time.Second)
  66. entry := c.Set("k", "test")
  67. // ETag should be quoted hex string: "abcdef..."
  68. require.True(t, len(entry.ETag) > 2)
  69. require.Equal(t, byte('"'), entry.ETag[0])
  70. require.Equal(t, byte('"'), entry.ETag[len(entry.ETag)-1])
  71. }
  72. func TestBuildETagFromAny_UnmarshalablePayload(t *testing.T) {
  73. // channels are not JSON-serializable
  74. etag := buildETagFromAny(make(chan int))
  75. require.Empty(t, etag)
  76. }
  77. func TestSnapshotCache_GetOrLoad_MissThenHit(t *testing.T) {
  78. c := newSnapshotCache(5 * time.Second)
  79. var loads atomic.Int32
  80. entry, hit, err := c.GetOrLoad("key1", func() (any, error) {
  81. loads.Add(1)
  82. return map[string]string{"hello": "world"}, nil
  83. })
  84. require.NoError(t, err)
  85. require.False(t, hit)
  86. require.NotEmpty(t, entry.ETag)
  87. require.Equal(t, int32(1), loads.Load())
  88. entry2, hit, err := c.GetOrLoad("key1", func() (any, error) {
  89. loads.Add(1)
  90. return map[string]string{"unexpected": "value"}, nil
  91. })
  92. require.NoError(t, err)
  93. require.True(t, hit)
  94. require.Equal(t, entry.ETag, entry2.ETag)
  95. require.Equal(t, int32(1), loads.Load())
  96. }
  97. func TestSnapshotCache_GetOrLoad_ConcurrentSingleflight(t *testing.T) {
  98. c := newSnapshotCache(5 * time.Second)
  99. var loads atomic.Int32
  100. start := make(chan struct{})
  101. const callers = 8
  102. errCh := make(chan error, callers)
  103. var wg sync.WaitGroup
  104. wg.Add(callers)
  105. for range callers {
  106. go func() {
  107. defer wg.Done()
  108. <-start
  109. _, _, err := c.GetOrLoad("shared", func() (any, error) {
  110. loads.Add(1)
  111. time.Sleep(20 * time.Millisecond)
  112. return "value", nil
  113. })
  114. errCh <- err
  115. }()
  116. }
  117. close(start)
  118. wg.Wait()
  119. close(errCh)
  120. for err := range errCh {
  121. require.NoError(t, err)
  122. }
  123. require.Equal(t, int32(1), loads.Load())
  124. }
  125. func TestParseBoolQueryWithDefault(t *testing.T) {
  126. tests := []struct {
  127. name string
  128. raw string
  129. def bool
  130. want bool
  131. }{
  132. {"empty returns default true", "", true, true},
  133. {"empty returns default false", "", false, false},
  134. {"1", "1", false, true},
  135. {"true", "true", false, true},
  136. {"TRUE", "TRUE", false, true},
  137. {"yes", "yes", false, true},
  138. {"on", "on", false, true},
  139. {"0", "0", true, false},
  140. {"false", "false", true, false},
  141. {"FALSE", "FALSE", true, false},
  142. {"no", "no", true, false},
  143. {"off", "off", true, false},
  144. {"whitespace trimmed", " true ", false, true},
  145. {"unknown returns default true", "maybe", true, true},
  146. {"unknown returns default false", "maybe", false, false},
  147. }
  148. for _, tc := range tests {
  149. t.Run(tc.name, func(t *testing.T) {
  150. got := parseBoolQueryWithDefault(tc.raw, tc.def)
  151. require.Equal(t, tc.want, got)
  152. })
  153. }
  154. }