Skip to content

Commit

Permalink
test: use a lower unenroll timeout in TestActionUnenrollHandler (#5465)
Browse files Browse the repository at this point in the history
* test: use a lower unenroll timeout in TestActionUnenrollHandler

auto unenroll with no running components is timing out after 15s due to
hardcoded unenrollTimeout.
There is no need to use a huge timeout in test.
Pass the timeout as a method arg and use a lower timeout.

Test time went from 15s to 0.1s

* lint: fix linter issues
  • Loading branch information
kruskall authored Sep 9, 2024
1 parent 2305346 commit bc852c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func NewUnenroll(

// Handle handles UNENROLL action.
func (h *Unenroll) Handle(ctx context.Context, a fleetapi.Action, acker acker.Acker) error {
return h.handle(ctx, a, acker, unenrollTimeout)
}

func (h *Unenroll) handle(ctx context.Context, a fleetapi.Action, acker acker.Acker, timeout time.Duration) error {
h.log.Debugf("handlerUnenroll: action '%+v' received", a)
action, ok := a.(*fleetapi.ActionUnenroll)
if !ok {
Expand Down Expand Up @@ -98,7 +102,7 @@ func (h *Unenroll) Handle(ctx context.Context, a fleetapi.Action, acker acker.Ac
h.log.Warnf("Failed to update state store: %v", err)
}

unenrollCtx, cancel := context.WithTimeout(ctx, unenrollTimeout)
unenrollCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

unenrollPolicy.WaitAck(unenrollCtx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sync/atomic"
"testing"
"time"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
Expand Down Expand Up @@ -219,7 +220,7 @@ func TestActionUnenrollHandler(t *testing.T) {
if tc.autoUnenroll {
a.IsDetected = true
}
err := handler.Handle(ctx, a, acker)
err := handler.handle(ctx, a, acker, 100*time.Millisecond)

require.ErrorIs(t, err, tc.wantErr)

Expand Down

0 comments on commit bc852c0

Please sign in to comment.