client

package
Version: v0.0.0-...-aed1ee4 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package client implements a fake client implementation to be used with streaming telemetry collection. It provides a simple Updates queue of data to send it should be used to provide an RPC free test infra for user facing libraries.

Index

Examples

Constants

This section is empty.

Variables

View Source
var New = func(ctx context.Context, _ client.Destination) (client.Impl, error) {
	return &Client{Context: ctx}, nil
}

New can be replaced for any negative testing you would like to do as well.

New exists for compatibility reasons. Most new clients should use Mock. Mock ensures that q.NotificationHandler and ctx aren't forgotten.

Functions

func Mock

func Mock(typ string, updates []interface{})

Mock overrides a client implementation named typ (most implementation libraries have Type constant containing that name) with a fake client sending given updates.

See Client documentation about updates slice contents.

Types

type Block

type Block chan struct{}

Block is a special update that lets the stream of updates to be paused. See Client docs for usage example.

func (Block) Unblock

func (b Block) Unblock()

Unblock unpauses the update stream following the Block. Can only be called once.

type Client

type Client struct {
	Updates      []interface{}
	Handler      client.NotificationHandler
	ProtoHandler client.ProtoHandler
	// BlockAfterSync is deprecated: use Block update as last Updates slice
	// element instead.
	//
	// When BlockAfterSync is set, Client will read from it in Recv after
	// sending all Updates before returning ErrStopReading.
	// BlockAfterSync is closed when Close is called.
	BlockAfterSync chan struct{}

	Context context.Context
	// contains filtered or unexported fields
}

Client is the fake of a client implementation. It will provide a simple list of updates to send to the generic client.

The Updates slice can contain: - client.Notification: passed to query.NotificationHandler - proto.Message: passed to query.ProtoHandler - error: returned from Recv, interrupts the update stream - Block: pauses Recv, proceeds to next update on Unblock

See ExampleClient for sample use case.

Example
block := make(Block)
Mock("fake", []interface{}{
	client.Update{Path: []string{"target", "a", "b"}, Val: 1, TS: time.Now()},
	client.Update{Path: []string{"target", "a", "c"}, Val: 2, TS: time.Now()},
	block,
	client.Delete{Path: []string{"target", "a", "b"}, TS: time.Now()},
	errors.New("unexpected error"),
})

// Unblock the stream after a second.
go func() {
	time.Sleep(time.Second)
	block.Unblock()
}()

err := client.New().Subscribe(context.Background(), client.Query{
	Addrs:   []string{""},
	Queries: []client.Path{{"*"}},
	Type:    client.Once,
	NotificationHandler: func(n client.Notification) error {
		switch nn := n.(type) {
		case client.Connected:
			fmt.Println("connected")
		case client.Sync:
			fmt.Println("sync")
		case client.Update:
			fmt.Printf("%q: %v\n", nn.Path, nn.Val)
		case client.Delete:
			fmt.Printf("%q deleted\n", nn.Path)
		}
		return nil
	},
})
fmt.Println("got error:", err)
Output:

connected
["target" "a" "b"]: 1
["target" "a" "c"]: 2
["target" "a" "b"] deleted
got error: unexpected error

func (*Client) Close

func (c *Client) Close() error

Close is a noop in the fake.

func (*Client) Poll

func (c *Client) Poll() error

Poll is a noop in the fake.

func (*Client) Recv

func (c *Client) Recv() error

Recv will be called for each update the generic client wants to receive.

func (*Client) Reset

func (c *Client) Reset(u []interface{})

Reset will reset the client to start playing new updates.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, q client.Query) error

Subscribe implements the client.Impl interface.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL