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 ¶
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 ¶
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.
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