esClient.HTTPClient = &http.Client{ Timeout: 5 * time.Second, } logHandlers = append(logHandlers, levelHandler.New(esHandler.New(&esHandler.Config{ Client: esClient, Prefix: cmd.Name(), BufferSize: 10, }), logLevel)) } ctx = &log.Logger{ Handler: multiHandler.New(logHandlers...), } // Set the API/gRPC logger ttnlog.Set(apex.Wrap(ctx)) grpclog.SetLogger(grpc.Wrap(ttnlog.Get())) ctx.WithFields(log.Fields{ "ComponentID": viper.GetString("id"), "Description": viper.GetString("description"), "Discovery Server Address": viper.GetString("discovery-address"), "Auth Servers": viper.GetStringMapString("auth-servers"), "Monitors": viper.GetStringMapString("monitor-servers"), }).Info("Initializing The Things Network") }, PersistentPostRun: func(cmd *cobra.Command, args []string) { if logFile != nil { logFile.Close() } },
func TestHandlerBrokerCommunication(t *testing.T) { a := New(t) ctx := GetLogger(t, "TestHandlerBrokerCommunication") log.Set(apex.Wrap(ctx)) brk := newTestBroker() rand.Seed(time.Now().UnixNano()) port := rand.Intn(1000) + 10000 go brk.Serve(port) conn, _ := api.Dial(fmt.Sprintf("localhost:%d", port)) { brk.HandlerPublishChanFunc = func(md metadata.MD) (chan *DownlinkMessage, error) { ch := make(chan *DownlinkMessage, 1) go func() { ctx.Info("[SERVER] Channel opened") for message := range ch { ctx.WithField("Message", message).Info("[SERVER] Received Downlink") } ctx.Info("[SERVER] Channel closed") }() return ch, nil } brkClient := NewBrokerClient(conn) downlink := NewMonitoredHandlerPublishStream(brkClient, func() context.Context { return context.Background() }) err := downlink.Send(&DownlinkMessage{ Payload: []byte{1, 2, 3, 4}, }) a.So(err, ShouldBeNil) time.Sleep(10 * time.Millisecond) downlink.Close() time.Sleep(10 * time.Millisecond) } { brk.HandlerSubscribeChanFunc = func(md metadata.MD) (<-chan *DeduplicatedUplinkMessage, func(), error) { ch := make(chan *DeduplicatedUplinkMessage, 1) stop := make(chan struct{}) cancel := func() { ctx.Info("[SERVER] Canceling uplink") close(stop) } go func() { loop: for { select { case <-stop: break loop case <-time.After(5 * time.Millisecond): ctx.Info("[SERVER] Sending Uplink") ch <- &DeduplicatedUplinkMessage{ Payload: []byte{1, 2, 3, 4}, } } } close(ch) ctx.Info("[SERVER] Closed Uplink") }() return ch, cancel, nil } brkClient := NewBrokerClient(conn) uplink := NewMonitoredHandlerSubscribeStream(brkClient, func() context.Context { return context.Background() }) ch := uplink.Channel() go func() { for uplink := range ch { ctx.WithField("Uplink", uplink).Info("[CLIENT] Received Uplink") } ctx.Info("[CLIENT] Closed Uplink") }() time.Sleep(10 * time.Millisecond) uplink.Close() time.Sleep(10 * time.Millisecond) } }
func TestRouterCommunication(t *testing.T) { a := New(t) ctx := GetLogger(t, "TestRouterCommunication") log.Set(apex.Wrap(ctx)) rtr := newTestRouter() rand.Seed(time.Now().UnixNano()) port := rand.Intn(1000) + 10000 go rtr.Serve(port) conn, _ := api.Dial(fmt.Sprintf("localhost:%d", port)) { rtr.UplinkChanFunc = func(md metadata.MD) (chan *UplinkMessage, error) { ch := make(chan *UplinkMessage, 1) go func() { ctx.Info("[SERVER] Channel opened") for message := range ch { ctx.WithField("Message", message).Info("[SERVER] Received Uplink") } ctx.Info("[SERVER] Channel closed") }() return ch, nil } rtrClient := NewRouterClient(conn) gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token") uplink := NewMonitoredUplinkStream(gtwClient) err := uplink.Send(&UplinkMessage{ Payload: []byte{1, 2, 3, 4}, ProtocolMetadata: &protocol.RxMetadata{Protocol: &protocol.RxMetadata_Lorawan{Lorawan: &lorawan.Metadata{ Modulation: lorawan.Modulation_LORA, DataRate: "SF7BW125", CodingRate: "4/7", }}}, GatewayMetadata: &gateway.RxMetadata{ GatewayId: "dev", }, }) a.So(err, ShouldBeNil) time.Sleep(10 * time.Millisecond) uplink.Close() time.Sleep(10 * time.Millisecond) gtwClient.Close() time.Sleep(10 * time.Millisecond) } { rtr.GatewayStatusChanFunc = func(md metadata.MD) (chan *gateway.Status, error) { ch := make(chan *gateway.Status, 1) go func() { ctx.Info("[SERVER] Channel opened") for message := range ch { ctx.WithField("Message", message).Info("[SERVER] Received GatewayStatus") } ctx.Info("[SERVER] Channel closed") }() return ch, nil } rtrClient := NewRouterClient(conn) gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token") status := NewMonitoredGatewayStatusStream(gtwClient) err := status.Send(&gateway.Status{Time: time.Now().UnixNano()}) a.So(err, ShouldBeNil) time.Sleep(10 * time.Millisecond) status.Close() time.Sleep(10 * time.Millisecond) gtwClient.Close() time.Sleep(10 * time.Millisecond) } { rtr.DownlinkChanFunc = func(md metadata.MD) (<-chan *DownlinkMessage, func(), error) { ch := make(chan *DownlinkMessage, 1) stop := make(chan struct{}) cancel := func() { ctx.Info("[SERVER] Canceling downlink") close(stop) } go func() { loop: for { select { case <-stop: break loop case <-time.After(5 * time.Millisecond): ctx.Info("[SERVER] Sending Downlink") ch <- &DownlinkMessage{ Payload: []byte{1, 2, 3, 4}, } } } close(ch) ctx.Info("[SERVER] Closed Downlink") }() return ch, cancel, nil } rtrClient := NewRouterClient(conn) gtwClient := NewRouterClientForGateway(rtrClient, "dev", "token") downlink := NewMonitoredDownlinkStream(gtwClient) ch := downlink.Channel() go func() { for downlink := range ch { ctx.WithField("Downlink", downlink).Info("[CLIENT] Received Downlink") } ctx.Info("[CLIENT] Closed Downlink") }() time.Sleep(10 * time.Millisecond) downlink.Close() time.Sleep(10 * time.Millisecond) gtwClient.Close() time.Sleep(10 * time.Millisecond) } }