Beispiel #1
0
        public async void Test()
        {
            var simulator   = new ProtocolSimulator(); //不丢失;
            var disposables = new CompositeDisposable();
            var msg         = new List <string>();
            var rst         = new List <CommContext>();

            var sendList = new List <CommContext>
            {
                new("AA01BB", "AA$1BB", 1000, "01"),
                new("AB02BB", "AB$1BB", 1000, "02"),
                new("AC03BB", "AC$1BB", 1000, "03"),
                new("AD04BB", "AD$1BB", 1000, "04"),
                new("AE05BB", "AE$1BB", 1000, "05"),
                new("AF06BB", "AF$1BB", 1000, "06")
            };

            simulator.History.Subscribe(p =>
            {
                msg.Add($"History@ {DateTime.Now:mm:ss fff} {p}");
                rst.Add(p.DeepClone());
            }).DisposeWith(disposables);

            simulator.Received
            .Delay(TimeSpan.FromMilliseconds(100))     //美观
            .Select(p => $"Received@ {DateTime.Now:mm:ss fff} 接收 : {p.ByteToHex()}")
            .Subscribe(p => msg.Add(p))
            .DisposeWith(disposables);

            await simulator.OpenAsync();

            await simulator.SendAsync(new CommContext("000000"));

            var loop = 2;

            for (var i = 0; i < loop; i++)
            {
                foreach (var context in sendList)
                {
                    await simulator.SendAsync(context);
                }
            }

            await simulator.SendAsync(new CommContext("BB06BB", "CC$1BB", 1000, "06"));

            await Task.Delay(1000);

            await simulator.CloseAsync();

            simulator.Dispose();
            disposables.Dispose();

            msg.Count.Should().Be(2 + 2 * sendList.Count * loop + 2);
            rst.Count(p => p.State == CommContextState.Success).Should().Be(rst.Count - 1 - 1);
            rst.Count(p => p.State == CommContextState.NoNeed).Should().Be(1);
            rst.Count(p => p.State == CommContextState.Timeout).Should().Be(1);
        }
Beispiel #2
0
        public async Task WaitingSendResultTest()
        {
            var simulator   = new ProtocolSimulator(0, 3000);
            var disposables = new CompositeDisposable();
            var msg         = new List <string>();
            var rst         = new List <CommContext>();

            simulator.Received.Select(p => $"@ {DateTime.Now:mm:ss fff} 接收 : {p.ByteToHex()}")
            .Subscribe(p => msg.Add(p))
            .DisposeWith(disposables);
            simulator.History.Subscribe(p =>
            {
                msg.Add($"@ {DateTime.Now:mm:ss fff} {p}");
                rst.Add(p);
            })
            .DisposeWith(disposables);

            await simulator.OpenAsync();

            //有返回值
            var ctx1 = new CommTaskContext("1", "AA01BB", "AA$1BB", 1000 + 3000);

            var begin = DateTime.Now;

            _ = simulator.SendAsync(ctx1);
            await simulator.WaitingSendResult(ctx1, 1000 + 3000);

            var end = DateTime.Now;

            ctx1.Response.Value.Should().Be("AA01BB");
            (end - begin).TotalMilliseconds.Should().BeApproximately(3500, 500);
            rst.TrueForAll(p => p.State == CommContextState.Success);

            rst.Clear();

            //无返回值
            var ctx2 = new CommTaskContext("2", "000000");

            _ = simulator.SendAsync(ctx2);
            await simulator.WaitingSendResult(ctx2, 3000 + 3000);

            ctx2.State.Should().Be(CommContextState.NoNeed);

            await simulator.CloseAsync();

            simulator.Dispose();
            disposables.Dispose();
        }
Beispiel #3
0
        public async Task ExecQueueTest()
        {
            var simulator   = new ProtocolSimulator();
            var disposables = new CompositeDisposable();
            var msg         = new List <string>();
            var history     = new List <CommTaskContext>();

            simulator.Received.Select(p => $"@ {DateTime.Now:mm:ss fff} 接收 : {p.ByteToHex()}").Subscribe(p =>
            {
                msg.Add(p);
            })
            .DisposeWith(disposables);
            simulator.History.Subscribe(p =>
            {
                msg.Add($"@ {DateTime.Now:mm:ss fff} {p}");
                history.Add(p as CommTaskContext);
            }).DisposeWith(disposables);

            await simulator.OpenAsync();

            var tcList = new List <CommTaskContext>
            {
                new("id1", "AA01BB", "AA$1BB", 1000, 3),
                new("id2", "AB01BB", "AB$1BB", 1000, 3),
                new("id3", "AC01BB", "AA$1BB", 1000, 3), //失败重试
                new("id4", "AD01BB", 3),                 //无返回值
                new("id5", "AE01BB", "AE$1BB", 1000, 3)
            };

            await simulator.ExecQueue(tcList, CancellationToken.None, 2000);

            await simulator.CloseAsync();

            simulator.Dispose();
            disposables.Dispose();

            tcList.Count(p => p.State == CommContextState.Timeout).Should().Be(1);
            tcList.Count(p => p.State == CommContextState.NoNeed).Should().Be(1);
            tcList.Count(p => p.State == CommContextState.Success).Should().Be(3);

            history.Count(p => p.State == CommContextState.NoNeed).Should().Be(1);
            history.Count(p => p.State == CommContextState.Success).Should().Be(3);
            history.Count(p => p.State == CommContextState.Timeout).Should().Be(3);
        }