account_health_service_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. )
  8. type stubHealthAccountStore struct {
  9. isolated map[int64]time.Time
  10. cleared []int64
  11. }
  12. func (s *stubHealthAccountStore) SetTempUnschedulable(_ context.Context, id int64, until time.Time, _ string) error {
  13. if s.isolated == nil {
  14. s.isolated = make(map[int64]time.Time)
  15. }
  16. s.isolated[id] = until
  17. return nil
  18. }
  19. func (s *stubHealthAccountStore) ClearTempUnschedulable(_ context.Context, id int64) error {
  20. s.cleared = append(s.cleared, id)
  21. return nil
  22. }
  23. type stubHealthSettingRepo struct {
  24. values map[string]string
  25. }
  26. func (s *stubHealthSettingRepo) Get(ctx context.Context, key string) (*Setting, error) {
  27. return &Setting{Key: key, Value: s.values[key]}, nil
  28. }
  29. func (s *stubHealthSettingRepo) GetValue(_ context.Context, key string) (string, error) {
  30. return s.values[key], nil
  31. }
  32. func (s *stubHealthSettingRepo) Set(_ context.Context, key, value string) error {
  33. if s.values == nil {
  34. s.values = make(map[string]string)
  35. }
  36. s.values[key] = value
  37. return nil
  38. }
  39. func (s *stubHealthSettingRepo) GetMultiple(_ context.Context, keys []string) (map[string]string, error) {
  40. return nil, nil
  41. }
  42. func (s *stubHealthSettingRepo) SetMultiple(_ context.Context, settings map[string]string) error {
  43. return nil
  44. }
  45. func (s *stubHealthSettingRepo) GetAll(context.Context) (map[string]string, error) {
  46. return nil, nil
  47. }
  48. func (s *stubHealthSettingRepo) Delete(_ context.Context, _ string) error { return nil }
  49. func TestAccountHealthIsolateResume(t *testing.T) {
  50. store := &stubHealthAccountStore{}
  51. svc := NewAccountHealthService(nil, store, &stubHealthSettingRepo{})
  52. require.NoError(t, svc.Isolate(context.Background(), 7))
  53. require.Contains(t, store.isolated, int64(7))
  54. require.NoError(t, svc.Resume(context.Background(), 7))
  55. require.Contains(t, store.cleared, int64(7))
  56. require.Empty(t, svc.Snapshot())
  57. }
  58. func TestAccountHealthSettingsRoundtrip(t *testing.T) {
  59. repo := &stubHealthSettingRepo{}
  60. svc := NewAccountHealthService(nil, &stubHealthAccountStore{}, repo)
  61. got := svc.GetSettings(context.Background())
  62. require.True(t, got.WindowMinutes > 0)
  63. updated, err := svc.UpdateSettings(context.Background(), AccountHealthSettings{Enabled: false})
  64. require.NoError(t, err)
  65. require.False(t, updated.Enabled)
  66. require.Contains(t, repo.values[SettingKeyAccountHealthSettings], `"enabled":false`)
  67. }