setting_service_backend_mode_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "errors"
  6. "testing"
  7. "time"
  8. "github.com/Wei-Shaw/sub2api/internal/config"
  9. "github.com/stretchr/testify/require"
  10. )
  11. type bmRepoStub struct {
  12. getValueFn func(ctx context.Context, key string) (string, error)
  13. calls int
  14. }
  15. func (s *bmRepoStub) Get(ctx context.Context, key string) (*Setting, error) {
  16. panic("unexpected Get call")
  17. }
  18. func (s *bmRepoStub) GetValue(ctx context.Context, key string) (string, error) {
  19. s.calls++
  20. if s.getValueFn == nil {
  21. panic("unexpected GetValue call")
  22. }
  23. return s.getValueFn(ctx, key)
  24. }
  25. func (s *bmRepoStub) Set(ctx context.Context, key, value string) error {
  26. panic("unexpected Set call")
  27. }
  28. func (s *bmRepoStub) GetMultiple(ctx context.Context, keys []string) (map[string]string, error) {
  29. panic("unexpected GetMultiple call")
  30. }
  31. func (s *bmRepoStub) SetMultiple(ctx context.Context, settings map[string]string) error {
  32. panic("unexpected SetMultiple call")
  33. }
  34. func (s *bmRepoStub) GetAll(ctx context.Context) (map[string]string, error) {
  35. panic("unexpected GetAll call")
  36. }
  37. func (s *bmRepoStub) Delete(ctx context.Context, key string) error {
  38. panic("unexpected Delete call")
  39. }
  40. type bmUpdateRepoStub struct {
  41. updates map[string]string
  42. getValueFn func(ctx context.Context, key string) (string, error)
  43. }
  44. func (s *bmUpdateRepoStub) Get(ctx context.Context, key string) (*Setting, error) {
  45. panic("unexpected Get call")
  46. }
  47. func (s *bmUpdateRepoStub) GetValue(ctx context.Context, key string) (string, error) {
  48. if s.getValueFn == nil {
  49. panic("unexpected GetValue call")
  50. }
  51. return s.getValueFn(ctx, key)
  52. }
  53. func (s *bmUpdateRepoStub) Set(ctx context.Context, key, value string) error {
  54. panic("unexpected Set call")
  55. }
  56. func (s *bmUpdateRepoStub) GetMultiple(ctx context.Context, keys []string) (map[string]string, error) {
  57. panic("unexpected GetMultiple call")
  58. }
  59. func (s *bmUpdateRepoStub) SetMultiple(ctx context.Context, settings map[string]string) error {
  60. s.updates = make(map[string]string, len(settings))
  61. for k, v := range settings {
  62. s.updates[k] = v
  63. }
  64. return nil
  65. }
  66. func (s *bmUpdateRepoStub) GetAll(ctx context.Context) (map[string]string, error) {
  67. panic("unexpected GetAll call")
  68. }
  69. func (s *bmUpdateRepoStub) Delete(ctx context.Context, key string) error {
  70. panic("unexpected Delete call")
  71. }
  72. func resetBackendModeTestCache(t *testing.T) {
  73. t.Helper()
  74. backendModeCache.Store((*cachedBackendMode)(nil))
  75. t.Cleanup(func() {
  76. backendModeCache.Store((*cachedBackendMode)(nil))
  77. })
  78. }
  79. func TestIsBackendModeEnabled_ReturnsTrue(t *testing.T) {
  80. resetBackendModeTestCache(t)
  81. repo := &bmRepoStub{
  82. getValueFn: func(ctx context.Context, key string) (string, error) {
  83. require.Equal(t, SettingKeyBackendModeEnabled, key)
  84. return "true", nil
  85. },
  86. }
  87. svc := NewSettingService(repo, &config.Config{})
  88. require.True(t, svc.IsBackendModeEnabled(context.Background()))
  89. require.Equal(t, 1, repo.calls)
  90. }
  91. func TestIsBackendModeEnabled_ReturnsFalse(t *testing.T) {
  92. resetBackendModeTestCache(t)
  93. repo := &bmRepoStub{
  94. getValueFn: func(ctx context.Context, key string) (string, error) {
  95. require.Equal(t, SettingKeyBackendModeEnabled, key)
  96. return "false", nil
  97. },
  98. }
  99. svc := NewSettingService(repo, &config.Config{})
  100. require.False(t, svc.IsBackendModeEnabled(context.Background()))
  101. require.Equal(t, 1, repo.calls)
  102. }
  103. func TestIsBackendModeEnabled_ReturnsFalseOnNotFound(t *testing.T) {
  104. resetBackendModeTestCache(t)
  105. repo := &bmRepoStub{
  106. getValueFn: func(ctx context.Context, key string) (string, error) {
  107. require.Equal(t, SettingKeyBackendModeEnabled, key)
  108. return "", ErrSettingNotFound
  109. },
  110. }
  111. svc := NewSettingService(repo, &config.Config{})
  112. require.False(t, svc.IsBackendModeEnabled(context.Background()))
  113. require.Equal(t, 1, repo.calls)
  114. }
  115. func TestIsBackendModeEnabled_ReturnsFalseOnDBError(t *testing.T) {
  116. resetBackendModeTestCache(t)
  117. repo := &bmRepoStub{
  118. getValueFn: func(ctx context.Context, key string) (string, error) {
  119. require.Equal(t, SettingKeyBackendModeEnabled, key)
  120. return "", errors.New("db down")
  121. },
  122. }
  123. svc := NewSettingService(repo, &config.Config{})
  124. require.False(t, svc.IsBackendModeEnabled(context.Background()))
  125. require.Equal(t, 1, repo.calls)
  126. }
  127. func TestIsBackendModeEnabled_CachesResult(t *testing.T) {
  128. resetBackendModeTestCache(t)
  129. repo := &bmRepoStub{
  130. getValueFn: func(ctx context.Context, key string) (string, error) {
  131. require.Equal(t, SettingKeyBackendModeEnabled, key)
  132. return "true", nil
  133. },
  134. }
  135. svc := NewSettingService(repo, &config.Config{})
  136. require.True(t, svc.IsBackendModeEnabled(context.Background()))
  137. require.True(t, svc.IsBackendModeEnabled(context.Background()))
  138. require.Equal(t, 1, repo.calls)
  139. }
  140. func TestUpdateSettings_InvalidatesBackendModeCache(t *testing.T) {
  141. resetBackendModeTestCache(t)
  142. backendModeCache.Store(&cachedBackendMode{
  143. value: true,
  144. expiresAt: time.Now().Add(backendModeCacheTTL).UnixNano(),
  145. })
  146. repo := &bmUpdateRepoStub{
  147. getValueFn: func(ctx context.Context, key string) (string, error) {
  148. require.Equal(t, SettingKeyBackendModeEnabled, key)
  149. return "true", nil
  150. },
  151. }
  152. svc := NewSettingService(repo, &config.Config{})
  153. err := svc.UpdateSettings(context.Background(), &SystemSettings{
  154. BackendModeEnabled: false,
  155. })
  156. require.NoError(t, err)
  157. require.Equal(t, "false", repo.updates[SettingKeyBackendModeEnabled])
  158. require.False(t, svc.IsBackendModeEnabled(context.Background()))
  159. }