Unexport internally used interfaces

pull/456/head
Eugene Kim 6 years ago
parent a62b65ece9
commit f05eca3f8a
  1. 12
      p2p/host/hostv2/hostv2.go
  2. 12
      p2p/host/hostv2/hostv2_test.go
  3. 68
      p2p/host/hostv2/mock/hostv2_mock.go

@ -32,8 +32,8 @@ const (
numOutgoing = 16 numOutgoing = 16
) )
// PubSub captures the pubsub interface we expect from libp2p. // pubsub captures the pubsub interface we expect from libp2p.
type PubSub interface { type pubsub interface {
Publish(topic string, data []byte) error Publish(topic string, data []byte) error
Subscribe(topic string, opts ...libp2p_pubsub.SubOpt) (*libp2p_pubsub.Subscription, error) Subscribe(topic string, opts ...libp2p_pubsub.SubOpt) (*libp2p_pubsub.Subscription, error)
} }
@ -41,7 +41,7 @@ type PubSub interface {
// HostV2 is the version 2 p2p host // HostV2 is the version 2 p2p host
type HostV2 struct { type HostV2 struct {
h p2p_host.Host h p2p_host.Host
pubsub PubSub pubsub pubsub
self p2p.Peer self p2p.Peer
priKey p2p_crypto.PrivKey priKey p2p_crypto.PrivKey
lock sync.Mutex lock sync.Mutex
@ -62,15 +62,15 @@ func (host *HostV2) SendMessageToGroups(groups []p2p.GroupID, msg []byte) error
return error return error
} }
// Subscription captures the subscription interface // subscription captures the subscription interface we expect from libp2p.
type Subscription interface { type subscription interface {
Next(ctx context.Context) (*libp2p_pubsub.Message, error) Next(ctx context.Context) (*libp2p_pubsub.Message, error)
Cancel() Cancel()
} }
// GroupReceiverImpl is a multicast group receiver implementation. // GroupReceiverImpl is a multicast group receiver implementation.
type GroupReceiverImpl struct { type GroupReceiverImpl struct {
sub Subscription sub subscription
} }
// Close closes this receiver. // Close closes this receiver.

@ -22,7 +22,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish() defer mc.Finish()
groups := []p2p.GroupID{"ABC", "DEF"} groups := []p2p.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
pubsub := mock.NewMockPubSub(mc) pubsub := mock.NewMockpubsub(mc)
gomock.InOrder( gomock.InOrder(
pubsub.EXPECT().Publish("ABC", data), pubsub.EXPECT().Publish("ABC", data),
pubsub.EXPECT().Publish("DEF", data), pubsub.EXPECT().Publish("DEF", data),
@ -37,7 +37,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish() defer mc.Finish()
groups := []p2p.GroupID{"ABC", "DEF"} groups := []p2p.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
pubsub := mock.NewMockPubSub(mc) pubsub := mock.NewMockpubsub(mc)
gomock.InOrder( gomock.InOrder(
pubsub.EXPECT().Publish("ABC", data).Return(errors.New("FIAL")), pubsub.EXPECT().Publish("ABC", data).Return(errors.New("FIAL")),
pubsub.EXPECT().Publish("DEF", data), // Should not early-return pubsub.EXPECT().Publish("DEF", data), // Should not early-return
@ -52,7 +52,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
func TestGroupReceiver_Close(t *testing.T) { func TestGroupReceiver_Close(t *testing.T) {
mc := gomock.NewController(t) mc := gomock.NewController(t)
defer mc.Finish() defer mc.Finish()
sub := mock.NewMockSubscription(mc) sub := mock.NewMocksubscription(mc)
sub.EXPECT().Cancel() sub.EXPECT().Cancel()
receiver := GroupReceiverImpl{sub: sub} receiver := GroupReceiverImpl{sub: sub}
if err := receiver.Close(); err != nil { if err := receiver.Close(); err != nil {
@ -68,7 +68,7 @@ func pubsubMessage(from peer.ID, data []byte) *libp2p_pubsub.Message {
func TestGroupReceiver_Receive(t *testing.T) { func TestGroupReceiver_Receive(t *testing.T) {
mc := gomock.NewController(t) mc := gomock.NewController(t)
defer mc.Finish() defer mc.Finish()
sub := mock.NewMockSubscription(mc) sub := mock.NewMocksubscription(mc)
ctx, _ := context.WithCancel(context.Background()) ctx, _ := context.WithCancel(context.Background())
gomock.InOrder( gomock.InOrder(
sub.EXPECT().Next(ctx).Return(pubsubMessage("ABC", []byte{1, 2, 3}), nil), sub.EXPECT().Next(ctx).Return(pubsubMessage("ABC", []byte{1, 2, 3}), nil),
@ -102,7 +102,7 @@ func TestHostV2_GroupReceiver(t *testing.T) {
mc := gomock.NewController(t) mc := gomock.NewController(t)
defer mc.Finish() defer mc.Finish()
sub := &libp2p_pubsub.Subscription{} sub := &libp2p_pubsub.Subscription{}
pubsub := mock.NewMockPubSub(mc) pubsub := mock.NewMockpubsub(mc)
pubsub.EXPECT().Subscribe("ABC").Return(sub, nil) pubsub.EXPECT().Subscribe("ABC").Return(sub, nil)
host := &HostV2{pubsub: pubsub} host := &HostV2{pubsub: pubsub}
gotReceiver, err := host.GroupReceiver("ABC") gotReceiver, err := host.GroupReceiver("ABC")
@ -118,7 +118,7 @@ func TestHostV2_GroupReceiver(t *testing.T) {
t.Run("Error", func(t *testing.T) { t.Run("Error", func(t *testing.T) {
mc := gomock.NewController(t) mc := gomock.NewController(t)
defer mc.Finish() defer mc.Finish()
pubsub := mock.NewMockPubSub(mc) pubsub := mock.NewMockpubsub(mc)
pubsub.EXPECT().Subscribe("ABC").Return(nil, errors.New("FIAL")) pubsub.EXPECT().Subscribe("ABC").Return(nil, errors.New("FIAL"))
host := &HostV2{pubsub: pubsub} host := &HostV2{pubsub: pubsub}
gotReceiver, err := host.GroupReceiver("ABC") gotReceiver, err := host.GroupReceiver("ABC")

@ -11,43 +11,43 @@ import (
reflect "reflect" reflect "reflect"
) )
// MockPubSub is a mock of PubSub interface // Mockpubsub is a mock of pubsub interface
type MockPubSub struct { type Mockpubsub struct {
ctrl *gomock.Controller ctrl *gomock.Controller
recorder *MockPubSubMockRecorder recorder *MockpubsubMockRecorder
} }
// MockPubSubMockRecorder is the mock recorder for MockPubSub // MockpubsubMockRecorder is the mock recorder for Mockpubsub
type MockPubSubMockRecorder struct { type MockpubsubMockRecorder struct {
mock *MockPubSub mock *Mockpubsub
} }
// NewMockPubSub creates a new mock instance // NewMockpubsub creates a new mock instance
func NewMockPubSub(ctrl *gomock.Controller) *MockPubSub { func NewMockpubsub(ctrl *gomock.Controller) *Mockpubsub {
mock := &MockPubSub{ctrl: ctrl} mock := &Mockpubsub{ctrl: ctrl}
mock.recorder = &MockPubSubMockRecorder{mock} mock.recorder = &MockpubsubMockRecorder{mock}
return mock return mock
} }
// EXPECT returns an object that allows the caller to indicate expected use // EXPECT returns an object that allows the caller to indicate expected use
func (m *MockPubSub) EXPECT() *MockPubSubMockRecorder { func (m *Mockpubsub) EXPECT() *MockpubsubMockRecorder {
return m.recorder return m.recorder
} }
// Publish mocks base method // Publish mocks base method
func (m *MockPubSub) Publish(topic string, data []byte) error { func (m *Mockpubsub) Publish(topic string, data []byte) error {
ret := m.ctrl.Call(m, "Publish", topic, data) ret := m.ctrl.Call(m, "Publish", topic, data)
ret0, _ := ret[0].(error) ret0, _ := ret[0].(error)
return ret0 return ret0
} }
// Publish indicates an expected call of Publish // Publish indicates an expected call of Publish
func (mr *MockPubSubMockRecorder) Publish(topic, data interface{}) *gomock.Call { func (mr *MockpubsubMockRecorder) Publish(topic, data interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Publish", reflect.TypeOf((*MockPubSub)(nil).Publish), topic, data) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Publish", reflect.TypeOf((*Mockpubsub)(nil).Publish), topic, data)
} }
// Subscribe mocks base method // Subscribe mocks base method
func (m *MockPubSub) Subscribe(topic string, opts ...go_libp2p_pubsub.SubOpt) (*go_libp2p_pubsub.Subscription, error) { func (m *Mockpubsub) Subscribe(topic string, opts ...go_libp2p_pubsub.SubOpt) (*go_libp2p_pubsub.Subscription, error) {
varargs := []interface{}{topic} varargs := []interface{}{topic}
for _, a := range opts { for _, a := range opts {
varargs = append(varargs, a) varargs = append(varargs, a)
@ -59,36 +59,36 @@ func (m *MockPubSub) Subscribe(topic string, opts ...go_libp2p_pubsub.SubOpt) (*
} }
// Subscribe indicates an expected call of Subscribe // Subscribe indicates an expected call of Subscribe
func (mr *MockPubSubMockRecorder) Subscribe(topic interface{}, opts ...interface{}) *gomock.Call { func (mr *MockpubsubMockRecorder) Subscribe(topic interface{}, opts ...interface{}) *gomock.Call {
varargs := append([]interface{}{topic}, opts...) varargs := append([]interface{}{topic}, opts...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subscribe", reflect.TypeOf((*MockPubSub)(nil).Subscribe), varargs...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subscribe", reflect.TypeOf((*Mockpubsub)(nil).Subscribe), varargs...)
} }
// MockSubscription is a mock of Subscription interface // Mocksubscription is a mock of subscription interface
type MockSubscription struct { type Mocksubscription struct {
ctrl *gomock.Controller ctrl *gomock.Controller
recorder *MockSubscriptionMockRecorder recorder *MocksubscriptionMockRecorder
} }
// MockSubscriptionMockRecorder is the mock recorder for MockSubscription // MocksubscriptionMockRecorder is the mock recorder for Mocksubscription
type MockSubscriptionMockRecorder struct { type MocksubscriptionMockRecorder struct {
mock *MockSubscription mock *Mocksubscription
} }
// NewMockSubscription creates a new mock instance // NewMocksubscription creates a new mock instance
func NewMockSubscription(ctrl *gomock.Controller) *MockSubscription { func NewMocksubscription(ctrl *gomock.Controller) *Mocksubscription {
mock := &MockSubscription{ctrl: ctrl} mock := &Mocksubscription{ctrl: ctrl}
mock.recorder = &MockSubscriptionMockRecorder{mock} mock.recorder = &MocksubscriptionMockRecorder{mock}
return mock return mock
} }
// EXPECT returns an object that allows the caller to indicate expected use // EXPECT returns an object that allows the caller to indicate expected use
func (m *MockSubscription) EXPECT() *MockSubscriptionMockRecorder { func (m *Mocksubscription) EXPECT() *MocksubscriptionMockRecorder {
return m.recorder return m.recorder
} }
// Next mocks base method // Next mocks base method
func (m *MockSubscription) Next(ctx context.Context) (*go_libp2p_pubsub.Message, error) { func (m *Mocksubscription) Next(ctx context.Context) (*go_libp2p_pubsub.Message, error) {
ret := m.ctrl.Call(m, "Next", ctx) ret := m.ctrl.Call(m, "Next", ctx)
ret0, _ := ret[0].(*go_libp2p_pubsub.Message) ret0, _ := ret[0].(*go_libp2p_pubsub.Message)
ret1, _ := ret[1].(error) ret1, _ := ret[1].(error)
@ -96,16 +96,16 @@ func (m *MockSubscription) Next(ctx context.Context) (*go_libp2p_pubsub.Message,
} }
// Next indicates an expected call of Next // Next indicates an expected call of Next
func (mr *MockSubscriptionMockRecorder) Next(ctx interface{}) *gomock.Call { func (mr *MocksubscriptionMockRecorder) Next(ctx interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Next", reflect.TypeOf((*MockSubscription)(nil).Next), ctx) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Next", reflect.TypeOf((*Mocksubscription)(nil).Next), ctx)
} }
// Cancel mocks base method // Cancel mocks base method
func (m *MockSubscription) Cancel() { func (m *Mocksubscription) Cancel() {
m.ctrl.Call(m, "Cancel") m.ctrl.Call(m, "Cancel")
} }
// Cancel indicates an expected call of Cancel // Cancel indicates an expected call of Cancel
func (mr *MockSubscriptionMockRecorder) Cancel() *gomock.Call { func (mr *MocksubscriptionMockRecorder) Cancel() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cancel", reflect.TypeOf((*MockSubscription)(nil).Cancel)) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cancel", reflect.TypeOf((*Mocksubscription)(nil).Cancel))
} }

Loading…
Cancel
Save