MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / ExampleChannel

Function ExampleChannel

pkg/events/handler_test.go:50–76  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48}
49
50func 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
78func TestChannelReceive(t *testing.T) {
79 t.Parallel()

Callers

nothing calls this directly

Calls 4

ContextFunction · 0.92
SubscribeFunction · 0.92
unsubscribeFunction · 0.85
PrintfMethod · 0.45

Tested by

no test coverage detected