예제 #1
0
        public async Task FallbackFunctionsCanReturnPlainTask()
        {
            int touched = 0;
            await Fallback.ExecuteAsync(
                () => { ++touched; return(Task.CompletedTask); },
                () => { ++touched; return(Task.CompletedTask); });

            Assert.Equal(1, touched);

            await Fallback.ExecuteAsync(
                () => { ++touched; throw new Exception(); },
                () => { ++touched; return(Task.CompletedTask); });

            Assert.Equal(3, touched);

            Try <bool> result = await Fallback.ExecuteAsync(
                () => throw new Exception(),
                () => throw new Exception(),
                () => throw new Exception(),
                () => throw new Exception(),
                () => { ++touched; return(Task.CompletedTask); },
                () => throw new Exception(),
                () => { ++touched; return(Task.CompletedTask); });

            Assert.True(result.Success);
            Assert.True(result.Value);
            Assert.Equal(4, touched);
        }
예제 #2
0
        public async Task FallbackInvokesPrimaryNotSecondary()
        {
            bool      secondary = false;
            Try <int> result    = await Fallback.ExecuteAsync(
                () => Task.FromResult(1),
                () => { secondary = true; return(Task.FromResult(2)); });

            Assert.True(result.Success);
            Assert.Equal(1, result.Value);
            Assert.False(secondary);
        }
예제 #3
0
        Task <ISdkModuleClient> CreateSdkModuleClient(ConnectionStatusChangesHandler statusChangedHandler)
        => this.upstreamProtocol
        .Map(u => this.CreateAndOpenSdkModuleClient(u, statusChangedHandler))
        .GetOrElse(
            async() =>
        {
            // The device SDK doesn't appear to be falling back to WebSocket from TCP,
            // so we'll do it explicitly until we can get the SDK sorted out.
            Try <ISdkModuleClient> result = await Fallback.ExecuteAsync(
                () => this.CreateAndOpenSdkModuleClient(UpstreamProtocol.Amqp, statusChangedHandler),
                () => this.CreateAndOpenSdkModuleClient(UpstreamProtocol.AmqpWs, statusChangedHandler));

            if (!result.Success)
            {
                Events.DeviceConnectionError(result.Exception);
                ExceptionDispatchInfo.Capture(result.Exception).Throw();
            }

            return(result.Value);
        });
예제 #4
0
 internal static Task <Client.ModuleClient> CreateDeviceClientForUpstreamProtocol(
     Option <UpstreamProtocol> upstreamProtocol,
     Func <TransportType, Task <Client.ModuleClient> > deviceClientCreator)
 => upstreamProtocol
 .Map(u => deviceClientCreator(UpstreamProtocolTransportTypeMap[u]))
 .GetOrElse(
     async() =>
 {
     // The device SDK doesn't appear to be falling back to WebSocket from TCP,
     // so we'll do it explicitly until we can get the SDK sorted out.
     Try <Client.ModuleClient> result = await Fallback.ExecuteAsync(
         () => deviceClientCreator(TransportType.Amqp_Tcp_Only),
         () => deviceClientCreator(TransportType.Amqp_WebSocket_Only));
     if (!result.Success)
     {
         Events.DeviceConnectionError(result.Exception);
         throw result.Exception;
     }
     return(result.Value);
 });
예제 #5
0
        internal static Task <Client.ModuleClient> CreateDeviceClientForUpstreamProtocol(
            Option <UpstreamProtocol> upstreamProtocol,
            Func <UpstreamProtocol, Task <Client.ModuleClient> > deviceClientCreator)
        => upstreamProtocol
        .Map(deviceClientCreator)
        .GetOrElse(
            async() =>
        {
            // The device SDK doesn't appear to be falling back to WebSocket from TCP,
            // so we'll do it explicitly until we can get the SDK sorted out.
            Try <Client.ModuleClient> result = await Fallback.ExecuteAsync(
                () => deviceClientCreator(UpstreamProtocol.Amqp),
                () => deviceClientCreator(UpstreamProtocol.AmqpWs));
            if (!result.Success)
            {
                Events.DeviceConnectionError(result.Exception);
                ExceptionDispatchInfo.Capture(result.Exception).Throw();
            }

            return(result.Value);
        });