Skip to content

Commit

Permalink
Merge pull request #38 from hovsep/v.0.1.0
Browse files Browse the repository at this point in the history
V.0.1.0
  • Loading branch information
hovsep authored Sep 16, 2024
2 parents 0dcb84f + b3e7371 commit 5e514e9
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 275 deletions.
17 changes: 17 additions & 0 deletions cycle/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cycle

// Collection contains the results of several activation cycles
type Collection []*Cycle

// NewCollection creates a collection
func NewCollection() Collection {
return make(Collection, 0)
}

// Add adds cycle results to existing collection
func (collection Collection) Add(newCycleResults ...*Cycle) Collection {
for _, cycleResult := range newCycleResults {
collection = append(collection, cycleResult)
}
return collection
}
68 changes: 68 additions & 0 deletions cycle/collection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cycle

import (
"github.com/hovsep/fmesh/component"
"reflect"
"testing"
)

func TestNewCollection(t *testing.T) {
tests := []struct {
name string
want Collection
}{
{
name: "happy path",
want: Collection{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewCollection(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCollection() = %v, want %v", got, tt.want)
}
})
}
}

func TestCollection_Add(t *testing.T) {
type args struct {
cycleResults []*Cycle
}
tests := []struct {
name string
cycleResults Collection
args args
want Collection
}{
{
name: "happy path",
cycleResults: NewCollection(),
args: args{
cycleResults: []*Cycle{
New().WithActivationResults(component.NewActivationResult("c1").SetActivated(false)),
New().WithActivationResults(component.NewActivationResult("c1").SetActivated(true)),
},
},
want: Collection{
{
activationResults: component.ActivationResultCollection{
"c1": component.NewActivationResult("c1").SetActivated(false),
},
},
{
activationResults: component.ActivationResultCollection{
"c1": component.NewActivationResult("c1").SetActivated(true),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cycleResults.Add(tt.args.cycleResults...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Add() = %v, want %v", got, tt.want)
}
})
}
}
45 changes: 45 additions & 0 deletions cycle/cycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cycle

import (
"github.com/hovsep/fmesh/component"
"sync"
)

// Cycle contains the info about given activation cycle
type Cycle struct {
sync.Mutex
activationResults component.ActivationResultCollection
}

// New creates a new cycle
func New() *Cycle {
return &Cycle{
activationResults: make(component.ActivationResultCollection),
}
}

// ActivationResults getter
func (cycle *Cycle) ActivationResults() component.ActivationResultCollection {
return cycle.activationResults
}

// HasErrors tells whether the cycle is ended wih activation errors (at lease one component returned an error)
func (cycle *Cycle) HasErrors() bool {
return cycle.ActivationResults().HasErrors()
}

// HasPanics tells whether the cycle is ended wih panic(at lease one component panicked)
func (cycle *Cycle) HasPanics() bool {
return cycle.ActivationResults().HasPanics()
}

// HasActivatedComponents tells when at least one component in the cycle has activated
func (cycle *Cycle) HasActivatedComponents() bool {
return cycle.ActivationResults().HasActivatedComponents()
}

// WithActivationResults adds multiple activation results
func (cycle *Cycle) WithActivationResults(activationResults ...*component.ActivationResult) *Cycle {
cycle.activationResults = cycle.ActivationResults().Add(activationResults...)
return cycle
}
Loading

0 comments on commit 5e514e9

Please sign in to comment.