Skip to content

Commit f316c89

Browse files
func: update integration handler tests
1 parent c0ebf91 commit f316c89

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

policy/handler.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,6 @@ func NewPolicyHandler(config HandlerConfig) (*Handler, error) {
172172
return h, nil
173173
}
174174

175-
func (h *Handler) configureHorizontalPolicy() error {
176-
177-
err := h.loadCheckRunners()
178-
if err != nil {
179-
return fmt.Errorf("failed to load check handlers: %w", err)
180-
}
181-
182-
h.minCount = h.policy.Min
183-
h.maxCount = h.policy.Max
184-
185-
h.calculateNewCount = h.calculateHorizontalNewCount
186-
return nil
187-
}
188-
189175
func (h *Handler) loadCheckRunners() error {
190176
runners := []*checkRunner{}
191177

@@ -211,6 +197,7 @@ func (h *Handler) loadCheckRunners() error {
211197
runners = append(runners, runner)
212198
}
213199

200+
// Do the update as a single operation to avoid partial updates.
214201
h.checkRunners = runners
215202
return nil
216203
}
@@ -397,24 +384,22 @@ func (h *Handler) updateHandler(updatedPolicy *sdk.ScalingPolicy) {
397384

398385
switch updatedPolicy.Type {
399386
case sdk.ScalingPolicyTypeCluster, sdk.ScalingPolicyTypeHorizontal:
400-
h.log.Debug("updating check handlers", "old_checks", len(h.policy.Checks),
401-
"new_checks", len(updatedPolicy.Checks))
402-
err := h.configureHorizontalPolicy()
387+
err := h.updateHorizontalPolicy(updatedPolicy)
403388
if err != nil {
404-
h.errChn <- fmt.Errorf("unable to update horizontal policy: %w", err)
389+
h.errChn <- fmt.Errorf("unable to update horizontal policy %w", err)
405390
return
406391
}
407392

408393
default:
409-
err := h.configureVerticalPolicy()
394+
err := h.updateVerticalPolicy(updatedPolicy)
410395
if err != nil {
411-
h.errChn <- fmt.Errorf("unable to update vertical policy: %w", err)
396+
h.errChn <- fmt.Errorf("unable to update vertical policy %w", err)
397+
return
412398
}
413399
}
414400

415-
h.log.Debug("policy successfully updated")
416401
h.policy = updatedPolicy
417-
402+
h.log.Debug("policy successfully updated")
418403
}
419404

420405
// applyMutators applies the mutators registered with the handler in order and

policy/handler_oss.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,28 @@ package policy
88

99
import (
1010
"context"
11+
"fmt"
1112

1213
"github.com/hashicorp/nomad-autoscaler/sdk"
1314
)
1415

16+
func (h *Handler) configureHorizontalPolicy() error {
17+
return h.updateHorizontalPolicy(h.policy)
18+
}
19+
20+
func (h *Handler) updateHorizontalPolicy(up *sdk.ScalingPolicy) error {
21+
err := h.loadCheckRunners()
22+
if err != nil {
23+
return fmt.Errorf("failed to load check handlers: %w", err)
24+
}
25+
26+
h.minCount = up.Min
27+
h.maxCount = up.Max
28+
29+
h.calculateNewCount = h.calculateHorizontalNewCount
30+
return nil
31+
}
32+
1533
func (h *Handler) configureVerticalPolicy() error {
1634

1735
h.calculateNewCount = func(ctx context.Context, currentCount int64) (sdk.ScalingAction, error) {
@@ -22,3 +40,7 @@ func (h *Handler) configureVerticalPolicy() error {
2240
}
2341
return nil
2442
}
43+
44+
func (h *Handler) updateVerticalPolicy(up *sdk.ScalingPolicy) error {
45+
return h.configureVerticalPolicy()
46+
}

policy/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ func TestHandler_Run_ScalingNotNeeded_Integration(t *testing.T) {
467467
pm: mdg,
468468
}
469469

470-
must.NoError(t, handler.loadCheckRunners())
470+
must.NoError(t, handler.configureHorizontalPolicy())
471471

472472
go handler.Run(ctx)
473473
time.Sleep(30 * time.Millisecond)

0 commit comments

Comments
 (0)