snstesting

package module
v0.0.0-...-6c6029b Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 10 Imported by: 0

README

snstesting

test golangci-lint

Package snstesting simplifies checking what messages arrive at any SNS topic from the inside of your integration tests. It does it by subscribing to SNS via ad-hoc SQS queue that is cleaned-up after the test.

Installation

go get github.com/prozz/snstesting

Usage

// make sure you can access AWS
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
    t.Fatalf("configuration error: %v ", err)
}

// subscribe to SNS, all resources created here will be cleaned up at the end of the test
receive := snstesting.New(t, cfg, topicName)

// fire your process here, whatever it is ;)

// get single message from SNS and examine it, repeat if needed
msg := receive()
assert.NotEmpty(t, msg)

In case you need more control over error handling, context or long polling settings, please use snstesting.NewSubscriber directly.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests.

License

MIT

Documentation

Overview

Package snstesting simplifies checking what messages arrive at any SNS topic from the inside of your integration tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	TopicName       string
	TopicARN        string
	QueueName       string
	QueueURL        string
	QueueARN        string
	SubscriptionARN string
}

Config describes both temporarily generated and existing resources used by the ad-hoc SNS checking mechanism.

type ReceiveFn

type ReceiveFn func() string

ReceiveFn checks for message that arrived at SNS (via ad-hoc SQS queue), can be called repeatedly.

func New

func New(t *testing.T, cfg aws.Config, topicName string) ReceiveFn

New creates Subscriber for testing purposes based on provided AWS configuration. Ad-hoc resources are cleaned up after the test automatically with use of t.Cleanup. In case of an error, t.Fatal is executed. In case more control is needed over Subscriber, or it's Config, please use NewSubscriber.

type SNSAPI

type SNSAPI interface {
	ListTopics(context.Context, *sns.ListTopicsInput, ...func(*sns.Options)) (*sns.ListTopicsOutput, error)
	Subscribe(context.Context, *sns.SubscribeInput, ...func(*sns.Options)) (*sns.SubscribeOutput, error)
	Unsubscribe(context.Context, *sns.UnsubscribeInput, ...func(*sns.Options)) (*sns.UnsubscribeOutput, error)
}

SNSAPI shows part of SNS API needed to fulfill the contract.

type SQSAPI

type SQSAPI interface {
	CreateQueue(context.Context, *sqs.CreateQueueInput, ...func(*sqs.Options)) (*sqs.CreateQueueOutput, error)
	DeleteQueue(context.Context, *sqs.DeleteQueueInput, ...func(*sqs.Options)) (*sqs.DeleteQueueOutput, error)
	GetQueueAttributes(context.Context, *sqs.GetQueueAttributesInput, ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error) //nolint
	SetQueueAttributes(context.Context, *sqs.SetQueueAttributesInput, ...func(*sqs.Options)) (*sqs.SetQueueAttributesOutput, error) //nolint
	ReceiveMessage(context.Context, *sqs.ReceiveMessageInput, ...func(*sqs.Options)) (*sqs.ReceiveMessageOutput, error)
}

SQSAPI shows part of SQS API needed to fulfill the contract.

type Subscriber

type Subscriber struct {
	SNS    SNSAPI
	SQS    SQSAPI
	Config Config
}

Subscriber for checking what arrives at any SNS topic in an integration testing setting. It does it through temporary SQS queue and ad-hoc subscription that may be easily cleaned up after the test. Temporary queues are prefixed with 'snstesting'.

func NewSubscriber

func NewSubscriber(ctx context.Context, SNS SNSAPI, SQS SQSAPI, topicName string) (Subscriber, error)

NewSubscriber creates Subscriber instance for ad-hoc subscribing to SNS topic.

func (Subscriber) Cleanup

func (s Subscriber) Cleanup(ctx context.Context) error

Cleanup unsubscribes temporary SQS queue from SNS and removes it.

func (Subscriber) Receive

func (s Subscriber) Receive(ctx context.Context) (string, error)

Receive receives single message that was published on SNS.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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