()
| 107 | } |
| 108 | |
| 109 | func Example() { |
| 110 | // The context typically comes from the request or something. |
| 111 | ctx := test.Context() |
| 112 | |
| 113 | // This is required for unit test to pass. |
| 114 | defer test.SetDefaultEventsPubSub(basic.NewPubSub())() //nolint:revive |
| 115 | |
| 116 | // The WaitGroup is only for synchronizing the unit test |
| 117 | var wg sync.WaitGroup |
| 118 | wg.Add(1) |
| 119 | |
| 120 | if err := events.Subscribe(ctx, []string{"ns.mac.adr.send_req"}, nil, events.HandlerFunc(func(e events.Event) { |
| 121 | fmt.Printf("Received event %s\n", e.Name()) |
| 122 | |
| 123 | wg.Done() // only for synchronizing the unit test |
| 124 | })); err != nil { |
| 125 | panic(err) |
| 126 | } |
| 127 | |
| 128 | // You can send any arbitrary event; you don't have to pass any identifiers or data. |
| 129 | events.Publish(events.New(test.Context(), "test.hello_world", "the events system says hello, world")) |
| 130 | |
| 131 | // Defining the event is not mandatory, but will be needed in order to translate the descriptions. |
| 132 | // Event names are lowercase snake_case and can be dot-separated as component.subsystem.subsystem.event |
| 133 | // Event descriptions are short descriptions of what the event means. |
| 134 | // Visibility rights are optional. If no rights are supplied, then the _ALL right is assumed. |
| 135 | adrSendEvent := events.Define( |
| 136 | "ns.mac.adr.send_req", "send ADR request", |
| 137 | events.WithVisibility(ttnpb.Right_RIGHT_APPLICATION_TRAFFIC_READ), |
| 138 | ) |
| 139 | |
| 140 | // These variables come from the request or you got them from the db or something. |
| 141 | var ( |
| 142 | dev ttnpb.EndDevice |
| 143 | requests []ttnpb.MACCommand_LinkADRReq |
| 144 | ) |
| 145 | |
| 146 | // It's nice to be able to correlate events; we use a Correlation ID for that. |
| 147 | // In most cases, there will already be a correlation ID in the context; |
| 148 | // this function will append a new one to the ones already in the context. |
| 149 | ctx = events.ContextWithCorrelationID(ctx, events.NewCorrelationID()) |
| 150 | |
| 151 | // Publishing an event to the events package will dispatch it on the "global" event pubsub. |
| 152 | events.Publish(adrSendEvent.NewWithIdentifiersAndData(ctx, dev.Ids, requests)) |
| 153 | |
| 154 | wg.Wait() // only for synchronizing the unit test |
| 155 | |
| 156 | // Output: |
| 157 | // Received event ns.mac.adr.send_req |
| 158 | } |
nothing calls this directly
no test coverage detected