testing

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2018 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package testing is a test suit for recorders. They should provide an object that implements the Constructor interface then run:

import rt "github.com/arsham/expipe/recorder/testing"
....
type Construct struct {
    *rt.BaseConstruct
    testServer *httptest.Server
}

func (c *Construct) TestServer() *httptest.Server {
    return /* a test server */
}

func (c *Construct) Object() (recorder.DataRecorder, error) {
    return expvar.New(c.Setters()...)
}

func TestMyRecorder(t *testing.T) {
    rt.TestSuites(t, func() (rt.Constructor, func()) {
        c := &Construct{
            testServer:    getTestServer(),
            BaseConstruct: rt.NewBaseConstruct(),
        }
        return c, func() { c.testServer.Close() }
    })
}

The test suit will pick it up and does all the tests.

Important Note

You need to write the edge cases if they are not covered in this section. The test suite might close and request the test server multiple times during its work. Make sure you return a brand new instance every time the TestServer method is been called. All tests are ran in isolation and they are set to be run as parallel. Make sure your code doesn't have any race conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TestSuites

func TestSuites(t *testing.T, setup func() (Constructor, func()))

TestSuites returns a map of test name to the runner function.

Types

type BaseConstruct added in v0.10.0

type BaseConstruct struct {
	sync.Mutex
	// contains filtered or unexported fields
}

BaseConstruct implements Constructor interface. It only remembers the setter functions, therefore you need to apply them when creating an object in the derived constructor. It is concurrent safe.

func NewBaseConstruct added in v0.10.0

func NewBaseConstruct() *BaseConstruct

NewBaseConstruct returns an instance of BaseConstruct.

func (*BaseConstruct) SetBackoff added in v0.10.0

func (b *BaseConstruct) SetBackoff(backoff int)

SetBackoff adds a Backoff value to setter configuration.

func (*BaseConstruct) SetEndpoint added in v0.10.0

func (b *BaseConstruct) SetEndpoint(endpoint string)

SetEndpoint adds a Endpoint value to setter configuration.

func (*BaseConstruct) SetIndexName added in v0.10.0

func (b *BaseConstruct) SetIndexName(indexName string)

SetIndexName adds an IndexName value to setter configuration.

func (*BaseConstruct) SetLogger added in v0.10.0

func (b *BaseConstruct) SetLogger(logger tools.FieldLogger)

SetLogger adds a Logger value to setter configuration.

func (*BaseConstruct) SetName added in v0.10.0

func (b *BaseConstruct) SetName(name string)

SetName adds a Name value to setter configuration.

func (*BaseConstruct) SetTimeout added in v0.10.0

func (b *BaseConstruct) SetTimeout(timeout time.Duration)

SetTimeout adds a Timeout value to setter configuration.

func (*BaseConstruct) Setters added in v0.10.0

func (b *BaseConstruct) Setters() []func(recorder.Constructor) error

Setters returns a copy of the configuration functions.

type Config

type Config struct {
	MockName      string
	MockEndpoint  string
	MockTimeout   time.Duration
	MockBackoff   int
	MockIndexName string
	MockLogger    tools.FieldLogger
}

Config holds the necessary configuration for setting up an elasticsearch recorder endpoint.

func (*Config) Backoff

func (c *Config) Backoff() int

Backoff is the mocked version.

func (*Config) Endpoint

func (c *Config) Endpoint() string

Endpoint is the mocked version.

func (*Config) IndexName

func (c *Config) IndexName() string

IndexName is the mocked version.

func (*Config) Logger

func (c *Config) Logger() tools.FieldLogger

Logger is the mocked version.

func (*Config) Name

func (c *Config) Name() string

Name is the mocked version.

func (*Config) Recorder added in v0.10.0

func (c *Config) Recorder() (recorder.DataRecorder, error)

Recorder implements the RecorderConf interface.

func (*Config) Timeout

func (c *Config) Timeout() time.Duration

Timeout is the mocked version.

type Constructor

type Constructor interface {
	recorder.Constructor
	ValidEndpoints() []string
	InvalidEndpoints() []string
	TestServer() *httptest.Server
	Object() (recorder.DataRecorder, error)
}

Constructor is an interface for setting up an object for testing. TestServer() should return a ready to use test server

type Recorder

type Recorder struct {
	ErrorFunc  func() error
	Smu        sync.RWMutex
	RecordFunc func(context.Context, *recorder.Job) error
	PingFunc   func() error
	Pinged     bool
	// contains filtered or unexported fields
}

Recorder is designed to be used in tests.

func GetRecorder

func GetRecorder(url string) *Recorder

GetRecorder provides a SimpleRecorder for using in the example.

func New

func New(options ...func(recorder.Constructor) error) (*Recorder, error)

New is a recorder for using in tests.

func (*Recorder) Backoff

func (r *Recorder) Backoff() int

Backoff returns the backoff.

func (*Recorder) Endpoint

func (r *Recorder) Endpoint() string

Endpoint returns the endpoint.

func (*Recorder) IndexName

func (r *Recorder) IndexName() string

IndexName returns the index name.

func (*Recorder) Logger

func (r *Recorder) Logger() tools.FieldLogger

Logger returns the log.

func (*Recorder) Name

func (r *Recorder) Name() string

Name returns the name.

func (*Recorder) Ping

func (r *Recorder) Ping() error

Ping pings the endpoint and return nil if was successful.

func (*Recorder) Record

func (r *Recorder) Record(ctx context.Context, job *recorder.Job) error

Record calls the RecordFunc if exists, otherwise continues as normal.

func (*Recorder) SetBackoff

func (r *Recorder) SetBackoff(backoff int)

SetBackoff sets the backoff of the recorder.

func (*Recorder) SetEndpoint

func (r *Recorder) SetEndpoint(endpoint string)

SetEndpoint sets the endpoint of the recorder.

func (*Recorder) SetIndexName

func (r *Recorder) SetIndexName(indexName string)

SetIndexName sets the index name of the recorder.

func (*Recorder) SetLogger

func (r *Recorder) SetLogger(log tools.FieldLogger)

SetLogger sets the log of the recorder.

func (*Recorder) SetName

func (r *Recorder) SetName(name string)

SetName sets the name of the recorder.

func (*Recorder) SetTimeout

func (r *Recorder) SetTimeout(timeout time.Duration)

SetTimeout sets the timeout of the recorder.

func (*Recorder) Timeout

func (r *Recorder) Timeout() time.Duration

Timeout returns the timeout.

Source Files

  • config.go
  • doc.go
  • example_provider.go
  • recorder.go
  • testing.go
  • testing_communication.go
  • testing_construction.go
  • testing_endpoints.go

Jump to

Keyboard shortcuts

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