psiface

package
v0.0.0-...-1c9a4c6 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

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
	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
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.
	}
}
Output:

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
}

Jump to

Keyboard shortcuts

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