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

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



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.




This section is empty.


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.


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.


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.

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()},
	client.Delete{Path: []string{"target", "a", "b"}, TS: time.Now()},
	errors.New("unexpected error"),

// Unblock the stream after a second.
go func() {

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:
		case client.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)

["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