Generate mocks in the same package

This enables mocking private interfaces.
pull/1862/head
Eugene Kim 5 years ago
parent daf78e5457
commit f3e268ab2b
  1. 2
      p2p/host/hostv2/hostv2.go
  2. 4
      p2p/host/hostv2/hostv2_mock_for_test.go
  3. 13
      p2p/host/hostv2/hostv2_test.go

@ -1,6 +1,6 @@
package hostv2
//go:generate mockgen -source hostv2.go -destination=mock/hostv2_mock.go
//go:generate mockgen -source=hostv2.go -package=hostv2 -destination=hostv2_mock_for_test.go
import (
"context"

@ -1,8 +1,8 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: hostv2.go
// Package mock_hostv2 is a generated GoMock package.
package mock_hostv2
// Package hostv2 is a generated GoMock package.
package hostv2
import (
context "context"

@ -12,7 +12,6 @@ import (
libp2p_pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
mock "github.com/harmony-one/harmony/p2p/host/hostv2/mock"
)
func TestHostV2_SendMessageToGroups(t *testing.T) {
@ -21,7 +20,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish()
groups := []nodeconfig.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3}
pubsub := mock.NewMockpubsub(mc)
pubsub := NewMockpubsub(mc)
gomock.InOrder(
pubsub.EXPECT().Publish("ABC", data),
pubsub.EXPECT().Publish("DEF", data),
@ -36,7 +35,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish()
groups := []nodeconfig.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3}
pubsub := mock.NewMockpubsub(mc)
pubsub := NewMockpubsub(mc)
gomock.InOrder(
pubsub.EXPECT().Publish("ABC", data).Return(errors.New("FIAL")),
pubsub.EXPECT().Publish("DEF", data), // Should not early-return
@ -51,7 +50,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
func TestGroupReceiver_Close(t *testing.T) {
mc := gomock.NewController(t)
defer mc.Finish()
sub := mock.NewMocksubscription(mc)
sub := NewMocksubscription(mc)
sub.EXPECT().Cancel()
receiver := GroupReceiverImpl{sub: sub}
if err := receiver.Close(); err != nil {
@ -67,7 +66,7 @@ func pubsubMessage(from libp2p_peer.ID, data []byte) *libp2p_pubsub.Message {
func TestGroupReceiver_Receive(t *testing.T) {
mc := gomock.NewController(t)
defer mc.Finish()
sub := mock.NewMocksubscription(mc)
sub := NewMocksubscription(mc)
ctx := context.Background()
gomock.InOrder(
sub.EXPECT().Next(ctx).Return(pubsubMessage("ABC", []byte{1, 2, 3}), nil),
@ -101,7 +100,7 @@ func TestHostV2_GroupReceiver(t *testing.T) {
mc := gomock.NewController(t)
defer mc.Finish()
sub := &libp2p_pubsub.Subscription{}
pubsub := mock.NewMockpubsub(mc)
pubsub := NewMockpubsub(mc)
pubsub.EXPECT().Subscribe("ABC").Return(sub, nil)
host := &HostV2{pubsub: pubsub}
gotReceiver, err := host.GroupReceiver("ABC")
@ -117,7 +116,7 @@ func TestHostV2_GroupReceiver(t *testing.T) {
t.Run("Error", func(t *testing.T) {
mc := gomock.NewController(t)
defer mc.Finish()
pubsub := mock.NewMockpubsub(mc)
pubsub := NewMockpubsub(mc)
pubsub.EXPECT().Subscribe("ABC").Return(nil, errors.New("FIAL"))
host := &HostV2{pubsub: pubsub}
gotReceiver, err := host.GroupReceiver("ABC")

Loading…
Cancel
Save