public void TestListenerForProxyingWithSomeRealSocketsWithBrokenConnections()
        {
            //TODO use event to reinstate proxying
            var fakeLogger       = new FakeLogging();
            var fakeDiscoverer   = new FakeDiscoverer();
            var source           = new CancellationTokenSource();
            var fakeSink         = new TestSink(_tag, 1, 242);
            var externalNodeSink = new ProxyNodeSink(_tag, 6000, fakeLogger, source);
            var internalNodeSink = new FakeNodeSink(_tag, 5000, 10, 40);

            using var referenceListener = new ListenerForPeer(externalNodeSink, fakeDiscoverer, source, fakeLogger);
            using var internalListener  = new ListenerForPeer(internalNodeSink, fakeDiscoverer, source, fakeLogger);
            referenceListener.Start();
            internalListener.Start();
            using var internalConnection = new ConnectionToPeer("RequestProxying", internalNodeSink, referenceListener.ExternalAddress, referenceListener.ExternalPortNumber, source, fakeLogger);
            internalConnection.AllocateChannel(internalNodeSink).SendAsync(ProxyNodeSink.ProxyRequest).Wait();
            int retries = 10;

            while (externalNodeSink.ListenerForProxying == null && retries-- > 0)
            {
                WaitForOthers(100);
            }
            var lfp = externalNodeSink.ListenerForProxying;

            Assert.IsNotNull(lfp, "Should have a ListenerForProxying by now");
            lfp.Start();
            WaitForOthers(300);
            internalConnection.SetDefaultSink(fakeSink);
            using var externalConnection = new ConnectionToPeer("ExternalMessage", internalNodeSink, lfp.ExternalAddress, lfp.ExternalPortNumber, source, fakeLogger);
            externalConnection.AllocateChannel(externalNodeSink); // just to bump channel
            var outsideChannel = externalConnection.AllocateChannel(externalNodeSink);

            outsideChannel.SendAsync(new ReadOnlySequence <byte>(new byte[] { _tag, 1, 2 })).Wait();
            retries = 10;
            while (fakeSink.ChannelProcessed == 0 && retries-- > 0)
            {
                WaitForOthers(100);
            }
            AssertHasSameItems <byte>(nameof(fakeSink.BytesProcessed), fakeSink.BytesProcessed.ToArray(), 2);
            Assert.AreEqual(1ul, fakeSink.ChannelProcessed);
            retries = 10;
            while (externalNodeSink.MessagesReceived.Count == 0 && retries-- > 0)
            {
                WaitForOthers(100);
            }
            AssertHasSameItems <byte>(nameof(externalNodeSink.MessagesReceived), externalNodeSink.MessagesReceived.SelectMany(l => l.ToArray()), 242);
            AssertHasLogLine(fakeLogger, "Debug: Sinked Message 'Ag' from Channel ProxyingClient#1@2 using new pair to Proxied Channel 1. Sent: True");
            AssertHasLogLine(fakeLogger, "Debug: Responded with Message '8g' from Channel ListenerClient#1@1 to External Channel 2. Sent: True");
            lfp.Stop();
            retries = 10;
            while (lfp.Alive && retries-- > 0)
            {
                WaitForOthers(100);
            }
            WaitForOthers(300);
            Assert.IsTrue(referenceListener.Alive, "External listener should stay alive after lfp having stopped");
        }
        public void TestProxyListenerCreation()
        {
            var fakeLogger     = new FakeLogging();
            var socketFactory  = new SocketFactory(fakeLogger, 10);
            var fakeDiscoverer = new FakeDiscoverer();
            var source         = new CancellationTokenSource();

            using IPeerServices peerServices = new PeerServices(_messageTag, 0, "UnitTest", "unit", 4096, fakeLogger, fakeDiscoverer, socketFactory, 10, 40);
            peerServices.WithCancellationTokenSource(source);
            Assert.IsNotNull(peerServices);
            Assert.IsNull(fakeLogger.LastLog);
            INodeSink   fakeNodeSink = new FakeNodeSink(_messageTag, 8002, 10, 40);
            IConnection connection   = new ConnectionToPeer("Test", fakeNodeSink, "localhost", 8003, source, fakeLogger);

            using var peerListener = peerServices.ProxyingServices.CreateListenerForProxying("rafael.interlockledger.network", "localhost", 9000, connection);
            Assert.IsNotNull(peerListener);
            Assert.IsNull(fakeLogger.LastLog);
            Assert.AreEqual("rafael.interlockledger.network", peerListener.ExternalAddress);
        }