Skip to content

Commit

Permalink
Merge pull request #87 from spiral/bug/collects_loop_issue
Browse files Browse the repository at this point in the history
fix(collects): Bug in the Collects loop with early exit
  • Loading branch information
rustatian authored Apr 4, 2021
2 parents 582d41e + 3f326ed commit 0769f30
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 19 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ linters:
- nakedret
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- typecheck
Expand Down
2 changes: 1 addition & 1 deletion examples/sample_1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ func main() {
return
}
}
}
}
16 changes: 11 additions & 5 deletions pkg/container/calculate_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,28 @@ func (e *Endure) implCollectorPath(vrtx *vertex.Vertex) error {
compatible = append(compatible, e.graph.Vertices[i])
// set, that we have interface deps
haveInterfaceDeps = true
e.logger.Info("vertex is compatible with Collects", zap.String("vertex id", e.graph.Vertices[i].ID), zap.String("collects from vertex", vrtx.ID))
e.logger.Debug("vertex is compatible with Collects", zap.String("vertex id", e.graph.Vertices[i].ID), zap.String("collects from vertex", vrtx.ID))
continue
}
e.logger.Info("vertex is not compatible with Collects", zap.String("vertex id", e.graph.Vertices[i].ID), zap.String("collects from vertex", vrtx.ID))
e.logger.Debug("vertex is not compatible with Collects", zap.String("vertex id", e.graph.Vertices[i].ID), zap.String("collects from vertex", vrtx.ID))
}

if len(compatible) == 0 && haveInterfaceDeps {
e.logger.Info("no compatible vertices found", zap.String("collects from vertex", vrtx.ID))
e.logger.Debug("no compatible vertices found", zap.String("collects from vertex", vrtx.ID))
return nil
}
// process mixed deps (interfaces + structs)
if haveInterfaceDeps {
return e.processInterfaceDeps(compatible, getFunctionName(fn), vrtx, params)
err = e.processInterfaceDeps(compatible, getFunctionName(fn), vrtx, params)
if err != nil {
return err
}
}
// process only struct deps if not interfaces were found
return e.processStructDeps(getFunctionName(fn), vrtx, params)
err = e.processStructDeps(getFunctionName(fn), vrtx, params)
if err != nil {
return err
}
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions tests/happy_scenarios/happyScenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func TestEndure_DoubleInitDoubleServe_OK(t *testing.T) {
assert.NoError(t, c.Init())
assert.Error(t, c.Init())

res, err := c.Serve()
_, err = c.Serve()
assert.NoError(t, err)
res, err = c.Serve()
res, err := c.Serve()
assert.Error(t, err)
go func() {
for r := range res {
Expand Down
34 changes: 34 additions & 0 deletions tests/interfaces/collects/collects_get_all_deps/plugin1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package collects_get_all_deps

type Plugin1 struct {
}

type SuperInterface interface {
Super() string
}

type Super2Interface interface {
Super2() string
}

func (f *Plugin1) Init() error {
return nil
}

func (f *Plugin1) Serve() chan error {
errCh := make(chan error)
return errCh
}

func (f *Plugin1) Stop() error {
return nil
}

// Super and Super2 interface impl
func (f *Plugin1) Super() string {
return "SUPER -> "
}

func (f *Plugin1) Super2() string {
return "I'm also SUPER2 -> "
}
40 changes: 40 additions & 0 deletions tests/interfaces/collects/collects_get_all_deps/plugin2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package collects_get_all_deps

import "github.com/spiral/errors"

type Plugin2 struct {
collectsDeps []interface{}
}

func (f *Plugin2) Init() error {
// should be 2 deps
f.collectsDeps = make([]interface{}, 0, 2)
return nil
}

func (f *Plugin2) Serve() chan error {
errCh := make(chan error)
if len(f.collectsDeps) != 2 {
errCh <- errors.E("not enough deps collected")
}
return errCh
}

func (f *Plugin2) Stop() error {
return nil
}

func (f *Plugin2) Collects() []interface{} {
return []interface{}{
f.GetSuper,
f.GetSuper2,
}
}

func (f *Plugin2) GetSuper(s SuperInterface) {
f.collectsDeps = append(f.collectsDeps, s)
}

func (f *Plugin2) GetSuper2(s Super2Interface) {
f.collectsDeps = append(f.collectsDeps, s)
}
31 changes: 31 additions & 0 deletions tests/interfaces/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/spiral/endure/tests/interfaces/plugins/plugin9"
notImplPlugin1 "github.com/spiral/endure/tests/interfaces/service/not_implemented_service/plugin1"
notImplPlugin2 "github.com/spiral/endure/tests/interfaces/service/not_implemented_service/plugin2"

collects_get_all_deps "github.com/spiral/endure/tests/interfaces/collects/collects_get_all_deps"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -193,3 +195,32 @@ func Test_MultiplyCollectsInterface(t *testing.T) {
assert.NoError(t, c.Stop())
time.Sleep(time.Second * 1)
}

func Test_MultiplyCollectsInterface2(t *testing.T) {
c, err := endure.NewContainer(nil)
assert.NoError(t, err)

assert.NoError(t, c.Register(&collects_get_all_deps.Plugin2{}))
assert.NoError(t, c.Register(&collects_get_all_deps.Plugin1{}))
err = c.Init()
if err != nil {
t.Fatal(err)
}

res, err := c.Serve()
assert.NoError(t, err)

go func() {
for r := range res {
if r.Error != nil {
assert.NoError(t, r.Error)
return
}
}
}()

time.Sleep(time.Second * 2)

assert.NoError(t, c.Stop())
time.Sleep(time.Second * 1)
}
5 changes: 2 additions & 3 deletions tests/interfaces/plugins/plugin10/plugin10.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package plugin10

import "fmt"

type Plugin10 struct {
}
type Plugin10 struct{}

// No deps
func (s *Plugin10) Init() error {
Expand All @@ -20,5 +19,5 @@ func (s *Plugin10) Stop() error {
}

func (s *Plugin10) Boo() {
fmt.Println("Boo")
fmt.Println("Boo from plugin10")
}
2 changes: 1 addition & 1 deletion tests/interfaces/plugins/plugin7/plugin7.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func (s *Plugin7) Name() string {
}

func (s *Plugin7) Boom() {
fmt.Println("Boom")
fmt.Println("Boom from plugin7")
}
24 changes: 19 additions & 5 deletions tests/interfaces/plugins/plugin8/plugin8.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package plugin8

import (
"fmt"

endure "github.com/spiral/endure/pkg/container"
"github.com/spiral/endure/tests/interfaces/plugins/plugin10"
"github.com/spiral/errors"
)

type SomeInterface interface {
Boom()
}

type Plugin8 struct {
collectedDeps []interface{}
}

// No deps
func (s *Plugin8) Init() error {
s.collectedDeps = make([]interface{}, 0, 6)
return nil
}

func (s *Plugin8) Serve() chan error {
errCh := make(chan error, 1)
// plugin7
// plugin9
// named + plugin10 (plugin7)
// named + plugin10 (plugin9)
// plugin7 + plugin10
// plugin9 + plugin10
if len(s.collectedDeps) != 6 {
errCh <- errors.E("not enough deps collected")
}
return errCh
}

Expand All @@ -37,18 +47,22 @@ func (s *Plugin8) Collects() []interface{} {
}

func (s *Plugin8) SomeCollects(named endure.Named, b SomeInterface, p10 *plugin10.Plugin10) error {
fmt.Println(named.Name())
s.collectedDeps = append(s.collectedDeps, b)
b.Boom()
return nil
}

func (s *Plugin8) SomeCollects2(named endure.Named, p10 *plugin10.Plugin10) error {
fmt.Println(named.Name())
s.collectedDeps = append(s.collectedDeps, p10)
println(named.Name())
p10.Boo()
return nil
}

func (s *Plugin8) SomeCollects3(named endure.Named, p10 *plugin10.Plugin10, named2 endure.Named, p *plugin10.Plugin10) error {
fmt.Println(named.Name())
s.collectedDeps = append(s.collectedDeps, p)
println(named.Name())
println(named2.Name())
p10.Boo()
p.Boo()
return nil
Expand Down
2 changes: 1 addition & 1 deletion tests/interfaces/plugins/plugin9/plugin9.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func (s *Plugin9) Name() string {
}

func (s *Plugin9) Boom() {
fmt.Println("Boom")
fmt.Println("Boom from plugin9")
}

0 comments on commit 0769f30

Please sign in to comment.