Пример #1
0
    public async Task SecsGem_SendAsync_And_Return_Secondary_Message()
    {
        var options = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
        });

        using var secsGem1 = new SecsGem(options, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };

        var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
        {
            SecsItem = A("Pong"),
        };

        using var cts = new CancellationTokenSource();
        _             = pipeConnection1.StartAsync(cts.Token);
        _             = pipeConnection2.StartAsync(cts.Token);
        _             = Task.Run(async() =>
        {
            var msg = await secsGem2.GetPrimaryMessageAsync(cts.Token).FirstAsync(cts.Token);
            msg.PrimaryMessage.Should().BeEquivalentTo(ping);
            await msg.TryReplyAsync(pong);
        });

        var reply = await secsGem1.SendAsync(ping, cts.Token);

        reply.Should().NotBeNull().And.BeEquivalentTo(pong);
    }
Пример #2
0
    public void SecsGem_SendAsync_With_Different_Device_Id()
    {
        var options1 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
        });
        var options2 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 1,
        });

        using var secsGem1 = new SecsGem(options1, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options2, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };

        using var cts = new CancellationTokenSource();
        _             = pipeConnection1.StartAsync(cts.Token);
        _             = pipeConnection2.StartAsync(cts.Token);

        var receiver = Substitute.For <Action <SecsMessage> >();

        _ = Task.Run(async() =>
        {
            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cts.Token))
            {
                // can't receive any message, reply S9F1 internally
                receiver(a.PrimaryMessage);
            }
        });

        Func <Task> sendAsync = async() =>
        {
            var reply = await secsGem1.SendAsync(ping, cts.Token);
        };

        sendAsync.Should().ThrowAsync <SecsException>().WithMessage(Resources.S9F1);
        receiver.DidNotReceive();
    }
Пример #3
0
    public void SecsGem_SendAsync_With_T3_Timeout()
    {
        var options1 = Options.Create(new SecsGemOptions
        {
            SocketReceiveBufferSize = 32,
            DeviceId = 0,
            T3       = 500,
        });

        using var secsGem1 = new SecsGem(options1, pipeConnection1, Substitute.For <ISecsGemLogger>());
        using var secsGem2 = new SecsGem(options1, pipeConnection2, Substitute.For <ISecsGemLogger>());

        var ping = new SecsMessage(s: 1, f: 13)
        {
            SecsItem = A("Ping"),
        };
        var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
        {
            SecsItem = A("Pong"),
        };

        using var cts = new CancellationTokenSource();

        _ = Task.Run(async() =>
        {
            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cts.Token))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(options1.Value.T3 + 100)); // process delay over T3
                await a.TryReplyAsync(pong);
            }
        });

        var         receiver  = Substitute.For <Action>();
        Func <Task> sendAsync = async() =>
        {
            var reply = await secsGem1.SendAsync(ping, cts.Token);

            receiver();
        };

        sendAsync.Should().ThrowAsync <SecsException>().WithMessage(Resources.T3Timeout);
        receiver.DidNotReceive();
    }
Пример #4
0
    private void SendAsyncManyMessagesAtOnce(ISecsConnection connection1, ISecsConnection connection2, CancellationToken cancellation)
    {
        var secsGem1 = new SecsGem(optionsActive, connection1, Substitute.For <ISecsGemLogger>());
        var secsGem2 = new SecsGem(optionsPassive, connection2, Substitute.For <ISecsGemLogger>());

        _ = Task.Run(async() =>
        {
            var pong = new SecsMessage(s: 1, f: 14, replyExpected: false)
            {
                SecsItem = A("Pong"),
            };

            await foreach (var a in secsGem2.GetPrimaryMessageAsync(cancellation))
            {
                await a.TryReplyAsync(pong);
            }
        });

        Func <Task> sendAsync = async() =>
        {
            var ping = new SecsMessage(s: 1, f: 13)
            {
                SecsItem = A("Ping"),
            };

            var sendCount  = 10000;
            var totalTasks = new List <Task <SecsMessage> >(capacity: sendCount);
            for (var g = 0; g < sendCount; g++)
            {
                totalTasks.Add(secsGem1.SendAsync(ping, cancellation).AsTask());
            }
            await Task.WhenAll(totalTasks.ToArray());
        };

        sendAsync.Should().NotThrowAsync();
    }