Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #19864 to 7.x: libbeat/publisher/pipeline: fix Client.Close #20124

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libbeat/publisher/pipeline/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (w *clientCloseWaiter) signalClose() {
return
}

w.closing.Store(false)
w.closing.Store(true)
if w.events.Load() == 0 {
w.finishClose()
return
Expand Down
92 changes: 92 additions & 0 deletions libbeat/publisher/pipeline/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import (
"context"
"sync"
"testing"
"time"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/outputs"
"github.com/elastic/beats/v7/libbeat/publisher"
"github.com/elastic/beats/v7/libbeat/publisher/queue"
"github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue"
"github.com/elastic/beats/v7/libbeat/tests/resources"
)

Expand Down Expand Up @@ -113,3 +116,92 @@ func TestClient(t *testing.T) {
}
})
}

func TestClientWaitClose(t *testing.T) {
routinesChecker := resources.NewGoroutinesChecker()
defer routinesChecker.Check(t)

makePipeline := func(settings Settings, qu queue.Queue) *Pipeline {
p, err := New(beat.Info{},
Monitors{},
func(queue.ACKListener) (queue.Queue, error) { return qu, nil },
outputs.Group{},
settings,
)
if err != nil {
panic(err)
}

return p
}
if testing.Verbose() {
logp.TestingSetup()
}

q := memqueue.NewQueue(logp.L(), memqueue.Settings{Events: 1})
pipeline := makePipeline(Settings{}, q)
defer pipeline.Close()

t.Run("WaitClose blocks", func(t *testing.T) {
client, err := pipeline.ConnectWith(beat.ClientConfig{
WaitClose: 500 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
defer client.Close()

// Send an event which never gets acknowledged.
client.Publish(beat.Event{})

closed := make(chan struct{})
go func() {
defer close(closed)
client.Close()
}()

select {
case <-closed:
t.Fatal("expected Close to wait for event acknowledgement")
case <-time.After(100 * time.Millisecond):
}

select {
case <-closed:
case <-time.After(10 * time.Second):
t.Fatal("expected Close to stop waiting after WaitClose elapses")
}
})

t.Run("ACKing events unblocks WaitClose", func(t *testing.T) {
client, err := pipeline.ConnectWith(beat.ClientConfig{
WaitClose: time.Minute,
})
if err != nil {
t.Fatal(err)
}
defer client.Close()

// Send an event which gets acknowledged immediately.
client.Publish(beat.Event{})
output := newMockClient(func(batch publisher.Batch) error {
batch.ACK()
return nil
})
defer output.Close()
pipeline.output.Set(outputs.Group{Clients: []outputs.Client{output}})
defer pipeline.output.Set(outputs.Group{})

closed := make(chan struct{})
go func() {
defer close(closed)
client.Close()
}()

select {
case <-closed:
case <-time.After(10 * time.Second):
t.Fatal("expected Close to stop waiting after event acknowledgement")
}
})
}