()
| 48 | } |
| 49 | |
| 50 | func ExampleChannel() { |
| 51 | // The context typically comes from the request or something. |
| 52 | ctx := test.Context() |
| 53 | |
| 54 | eventChan := make(events.Channel, 2) |
| 55 | |
| 56 | subCtx, unsubscribe := context.WithCancel(ctx) |
| 57 | if err := events.Subscribe(subCtx, []string{"example"}, nil, eventChan); err != nil { |
| 58 | panic(err) |
| 59 | } |
| 60 | |
| 61 | // From this moment on, "example" events will be delivered to the channel. |
| 62 | // As soon as the channel is full, events will be dropped, so it's probably a |
| 63 | // good idea to start handling the channel before subscribing. |
| 64 | |
| 65 | go func() { |
| 66 | for e := range eventChan { |
| 67 | fmt.Printf("Received event %v\n", e) |
| 68 | } |
| 69 | }() |
| 70 | |
| 71 | // We want to unsubscribe when this function returns. |
| 72 | defer unsubscribe() |
| 73 | |
| 74 | // Note that in-transit events may still be delivered after Unsubscribe returns. |
| 75 | // This means that you can't immediately close the channel after unsubscribing. |
| 76 | } |
| 77 | |
| 78 | func TestChannelReceive(t *testing.T) { |
| 79 | t.Parallel() |
nothing calls this directly
no test coverage detected