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 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 ( import (
"context" "context"

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

@ -12,7 +12,6 @@ import (
libp2p_pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb" libp2p_pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" 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) { func TestHostV2_SendMessageToGroups(t *testing.T) {
@ -21,7 +20,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish() defer mc.Finish()
groups := []nodeconfig.GroupID{"ABC", "DEF"} groups := []nodeconfig.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
pubsub := mock.NewMockpubsub(mc) pubsub := 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),
@ -36,7 +35,7 @@ func TestHostV2_SendMessageToGroups(t *testing.T) {
defer mc.Finish() defer mc.Finish()
groups := []nodeconfig.GroupID{"ABC", "DEF"} groups := []nodeconfig.GroupID{"ABC", "DEF"}
data := []byte{1, 2, 3} data := []byte{1, 2, 3}
pubsub := mock.NewMockpubsub(mc) pubsub := 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
@ -51,7 +50,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 := 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 {
@ -67,7 +66,7 @@ func pubsubMessage(from libp2p_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 := NewMocksubscription(mc)
ctx := context.Background() ctx := 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),
@ -101,7 +100,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 := 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")
@ -117,7 +116,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 := 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")

Loading…
Cancel
Save