-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce duplicate code in components helper
Signed-off-by: Bogdan Drutu <[email protected]>
- Loading branch information
1 parent
29859f2
commit 8e20e5d
Showing
9 changed files
with
220 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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,62 @@ | ||
// 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 componenthelper | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
// Start specifies the function invoked when the exporter is being started. | ||
type Start func(context.Context, component.Host) error | ||
|
||
// Shutdown specifies the function invoked when the exporter is being shutdown. | ||
type Shutdown func(context.Context) error | ||
|
||
type Settings struct { | ||
Start | ||
Shutdown | ||
} | ||
|
||
// DefaultSettings returns the default settings for a component. The Start and Shutdown are no-op. | ||
func DefaultSettings() *Settings { | ||
return &Settings{ | ||
Start: func(ctx context.Context, host component.Host) error { return nil }, | ||
Shutdown: func(ctx context.Context) error { return nil }, | ||
} | ||
} | ||
|
||
type baseComponent struct { | ||
start Start | ||
shutdown Shutdown | ||
} | ||
|
||
// Start all senders and exporter and is invoked during service start. | ||
func (be *baseComponent) Start(ctx context.Context, host component.Host) error { | ||
return be.start(ctx, host) | ||
} | ||
|
||
// Shutdown all senders and exporter and is invoked during service shutdown. | ||
func (be *baseComponent) Shutdown(ctx context.Context) error { | ||
return be.shutdown(ctx) | ||
} | ||
|
||
func NewComponent(s *Settings) component.Component { | ||
return &baseComponent{ | ||
start: s.Start, | ||
shutdown: s.Shutdown, | ||
} | ||
} |
This file contains 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,69 @@ | ||
// 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 componenthelper | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
) | ||
|
||
func TestDefaultSettings(t *testing.T) { | ||
st := DefaultSettings() | ||
require.NotNil(t, st) | ||
cp := NewComponent(st) | ||
require.NoError(t, cp.Start(context.Background(), componenttest.NewNopHost())) | ||
require.NoError(t, cp.Shutdown(context.Background())) | ||
} | ||
|
||
func TestWithStart(t *testing.T) { | ||
startCalled := false | ||
st := DefaultSettings() | ||
st.Start = func(context.Context, component.Host) error { startCalled = true; return nil } | ||
cp := NewComponent(st) | ||
assert.NoError(t, cp.Start(context.Background(), componenttest.NewNopHost())) | ||
assert.True(t, startCalled) | ||
} | ||
|
||
func TestWithStart_ReturnError(t *testing.T) { | ||
want := errors.New("my_error") | ||
st := DefaultSettings() | ||
st.Start = func(context.Context, component.Host) error { return want } | ||
cp := NewComponent(st) | ||
assert.Equal(t, want, cp.Start(context.Background(), componenttest.NewNopHost())) | ||
} | ||
|
||
func TestWithShutdown(t *testing.T) { | ||
shutdownCalled := false | ||
st := DefaultSettings() | ||
st.Shutdown = func(context.Context) error { shutdownCalled = true; return nil } | ||
cp := NewComponent(st) | ||
assert.NoError(t, cp.Shutdown(context.Background())) | ||
assert.True(t, shutdownCalled) | ||
} | ||
|
||
func TestWithShutdown_ReturnError(t *testing.T) { | ||
want := errors.New("my_error") | ||
st := DefaultSettings() | ||
st.Shutdown = func(context.Context) error { return want } | ||
cp := NewComponent(st) | ||
assert.Equal(t, want, cp.Shutdown(context.Background())) | ||
} |
This file contains 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 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 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 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 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.