public void 到達保証あり逆順受信テスト() { var ctx = new TestChannelContext(); var config = new ReliableChannelConfig { Timeout = TimeSpan.FromSeconds(60), MaxWindowSize = 1000, Ordered = false }; using (var control = new ReliableFlowControl(1, 1, ctx, config)) { List <byte[]> buf = new List <byte[]>(); short id = 1; for (int i = 0; i < 100; i++) { buf.Add(GetRandam(200 + i * 100)); control.Send(GetFragments(buf[buf.Count - 1], id++, Fragment.Size)); } buf.Reverse(); ctx.Reverse(); int count = 0; while (count < buf.Count && ctx.Receive(control, out var output)) { var ret = output.ToBytes(); Assert.IsTrue(Equals(buf[count++], ret), "逆順に届く"); output.RemoveRef(true); } Assert.AreEqual(buf.Count, count, "同じ数読み取れている"); } }
public bool Receive(ReliableFlowControl control, out List <Fragment> output, double loss = 0) { int sendCount = 0; int lossCount = 0; output = new List <Fragment>(); while (SentQueue.Count > 0) { var receive = SentQueue.Dequeue(); sendCount++; if (m_Random.NextDouble() < loss) { lossCount++; if (SentQueue.Count == 0) { control.Update(TimeSpan.FromMilliseconds(100)); } continue; } int offset = 0; if (ReliableAckData.TryUnpack(receive, ref offset, out var ack)) { control.ReceiveAck(ack); if (SentQueue.Count == 0) { control.Update(TimeSpan.FromMilliseconds(100)); } continue; } offset = 0; if (!ReliableData.TryUnpack(receive, ref offset, out var data)) { Assert.Fail(); } if (control.Enqueue(data)) { control.Update(TimeSpan.FromMilliseconds(100)); } if (control.TryDequeue(output)) { return(true); } } return(false); }
public void 到達保証ありチャンネルパケロステスト() { foreach (var ordered in new bool[] { true, false }) { var ctx = new TestChannelContext(); var config = new ReliableChannelConfig { Ordered = ordered }; using (var control = new ReliableFlowControl(1, 1, ctx, config)) { List <byte[]> buf = new List <byte[]>(); short id = 1; for (int i = 0; i < 10; i++) { buf.Add(GetRandam(200 + i * 100)); control.Send(GetFragments(buf[buf.Count - 1], id++, Fragment.Size)); } for (int i = 0; i < 50; i++) { buf.Add(GetRandam(2000 + (int)(10000 * m_Random.NextDouble()))); control.Send(GetFragments(buf[buf.Count - 1], id++, Fragment.Size)); } ctx.ShuffleSentQueue(); int count = 0; while (count < buf.Count && ctx.Receive(control, out var output, 0.3)) { ctx.ShuffleSentQueue(); var ret = output.ToBytes(); if (ordered) { Assert.IsTrue(Equals(buf[count++], ret), "順番に届く"); } else { count++; } output.RemoveRef(true); } Assert.AreEqual(buf.Count, count, "同じ数読み取れている"); } } }
public void 到達保証ありチャンネル機能テスト() { var ctx = new TestChannelContext(); using (var control = new ReliableFlowControl(1, 1, ctx)) { var buf = GetRandam(1000); short id = 1; foreach (var size in new int[] { Fragment.Size, 100, 10 }) { var fragments = GetFragments(buf, id++, size); control.Send(fragments); Assert.IsTrue(ctx.Receive(control, out var output), "受信データをパース出来なかった"); Assert.IsTrue(Equals(buf, output.ToBytes()), "データを受け取れている"); output.RemoveRef(true); } } }
public void 到達保証ありエラー機能テスト() { var ctx = new TestChannelContext(); using (var control = new ReliableFlowControl(1, 1, ctx)) { var buf = GetRandam(1000); short id = 1; var fragments = GetFragments(buf, id++, Fragment.Size); control.Send(fragments); control.Update(TimeSpan.FromSeconds(1)); try { control.Update(TimeSpan.FromSeconds(10)); Assert.IsTrue(false, "Ackのタイムアウトで切断される"); } catch (Exception ex) { Assert.AreEqual(ReliableFlowControl.AckTimeoutError, ex.Message); } } }