emulator

package
v0.0.0-...-e1efe0e Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedRequest is returned by backends when they cannot
	// process a particular mongo client request.
	ErrUnsupportedRequest = xerrors.New("unsupported request")

	// ErrInvalidCursor is returned by backends when a request includes an
	// unknown/invalid cursor ID.
	ErrInvalidCursor = xerrors.New("invalid cursor")
)

Functions

This section is empty.

Types

type Backend

type Backend interface {
	// Name returns the name of the backend.
	Name() string

	// HandleRequest processes a decoded client request and returns back
	// a Response payload.
	HandleRequest(clientID string, req protocol.Request) (protocol.Response, error)

	// RemoveClient is invoked when a particular client disconnects and
	// allows the backend to perform any required state cleanup tasks.
	RemoveClient(clientID string) error
}

Backend is implemented by types that can emulate mongo commands and generate suitable responses.

type MongoEmulator

type MongoEmulator struct {
	// contains filtered or unexported fields
}

MongoEmulator emulates a mongo server by delegating CRUD requests to a pluggable backend and handling a subset of common mongo commands.

func NewMongoEmulator

func NewMongoEmulator(b Backend, logger *logrus.Entry) (*MongoEmulator, error)

NewMongoEmulator returns a MongoEmulator instance that delegates CRUD operations to the provided Backend instance.

func (*MongoEmulator) HandleRequest

func (emu *MongoEmulator) HandleRequest(clientID string, w io.Writer, reqData []byte) error

HandleRequest implements the RequestHandler interface. This method processes an incoming mongo request by first dispatching it to the configured backend. If the backend is unable to handle the request, the method checks whether the request includes one of the generic mongo commands supported by the emulator and handles it instead.

If an error occurs while handling a request, the emulator will first check whether the request expects a response and if so, serialize the error and write it back to the response stream. Otherwise, the error is buffered and can be retrieved by the client via a getLastError command.

func (*MongoEmulator) RemoveClient

func (emu *MongoEmulator) RemoveClient(clientID string) error

RemoveClient implements RequestHandler. It makes sure that any client-specific state tracked by the emulator or its backend is properly cleaned up when the remote client disconnects.

Directories

Path Synopsis
backend

Jump to

Keyboard shortcuts

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