testutil

package
v9.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package testutil contains helper functions for writing tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff added in v0.11.0

func Diff(x, y interface{}, opts ...cmp.Option) string

Diff reports the differences between two values. Diff(x, y) == "" iff Equal(x, y).

func Equal added in v0.11.0

func Equal(x, y interface{}, opts ...cmp.Option) bool

Equal tests two values for equality.

func JWTConfig added in v0.17.0

func JWTConfig() (*jwt.Config, error)

JWTConfig reads the JSON private key file whose name is in the default environment variable, and returns the jwt.Config it contains. It ignores scopes. If the environment variable is empty, it returns (nil, nil).

func PageBounds added in v0.18.0

func PageBounds(pageSize int, pageToken string, length int) (from, to int, nextPageToken string, err error)

PageBounds converts an incoming page size and token from an RPC request into slice bounds and the outgoing next-page token.

PageBounds assumes that the complete, unpaginated list of items exists as a single slice. In addition to the page size and token, PageBounds needs the length of that slice.

PageBounds's first two return values should be used to construct a sub-slice of the complete, unpaginated slice. E.g. if the complete slice is s, then s[from:to] is the desired page. Its third return value should be set as the NextPageToken field of the RPC response.

func ProjID

func ProjID() string

ProjID returns the project ID to use in integration tests, or the empty string if none is configured.

func TokenSource

func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource

TokenSource returns the OAuth2 token source to use in integration tests, or nil if none is configured. It uses the standard environment variable for tests in this repo.

func TokenSourceEnv added in v0.15.0

func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource

TokenSourceEnv returns the OAuth2 token source to use in integration tests. or nil if none is configured. It tries to get credentials from the filename in the environment variable envVar. If the environment variable is unset, TokenSourceEnv will try to find 'Application Default Credentials'. Else, TokenSourceEnv will return nil. TokenSourceEnv will log.Fatal if the token source is specified but missing or invalid.

Types

type Server

type Server struct {
	Addr string

	Gsrv *grpc.Server
	// contains filtered or unexported fields
}

A Server is an in-process gRPC server, listening on a system-chosen port on the local loopback interface. Servers are for testing only and are not intended to be used in production code.

To create a server, make a new Server, register your handlers, then call Start:

srv, err := NewServer()
...
mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler)
....
srv.Start()

Clients should connect to the server with no security:

conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
...

func NewServer

func NewServer(opts ...grpc.ServerOption) (*Server, error)

NewServer creates a new Server. The Server will be listening for gRPC connections at the address named by the Addr field, without TLS.

func (*Server) Close

func (s *Server) Close()

Close shuts down the server.

func (*Server) Start

func (s *Server) Start()

Start causes the server to start accepting incoming connections. Call Start after registering handlers.

type UIDSpace added in v0.8.0

type UIDSpace struct {
	Prefix string
	Sep    rune
	// contains filtered or unexported fields
}

A UIDSpace manages a set of unique IDs distinguished by a prefix.

func NewUIDSpace added in v0.8.0

func NewUIDSpace(prefix string) *UIDSpace

func NewUIDSpaceSep added in v0.16.0

func NewUIDSpaceSep(prefix string, sep rune) *UIDSpace

func (*UIDSpace) New added in v0.8.0

func (s *UIDSpace) New() string

New generates a new unique ID . The ID consists of the UIDSpace's prefix, a timestamp, and a counter value. All unique IDs generated in the same test execution will have the same timestamp.

Aside from the characters in the prefix, IDs contain only letters, numbers and sep.

func (*UIDSpace) Older added in v0.8.0

func (s *UIDSpace) Older(uid string, d time.Duration) bool

Older reports whether uid was created by m and has a timestamp older than the current time by at least d.

func (*UIDSpace) Timestamp added in v0.8.0

func (s *UIDSpace) Timestamp(uid string) (time.Time, bool)

Timestamp extracts the timestamp of uid, which must have been generated by s. The second return value is true on success, false if there was a problem.

Jump to

Keyboard shortcuts

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