Skip to content

Commit

Permalink
Component: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Sep 12, 2024
1 parent be10d11 commit ed13e89
Show file tree
Hide file tree
Showing 6 changed files with 650 additions and 7 deletions.
32 changes: 28 additions & 4 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ type Component struct {
// Components is a useful collection type
type Components map[string]*Component

// New creates a new empty component
func New(name string) *Component {
// NewComponent creates a new empty component
func NewComponent(name string) *Component {
return &Component{name: name}
}

func NewComponents(names ...string) Components {
components := make(Components, len(names))
for _, name := range names {
components[name] = NewComponent(name)
}
return components
}

// WithDescription sets description
func (c *Component) WithDescription(description string) *Component {
c.description = description
Expand Down Expand Up @@ -75,10 +83,14 @@ func (c *Component) Outputs() port.Ports {
func (c *Component) Activate() (aRes hop.ActivationResult) {
defer func() {
if r := recover(); r != nil {
errorFormat := "panicked with: %v, stacktrace: %s"
if _, ok := r.(error); ok {
errorFormat = "panicked with: %w, stacktrace: %s"
}
aRes = hop.ActivationResult{
Activated: true,
ComponentName: c.name,
Err: fmt.Errorf("panicked with %w, stacktrace: %s", r, debug.Stack()),
Err: fmt.Errorf(errorFormat, r, debug.Stack()),
}
}
}()
Expand All @@ -96,6 +108,18 @@ func (c *Component) Activate() (aRes hop.ActivationResult) {
return
}

if c.f == nil {
//Activation function is not set

aRes = hop.ActivationResult{
Activated: false,
ComponentName: c.name,
Err: nil,
}

return
}

//Run the computation
err := c.f(c.inputs, c.outputs)

Expand Down Expand Up @@ -135,7 +159,7 @@ func (c *Component) Activate() (aRes hop.ActivationResult) {
return
}

// FlushOutputs pushed signals out of the component
// FlushOutputs pushed signals out of the component outputs to pipes and clears outputs
func (c *Component) FlushOutputs() {
for _, out := range c.outputs {
if !out.HasSignal() || len(out.Pipes()) == 0 {
Expand Down
Loading

0 comments on commit ed13e89

Please sign in to comment.