package psiface

import "github.com/googleapis/google-cloud-go-testing/pubsub/psiface"

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

Types

type Client

type Client interface {
	CreateTopic(ctx context.Context, topicID string) (Topic, error)
	Topic(id string) Topic
	CreateSubscription(ctx context.Context, id string, cfg SubscriptionConfig) (Subscription, error)
	Subscription(id string) Subscription
	// 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

Code:play 

package main

import (
	"context"

	"cloud.google.com/go/pubsub"
	"github.com/googleapis/google-cloud-go-testing/pubsub/psiface"
)

func main() {
	ctx := context.Background()
	c, err := pubsub.NewClient(ctx, "")
	if err != nil {
		// TODO: Handle error.
	}
	client := psiface.AdaptClient(c)
	msg := psiface.AdaptMessage(&pubsub.Message{})
	_, err = client.Topic("my-topic").Publish(ctx, msg).Get(ctx)
	if err != nil {
		// TODO: Handle 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.

type PublishResult

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

type Subscription

type Subscription interface {
	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
}

type SubscriptionConfig

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

type Topic

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

Source Files

adapters.go doc.go interfaces.go structs.go

Version
v0.0.0-20210719221736-1c9a4c676720 (latest)
Published
Jul 19, 2021
Platform
linux/amd64
Imports
3 packages
Last checked
3 weeks ago

Tools for package owners.