-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Verify extension lifecycle #2679
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
bogdandrutu
merged 14 commits into
open-telemetry:main
from
pjanotti:verify-lifecycle-for-extensions
Mar 17, 2021
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16e9dce
Verify extension lifecycle
pjanotti 20812d5
PR feedback
pjanotti bde4225
Use available port for healthcheck in TestApplication_Start
pjanotti 0c20030
Embed nopHost into assertNoErrorHost
pjanotti 0ce0e77
Fix accidentally lost shutdown for activeExt in VerityExtensionLifecycle
pjanotti dd1c855
to be reverted, testing CI
pjanotti 0a2b67a
Coverage tax
pjanotti 8098c4d
Use runtime.Gosched instead of long sleep
pjanotti 0c8d0b2
Add assert info and delay to release TCP socket
pjanotti 8bd195c
Fix healchcheck shutdown to only exit after the Serve goroutine is done
pjanotti 38cc51a
Ensure shutdown of extensions wait for server stop
pjanotti cface3b
Remove loop from VerifyExtensionLifecycle
pjanotti b9a068c
Make verify lifecycle private to defaults_test.go
pjanotti c41e585
Remove public NewAssertNoErrorHost
pjanotti 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed 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 componenttest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/config/configmodels" | ||
| ) | ||
|
|
||
| // assertNoErrorHost implements a component.Host that asserts that | ||
| // there were no errors. | ||
| type assertNoErrorHost struct { | ||
| t *testing.T | ||
pjanotti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| var _ component.Host = (*assertNoErrorHost)(nil) | ||
|
|
||
| // NewAssertNoError returns a new instance of assertNoErrorHost. | ||
| func NewAssertNoError(t *testing.T) component.Host { | ||
| return &assertNoErrorHost{ | ||
| t: t, | ||
| } | ||
| } | ||
|
|
||
| func (aneh *assertNoErrorHost) ReportFatalError(err error) { | ||
| assert.NoError(aneh.t, err) | ||
| } | ||
|
|
||
| func (aneh *assertNoErrorHost) GetFactory(_ component.Kind, _ configmodels.Type) component.Factory { | ||
| return nil | ||
| } | ||
|
|
||
| func (aneh *assertNoErrorHost) GetExtensions() map[configmodels.NamedEntity]component.Extension { | ||
| return nil | ||
| } | ||
|
|
||
| func (aneh *assertNoErrorHost) GetExporters() map[configmodels.DataType]map[configmodels.NamedEntity]component.Exporter { | ||
| return nil | ||
| } | ||
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,79 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed 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 componenttest | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
|
|
||
| "go.opentelemetry.io/collector/component" | ||
| "go.opentelemetry.io/collector/config/configmodels" | ||
| ) | ||
|
|
||
| // GetExtensionConfigFn is used customize the configuration passed to the verification. | ||
| // This is used to change ports or provide values required but not provided by the | ||
| // default configuration. | ||
| type GetExtensionConfigFn func() configmodels.Extension | ||
|
|
||
| // VerityExtensionLifecycle is used to test if an extension type can handle the typical | ||
| // lifecycle of a component. The getConfigFn parameter only need to be specified if | ||
| // the test can't be done with the default configuration for the component. | ||
| func VerityExtensionLifecycle(t *testing.T, factory component.ExtensionFactory, getConfigFn GetExtensionConfigFn) { | ||
| ctx := context.Background() | ||
| host := NewAssertNoError(t) | ||
| extCreateParams := component.ExtensionCreateParams{ | ||
| Logger: zap.NewNop(), | ||
| ApplicationStartInfo: component.DefaultApplicationStartInfo(), | ||
| } | ||
|
|
||
| var activeExt, builtExt component.Extension | ||
| defer func() { | ||
| // If the shutdown happens here there were already some errors on the test. | ||
| // Ignore errors on this attempt to clean-up. | ||
pjanotti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if activeExt != nil { | ||
| _ = activeExt.Shutdown(ctx) | ||
| } | ||
| if builtExt != nil { | ||
| _ = builtExt.Shutdown(ctx) | ||
| } | ||
| }() | ||
|
|
||
| if getConfigFn == nil { | ||
| getConfigFn = factory.CreateDefaultConfig | ||
| } | ||
|
|
||
| for i := 0; i < 3; i++ { | ||
pjanotti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var err error | ||
| builtExt, err = factory.CreateExtension(ctx, extCreateParams, getConfigFn()) | ||
| require.NoError(t, err, "Extension type: %s", factory.Type()) | ||
|
|
||
| if activeExt != nil { | ||
| assert.NoError(t, activeExt.Shutdown(ctx), "Extension type: %s", factory.Type()) | ||
| activeExt = nil | ||
| } | ||
|
|
||
| require.NoError(t, builtExt.Start(ctx, host), "Extension type: %s", factory.Type()) | ||
| activeExt = builtExt | ||
| builtExt = nil | ||
|
|
||
| // The component may start go routines, give them a chance to run. | ||
| time.Sleep(100 * time.Millisecond) | ||
pjanotti marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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
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
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
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
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
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.