peer manager: Disable in tests

This commit is contained in:
Łukasz Magiera
2019-10-23 13:02:00 +02:00
parent fc7c7ddd97
commit 1d1f468c98
5 changed files with 62 additions and 33 deletions
+39
View File
@@ -1,6 +1,7 @@
package node
import (
"go.uber.org/fx"
"reflect"
)
@@ -38,6 +39,44 @@ func ApplyIf(check func(s *Settings) bool, opts ...Option) Option {
}
}
// Override option changes constructor for a given type
func Override(typ, constructor interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = fx.Invoke(constructor)
return nil
}
if c, ok := typ.(special); ok {
s.modules[c] = fx.Provide(constructor)
return nil
}
ctor := as(constructor, typ)
rt := reflect.TypeOf(typ).Elem()
s.modules[rt] = fx.Provide(ctor)
return nil
}
}
func Unset(typ interface{}) Option {
return func(s *Settings) error {
if i, ok := typ.(invoke); ok {
s.invokes[i] = nil
return nil
}
if c, ok := typ.(special); ok {
delete(s.modules, c)
return nil
}
rt := reflect.TypeOf(typ).Elem()
delete(s.modules, rt)
return nil
}
}
// from go-ipfs
// as casts input constructor to a given interface (if a value is given, it
// wraps it into a constructor).