Skip to content

Commit

Permalink
fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
et-nik committed Jan 16, 2024
1 parent 2f70d40 commit 5d28a98
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
34 changes: 23 additions & 11 deletions internal/app/domain/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func (s *Server) Setting(key string) string {
s.mu.RLock()
defer s.mu.RUnlock()

return s.setting(key)
}

func (s *Server) setting(key string) string {
if val, ok := s.settings[key]; ok {
return val
}
Expand All @@ -365,6 +369,10 @@ func (s *Server) SetSetting(key string, value string) {
s.mu.Lock()
defer s.mu.Unlock()

s.setSetting(key, value)
}

func (s *Server) setSetting(key string, value string) {
s.settings[key] = value
s.setValueIsChanged("settings")

Expand All @@ -386,10 +394,14 @@ func (s *Server) AutoStart() bool {
s.mu.RLock()
defer s.mu.RUnlock()

autostart := s.Setting(autostartCurrentSettingKey)
return s.autoStart()
}

func (s *Server) autoStart() bool {
autostart := s.setting(autostartCurrentSettingKey)

if autostart == "" {
autostart = s.Setting(autostartSettingKey)
autostart = s.setting(autostartSettingKey)
}

if autostart == "" {
Expand All @@ -404,22 +416,22 @@ func (s *Server) AffectInstall() {
}

func (s *Server) AffectStart() {
s.mu.RLock()
defer s.mu.RUnlock()
s.mu.Lock()
defer s.mu.Unlock()

autostart := s.readBoolSetting(s.Setting(autostartSettingKey))
autostart := s.readBoolSetting(s.setting(autostartSettingKey))
if autostart {
s.SetSetting(autostartCurrentSettingKey, "1")
s.setSetting(autostartCurrentSettingKey, "1")
s.updatedAt = time.Now()
}
}

func (s *Server) AffectStop() {
s.mu.RLock()
defer s.mu.RUnlock()
s.mu.Lock()
defer s.mu.Unlock()

if s.AutoStart() {
s.SetSetting(autostartCurrentSettingKey, "0")
if s.autoStart() {
s.setSetting(autostartCurrentSettingKey, "0")
s.updatedAt = time.Now()
}
}
Expand All @@ -428,7 +440,7 @@ func (s *Server) UpdateBeforeStart() bool {
s.mu.RLock()
defer s.mu.RUnlock()

return s.readBoolSetting(s.Setting(updateBeforeStartSettingKey))
return s.readBoolSetting(s.setting(updateBeforeStartSettingKey))
}

func (s *Server) InstallationStatus() InstallationStatus {
Expand Down
6 changes: 5 additions & 1 deletion pkg/limiter/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import (
func Test_Limiter(t *testing.T) {
calledSingle := 0
calledBulk := 0
count := 0

s := NewAPICallScheduler(
10*time.Millisecond,
5,
func(ctx context.Context, q *Queue) error {
q.Get()
calledSingle++
count++
return nil
},
func(ctx context.Context, q *Queue) error {
q.GetN(10)
n := q.GetN(10)
calledBulk++
count += len(n)
return nil
},
logger.NewLogger(config.Config{}),
Expand Down Expand Up @@ -55,4 +58,5 @@ func Test_Limiter(t *testing.T) {

assert.Equal(t, 3, calledSingle)
assert.Equal(t, 5, calledBulk)
assert.Equal(t, 53, count)
}

0 comments on commit 5d28a98

Please sign in to comment.