Skip to content

Commit cc9f653

Browse files
bors[bot]Valery Piashchynski
andauthored
Merge #46
46: internal.go: make Service interface optional to implement r=48d90782 a=48d90782 Co-authored-by: Valery Piashchynski <piashchynski_valery@hotmail.com>
2 parents 28e3a46 + 121ed70 commit cc9f653

File tree

9 files changed

+84
-46
lines changed

9 files changed

+84
-46
lines changed

calculate_deps.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (e *Endure) addEdges() error {
6969
// and we can safely skip the OK parameter here
7070
init, _ := reflect.TypeOf(vrtx.Iface).MethodByName(InitMethodName)
7171

72+
if init.Type == nil {
73+
e.logger.Fatal("init method is absent in struct", zap.String("vertexId", vertexID))
74+
return errNoInitMethodInStructure
75+
}
76+
7277
/* Add the dependencies (if) which this vertex needs to init
7378
Information we know at this step is:
7479
1. vertexID
@@ -168,7 +173,7 @@ func (e *Endure) addDependersDeps(vertexID string, vertex interface{}) error {
168173
}
169174

170175
func (e *Endure) addInitDeps(vertexID string, initMethod reflect.Method) error {
171-
// S2 init args
176+
// Init function in arguments
172177
initArgs := functionParameters(initMethod)
173178

174179
// iterate over all function parameters

endure.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ func (e *Endure) Register(vertex interface{}) error {
178178
return errors.New("you should pass pointer to the structure instead of value")
179179
}
180180

181-
ok := t.Implements(reflect.TypeOf((*Service)(nil)).Elem())
182-
if !ok {
183-
return errTypeNotImplementError
184-
}
185-
186181
/* Depender the type
187182
Information we know at this step is:
188183
1. vertexID

errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ var ErrorDuringServe = Error{
4646
var errTypeNotImplementError = errors.New("type should implement Service interface")
4747
var errVertexAlreadyExists = func(name string) error { return fmt.Errorf("vertex `%s` already exists", name) }
4848
var errUnknownErrorOccurred = errors.New("unknown error occurred during the function call")
49+
var errNoInitMethodInStructure = errors.New("init method is absent in struct")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ require (
77
github.com/pkg/errors v0.9.1
88
github.com/stretchr/testify v1.6.1
99
go.uber.org/zap v1.16.0
10-
)
10+
)

internal.go

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,31 @@ func (e *Endure) callConfigureFn(vertex *structures.Vertex, in []reflect.Value)
426426
return nil
427427
}
428428

429+
func (e *Endure) stop(vID string) error {
430+
vertex := e.graph.GetVertex(vID)
431+
if reflect.TypeOf(vertex.Iface).Implements(reflect.TypeOf((*Service)(nil)).Elem()) {
432+
in := make([]reflect.Value, 0, 1)
433+
// add service itself
434+
in = append(in, reflect.ValueOf(vertex.Iface))
435+
436+
err := e.callStopFn(vertex, in)
437+
if err != nil {
438+
e.logger.Error("error occurred during the callStopFn", zap.String("vertex id", vertex.ID))
439+
return err
440+
}
441+
442+
if reflect.TypeOf(vertex.Iface).Implements(reflect.TypeOf((*Graceful)(nil)).Elem()) {
443+
err = e.callCloseFn(vertex.ID, in)
444+
if err != nil {
445+
e.logger.Error("error occurred during the callCloseFn", zap.String("vertex id", vertex.ID))
446+
return err
447+
}
448+
}
449+
}
450+
451+
return nil
452+
}
453+
429454
func (e *Endure) callStopFn(vertex *structures.Vertex, in []reflect.Value) error {
430455
// Call Stop() method, which returns only error (or nil)
431456
e.logger.Debug("stopping vertex", zap.String("vertexId", vertex.ID))
@@ -441,30 +466,6 @@ func (e *Endure) callStopFn(vertex *structures.Vertex, in []reflect.Value) error
441466
return nil
442467
}
443468

444-
func (e *Endure) stop(vID string) error {
445-
vertex := e.graph.GetVertex(vID)
446-
447-
in := make([]reflect.Value, 0, 1)
448-
// add service itself
449-
in = append(in, reflect.ValueOf(vertex.Iface))
450-
451-
err := e.callStopFn(vertex, in)
452-
if err != nil {
453-
e.logger.Error("error occurred during the callStopFn", zap.String("vertex id", vertex.ID))
454-
return err
455-
}
456-
457-
if reflect.TypeOf(vertex.Iface).Implements(reflect.TypeOf((*Graceful)(nil)).Elem()) {
458-
err = e.callCloseFn(vertex.ID, in)
459-
if err != nil {
460-
e.logger.Error("error occurred during the callCloseFn", zap.String("vertex id", vertex.ID))
461-
return err
462-
}
463-
}
464-
465-
return nil
466-
}
467-
468469
// TODO add stack to the all of the log events
469470
func (e *Endure) callCloseFn(vID string, in []reflect.Value) error {
470471
v := e.graph.GetVertex(vID)
@@ -563,21 +564,24 @@ func (e *Endure) forceExitHandler(ctx context.Context, data chan *structures.Dll
563564

564565
// serve run configure (if exist) and callServeFn for each node and put the results in the map
565566
func (e *Endure) serve(n *structures.DllNode) error {
566-
// handle all configure
567-
in := make([]reflect.Value, 0, 1)
568-
// add service itself
569-
in = append(in, reflect.ValueOf(n.Vertex.Iface))
567+
// check if type implements serve, if implements, call serve
568+
if reflect.TypeOf(n.Vertex.Iface).Implements(reflect.TypeOf((*Service)(nil)).Elem()) {
569+
// handle all configure
570+
in := make([]reflect.Value, 0, 1)
571+
// add service itself
572+
in = append(in, reflect.ValueOf(n.Vertex.Iface))
570573

571-
res := e.callServeFn(n.Vertex, in)
572-
if res != nil {
573-
e.results.Store(res.vertexID, res)
574-
} else {
575-
e.logger.Error("nil result returned from the vertex", zap.String("vertex id", n.Vertex.ID), zap.String("tip:", "serve function should return initialized channel with errors"))
576-
return fmt.Errorf("nil result returned from the vertex, vertex id: %s", n.Vertex.ID)
577-
}
574+
res := e.callServeFn(n.Vertex, in)
575+
if res != nil {
576+
e.results.Store(res.vertexID, res)
577+
} else {
578+
e.logger.Error("nil result returned from the vertex", zap.String("vertex id", n.Vertex.ID), zap.String("tip:", "serve function should return initialized channel with errors"))
579+
return fmt.Errorf("nil result returned from the vertex, vertex id: %s", n.Vertex.ID)
580+
}
578581

579-
// start poll the vertex
580-
e.poll(res)
582+
// start poll the vertex
583+
e.poll(res)
584+
}
581585

582586
return nil
583587
}

tests/backoff/backoff_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ func TestEndure_MainThread_Backoff(t *testing.T) {
9191
wg.Wait()
9292

9393
after := time.Now().Second()
94-
// after - now should not be more than 11 as we set in NewContainer
95-
assert.Greater(t, 11, after-now, "time")
94+
// after - now should not be more than 15 as we set in NewContainer
95+
assert.Greater(t, 15, after-now, "time")
9696
}
9797

9898
func TestEndure_BackoffTimers(t *testing.T) {

tests/interfaces/interfaces_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/spiral/endure/tests/interfaces/plugins/plugin3"
1414
"github.com/spiral/endure/tests/interfaces/plugins/plugin4"
1515
"github.com/spiral/endure/tests/interfaces/plugins/plugin5"
16+
notImplPlugin1 "github.com/spiral/endure/tests/interfaces/service/not_implemented_service/plugin1"
17+
notImplPlugin2 "github.com/spiral/endure/tests/interfaces/service/not_implemented_service/plugin2"
1618
"github.com/stretchr/testify/assert"
1719
)
1820

@@ -110,3 +112,18 @@ func TestEndure_NamedProvides_WrongType_Fail(t *testing.T) {
110112

111113
assert.NoError(t, c.Stop())
112114
}
115+
116+
func TestEndure_ServiceInterface_NotImplemented_Ok(t *testing.T) {
117+
c, err := endure.NewContainer(endure.DebugLevel)
118+
assert.NoError(t, err)
119+
120+
assert.NoError(t, c.Register(&notImplPlugin1.Foo{}))
121+
assert.NoError(t, c.Register(&notImplPlugin2.Foo{}))
122+
123+
assert.NoError(t, c.Init())
124+
125+
_, err = c.Serve()
126+
assert.NoError(t, err)
127+
128+
assert.NoError(t, c.Stop())
129+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package plugin1
2+
3+
type Foo struct {
4+
}
5+
6+
func (f *Foo) Init() error {
7+
return nil
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package plugin2
2+
3+
type Foo struct {
4+
}
5+
6+
func (f *Foo) Init() error {
7+
return nil
8+
}

0 commit comments

Comments
 (0)