pubsubwrapper

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2020 License: MIT Imports: 6 Imported by: 0

README

pubSubWrapper

Wrap google pubsub golang client implementation to an interface so it can be mocked.

This wrapper is inspired by https://github.com/googleapis/google-cloud-go-testing/tree/master/pubsub/psiface adapted to feet usage in TankYou projects.

Why

  • Implement a mock using testify/mock
  • Add methods to interface which where not initialy supported by googleapis project

Documentation

Overview

Package psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub. These can be used to create mocks or other test doubles. The package also provides adapters to enable the types of the pubsub package to implement these interfaces.

We do not recommend using mocks for most testing. Please read https://testing.googleblog.com/2013/05/testing-on-toilet-dont-overuse-mocks.html.

Note: This package is in alpha. Some backwards-incompatible changes may occur.

You must embed these interfaces to implement them:

type ClientMock struct {
    psiface.Client
    ...
}

This ensures that your implementations will not break when methods are added to the interfaces.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	CreateTopic(ctx context.Context, topicID string) (Topic, error)
	Topic(id string) Topic
	Topics(ctx context.Context) ([]Topic, error)

	CreateSubscription(ctx context.Context, id string, cfg SubscriptionConfig) (Subscription, error)
	Subscription(id string) Subscription
	Subscriptions(ctx context.Context) ([]Subscription, error)
	// contains filtered or unexported methods
}

func AdaptClient

func AdaptClient(c *pubsub.Client) Client

AdaptClient adapts a pubsub.Client so that it satisfies the Client interface.

Example
package main

import (
	"context"

	"cloud.google.com/go/pubsub"

	"github.com/tankyouoss/pubsubwrapper"
)

func main() {
	ctx := context.Background()
	c, err := pubsub.NewClient(ctx, "")
	if err != nil {
		// TODO: Handle error.
	}
	client := pubsubwrapper.AdaptClient(c)
	msg := pubsubwrapper.AdaptMessage(&pubsub.Message{})
	_, err = client.Topic("my-topic").Publish(ctx, msg).Get(ctx)
	if err != nil {
		// TODO: Handle error.
	}
}
Output:

func NewMockedClient

func NewMockedClient() Client

type ClientMock

type ClientMock struct {
	mock.Mock
}

Client mock

func (*ClientMock) CreateSubscription

func (c *ClientMock) CreateSubscription(ctx context.Context, id string, cfg SubscriptionConfig) (Subscription, error)

func (*ClientMock) CreateTopic

func (c *ClientMock) CreateTopic(ctx context.Context, topicID string) (Topic, error)

func (*ClientMock) Subscription

func (c *ClientMock) Subscription(id string) Subscription

func (*ClientMock) Subscriptions

func (c *ClientMock) Subscriptions(ctx context.Context) ([]Subscription, error)

func (*ClientMock) Topic

func (c *ClientMock) Topic(id string) Topic

func (*ClientMock) Topics

func (c *ClientMock) Topics(ctx context.Context) ([]Topic, error)

type Message

type Message interface {
	ID() string
	Data() []byte
	Attributes() map[string]string
	PublishTime() time.Time
	Ack()
	Nack()
	// contains filtered or unexported methods
}

func AdaptMessage

func AdaptMessage(msg *pubsub.Message) Message

AdaptMessage adapts a pubsub.Message so that it satisfies the Message interface.

func NewMockedMessage

func NewMockedMessage() Message

type MessageMock

type MessageMock struct {
	mock.Mock
}

message

func (*MessageMock) Ack

func (m *MessageMock) Ack()

func (*MessageMock) Attributes

func (m *MessageMock) Attributes() map[string]string

func (*MessageMock) Data

func (m *MessageMock) Data() []byte

func (*MessageMock) ID

func (m *MessageMock) ID() string

func (*MessageMock) Nack

func (m *MessageMock) Nack()

func (*MessageMock) PublishTime

func (m *MessageMock) PublishTime() time.Time

type PublishResult

type PublishResult interface {
	Get(ctx context.Context) (serverID string, err error)
	// contains filtered or unexported methods
}

func NewMockedPublishResult

func NewMockedPublishResult() PublishResult

type PublishResultMock

type PublishResultMock struct {
	mock.Mock
}

publish result

func (*PublishResultMock) Get

func (p *PublishResultMock) Get(ctx context.Context) (serverID string, err error)

type Subscription

type Subscription interface {
	String() string
	Exists(ctx context.Context) (bool, error)
	Receive(ctx context.Context, f func(context.Context, Message)) error
	Delete(ctx context.Context) error
	// contains filtered or unexported methods
}

func NewMockedSubscription

func NewMockedSubscription() Subscription

type SubscriptionConfig

type SubscriptionConfig struct {
	pubsub.SubscriptionConfig
	Topic Topic // shadows pubsub.SubscriptionConfig's field
}

type SubscriptionMock

type SubscriptionMock struct {
	mock.Mock
	MessageMock Message
}

subscription

func (*SubscriptionMock) Delete

func (s *SubscriptionMock) Delete(ctx context.Context) error

func (*SubscriptionMock) Exists

func (s *SubscriptionMock) Exists(ctx context.Context) (bool, error)

func (*SubscriptionMock) Receive

func (s *SubscriptionMock) Receive(ctx context.Context, f func(context.Context, Message)) error

func (*SubscriptionMock) String

func (s *SubscriptionMock) String() string

type Topic

type Topic interface {
	String() string
	Publish(ctx context.Context, msg Message) PublishResult
	Exists(ctx context.Context) (bool, error)
	Delete(ctx context.Context) error
	// contains filtered or unexported methods
}

func NewMockedTopic

func NewMockedTopic() Topic

type TopicMock

type TopicMock struct {
	mock.Mock
}

Topic mock

func (*TopicMock) Delete

func (t *TopicMock) Delete(ctx context.Context) error

func (*TopicMock) Exists

func (t *TopicMock) Exists(ctx context.Context) (bool, error)

func (*TopicMock) Publish

func (t *TopicMock) Publish(ctx context.Context, msg Message) PublishResult

func (*TopicMock) String

func (t *TopicMock) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL