-
Notifications
You must be signed in to change notification settings - Fork 5k
Add unit tests for harvester.go of input-logfile package #24107
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
Merged
kvch
merged 9 commits into
elastic:master
from
kvch:test-filebeat-add-filestream-harvestergroup
Mar 10, 2021
+364
−9
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d810c1c
Add unit tests for HarvesterGroup of filestream input
kvch 7d89a39
address review notes
kvch 013ce48
fix tests
kvch 8dbd65d
use waitgroup for timing
kvch 5f912fa
address more review notes
kvch 53c83b0
address even more review notes
kvch 31d6a6a
let's see how this goes
kvch ecf73e8
more renaming
kvch 16db6e9
more addressing
kvch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
325 changes: 325 additions & 0 deletions
325
filebeat/input/filestream/internal/input-logfile/harvester_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,325 @@ | ||
| // Licensed to Elasticsearch B.V. under one or more contributor | ||
| // license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright | ||
| // ownership. Elasticsearch B.V. licenses this file to you under | ||
| // the Apache License, Version 2.0 (the "License"); you may | ||
| // not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package input_logfile | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| input "github.com/elastic/beats/v7/filebeat/input/v2" | ||
| "github.com/elastic/beats/v7/libbeat/logp" | ||
| "github.com/elastic/beats/v7/libbeat/tests/resources" | ||
| "github.com/elastic/beats/v7/x-pack/dockerlogbeat/pipelinemock" | ||
| "github.com/elastic/go-concert/unison" | ||
| ) | ||
|
|
||
| func TestReaderGroup(t *testing.T) { | ||
| requireGroupSuccess := func(t *testing.T, ctx context.Context, cf context.CancelFunc, err error) { | ||
| require.NotNil(t, ctx) | ||
| require.NotNil(t, cf) | ||
| require.Nil(t, err) | ||
| } | ||
|
|
||
| requireGroupError := func(t *testing.T, ctx context.Context, cf context.CancelFunc, err error) { | ||
| require.Nil(t, ctx) | ||
| require.Nil(t, cf) | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| t.Run("assert new group is empty", func(t *testing.T) { | ||
| rg := newReaderGroup() | ||
| require.Equal(t, 0, len(rg.table)) | ||
| }) | ||
|
|
||
| t.Run("assert non existent key can be removed", func(t *testing.T) { | ||
| rg := newReaderGroup() | ||
| require.Equal(t, 0, len(rg.table)) | ||
| rg.remove("no such id") | ||
| require.Equal(t, 0, len(rg.table)) | ||
| }) | ||
|
|
||
| t.Run("assert inserting existing key returns error", func(t *testing.T) { | ||
| rg := newReaderGroup() | ||
| ctx, cf, err := rg.newContext("test-id", context.Background()) | ||
| requireGroupSuccess(t, ctx, cf, err) | ||
| require.Equal(t, 1, len(rg.table)) | ||
|
|
||
| newCtx, newCf, err := rg.newContext("test-id", context.Background()) | ||
| requireGroupError(t, newCtx, newCf, err) | ||
| }) | ||
|
|
||
| t.Run("assert new key is added, can be removed and its context is cancelled", func(t *testing.T) { | ||
| rg := newReaderGroup() | ||
| ctx, cf, err := rg.newContext("test-id", context.Background()) | ||
| requireGroupSuccess(t, ctx, cf, err) | ||
| require.Equal(t, 1, len(rg.table)) | ||
|
|
||
| require.Nil(t, ctx.Err()) | ||
| rg.remove("test-id") | ||
|
|
||
| require.Equal(t, 0, len(rg.table)) | ||
| require.Error(t, ctx.Err(), context.Canceled) | ||
|
|
||
| newCtx, newCf, err := rg.newContext("test-id", context.Background()) | ||
| requireGroupSuccess(t, newCtx, newCf, err) | ||
| require.Equal(t, 1, len(rg.table)) | ||
| require.Nil(t, newCtx.Err()) | ||
| }) | ||
| } | ||
|
|
||
| func TestDefaultHarvesterGroup(t *testing.T) { | ||
| source := &testSource{"/path/to/test"} | ||
|
|
||
| requireSourceAddedToBookkeeper := func(t *testing.T, hg *defaultHarvesterGroup, s Source) { | ||
| require.True(t, hg.readers.hasID(s.Name())) | ||
| } | ||
|
|
||
| requireSourceRemovedFromBookkeeper := func(t *testing.T, hg *defaultHarvesterGroup, s Source) { | ||
| require.False(t, hg.readers.hasID(s.Name())) | ||
| } | ||
|
|
||
| t.Run("assert a harvester is started in a goroutine", func(t *testing.T) { | ||
| var wg sync.WaitGroup | ||
| mockHarvester := &mockHarvester{onRun: correctOnRun, wg: &wg} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
| defer gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| wg.Add(1) | ||
| hg.Start(input.Context{Logger: logp.L(), Cancelation: context.Background()}, source) | ||
|
|
||
| // wait until harvester.Run is done | ||
| wg.Wait() | ||
| // wait until goroutine that started `harvester.Run` is finished | ||
| gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| require.Equal(t, 1, mockHarvester.getRunCount()) | ||
|
|
||
| requireSourceRemovedFromBookkeeper(t, hg, source) | ||
| // stopped source can be stopped | ||
| require.Nil(t, hg.StopGroup()) | ||
| }) | ||
|
|
||
| t.Run("assert a harvester can be stopped and removed from bookkeeper", func(t *testing.T) { | ||
| mockHarvester := &mockHarvester{onRun: blockUntilCancelOnRun} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
|
|
||
| hg.Start(input.Context{Logger: logp.L(), Cancelation: context.Background()}, source) | ||
|
|
||
| gorountineChecker.WaitUntilIncreased(1) | ||
| // wait until harvester is started | ||
| if mockHarvester.getRunCount() == 1 { | ||
| requireSourceAddedToBookkeeper(t, hg, source) | ||
| // after started, stop it | ||
| hg.Stop(source) | ||
| gorountineChecker.WaitUntilOriginalCount() | ||
| } | ||
|
|
||
| requireSourceRemovedFromBookkeeper(t, hg, source) | ||
| }) | ||
|
|
||
| t.Run("assert a harvester for same source cannot be started", func(t *testing.T) { | ||
| mockHarvester := &mockHarvester{onRun: blockUntilCancelOnRun} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
| inputCtx := input.Context{Logger: logp.L(), Cancelation: context.Background()} | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
| defer gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| hg.Start(inputCtx, source) | ||
| hg.Start(inputCtx, source) | ||
|
|
||
| gorountineChecker.WaitUntilIncreased(2) | ||
| // error is expected as a harvester group was expected to start twice for the same source | ||
| for !hg.readers.hasID(source.Name()) { | ||
| } | ||
| time.Sleep(3 * time.Millisecond) | ||
|
|
||
| hg.Stop(source) | ||
|
|
||
| err := hg.StopGroup() | ||
| require.Error(t, err) | ||
|
|
||
| require.Equal(t, 1, mockHarvester.getRunCount()) | ||
| }) | ||
|
|
||
| t.Run("assert a harvester panic is handled", func(t *testing.T) { | ||
| mockHarvester := &mockHarvester{onRun: panicOnRun} | ||
|
urso marked this conversation as resolved.
Outdated
|
||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
| defer func() { | ||
| if v := recover(); v != nil { | ||
| t.Errorf("did not recover from harvester panic in defaultHarvesterGroup") | ||
| } | ||
| }() | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
|
|
||
| hg.Start(input.Context{Logger: logp.L(), Cancelation: context.Background()}, source) | ||
|
urso marked this conversation as resolved.
Outdated
|
||
|
|
||
| // wait until harvester is stopped | ||
| gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| // make sure harvester had run once | ||
| require.Equal(t, 1, mockHarvester.getRunCount()) | ||
| requireSourceRemovedFromBookkeeper(t, hg, source) | ||
|
|
||
| require.Nil(t, hg.StopGroup()) | ||
| }) | ||
|
|
||
| t.Run("assert a harvester error is handled", func(t *testing.T) { | ||
| mockHarvester := &mockHarvester{onRun: errorOnRun} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
| defer gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| hg.Start(input.Context{Logger: logp.L(), Cancelation: context.Background()}, source) | ||
|
|
||
| gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| requireSourceRemovedFromBookkeeper(t, hg, source) | ||
|
|
||
| err := hg.StopGroup() | ||
| require.Error(t, err) | ||
| }) | ||
|
|
||
| t.Run("assert already locked resource has to wait", func(t *testing.T) { | ||
| var wg sync.WaitGroup | ||
| mockHarvester := &mockHarvester{onRun: correctOnRun, wg: &wg} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
| inputCtx := input.Context{Logger: logp.L(), Cancelation: context.Background()} | ||
|
|
||
| r, err := lock(inputCtx, hg.store, source.Name()) | ||
| if err != nil { | ||
| t.Fatalf("cannot lock source") | ||
| } | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
|
|
||
| wg.Add(1) | ||
| hg.Start(inputCtx, source) | ||
|
|
||
| gorountineChecker.WaitUntilIncreased(1) | ||
| ok := false | ||
| for !ok { | ||
| // wait until harvester is added to the bookeeper | ||
| ok = hg.readers.hasID(source.Name()) | ||
| if ok { | ||
| releaseResource(r) | ||
|
urso marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // wait until harvester.Run is done | ||
| wg.Wait() | ||
| // wait until goroutine that started `harvester.Run` is finished | ||
| gorountineChecker.WaitUntilOriginalCount() | ||
| require.Equal(t, 1, mockHarvester.getRunCount()) | ||
| require.Nil(t, hg.StopGroup()) | ||
| }) | ||
|
|
||
| t.Run("assert already locked resource has no problem when harvestergroup is cancelled", func(t *testing.T) { | ||
| mockHarvester := &mockHarvester{onRun: correctOnRun} | ||
| hg := testDefaultHarvesterGroup(t, mockHarvester) | ||
| inputCtx := input.Context{Logger: logp.L(), Cancelation: context.Background()} | ||
|
|
||
| gorountineChecker := resources.NewGoroutinesChecker() | ||
| defer gorountineChecker.WaitUntilOriginalCount() | ||
|
|
||
| r, err := lock(inputCtx, hg.store, source.Name()) | ||
| if err != nil { | ||
| t.Fatalf("cannot lock source") | ||
| } | ||
| defer releaseResource(r) | ||
|
|
||
| hg.Start(inputCtx, source) | ||
|
|
||
| gorountineChecker.WaitUntilIncreased(1) | ||
| require.Error(t, hg.StopGroup()) | ||
|
|
||
| require.Equal(t, 0, mockHarvester.getRunCount()) | ||
| }) | ||
| } | ||
|
|
||
| func testDefaultHarvesterGroup(t *testing.T, mockHarvester Harvester) *defaultHarvesterGroup { | ||
| return &defaultHarvesterGroup{ | ||
| readers: newReaderGroup(), | ||
| pipeline: &pipelinemock.MockPipelineConnector{}, | ||
| harvester: mockHarvester, | ||
| store: testOpenStore(t, "test", nil), | ||
| tg: unison.TaskGroup{}, | ||
| } | ||
| } | ||
|
|
||
| type mockHarvester struct { | ||
| mu sync.Mutex | ||
| runCount int | ||
|
|
||
| wg *sync.WaitGroup | ||
| onRun func(input.Context, Source, Cursor, Publisher) error | ||
| } | ||
|
|
||
| func (m *mockHarvester) Run(ctx input.Context, s Source, c Cursor, p Publisher) error { | ||
| if m.wg != nil { | ||
| defer m.wg.Done() | ||
| } | ||
|
|
||
| m.mu.Lock() | ||
| m.runCount += 1 | ||
|
kvch marked this conversation as resolved.
Outdated
|
||
| m.mu.Unlock() | ||
|
|
||
| if m.onRun != nil { | ||
| return m.onRun(ctx, s, c, p) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockHarvester) getRunCount() int { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| return m.runCount | ||
| } | ||
|
|
||
| func (m *mockHarvester) Test(_ Source, _ input.TestContext) error { return nil } | ||
|
|
||
| func (m *mockHarvester) Name() string { return "mock" } | ||
|
|
||
| func correctOnRun(_ input.Context, _ Source, _ Cursor, _ Publisher) error { | ||
| return nil | ||
| } | ||
|
|
||
| func blockUntilCancelOnRun(c input.Context, _ Source, _ Cursor, _ Publisher) error { | ||
| <-c.Cancelation.Done() | ||
| return nil | ||
| } | ||
|
|
||
| func errorOnRun(_ input.Context, _ Source, _ Cursor, _ Publisher) error { | ||
| return fmt.Errorf("harvester error") | ||
| } | ||
|
kvch marked this conversation as resolved.
Outdated
|
||
|
|
||
| func panicOnRun(_ input.Context, _ Source, _ Cursor, _ Publisher) error { | ||
| panic("don't panic") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.