コード例 #1
0
        public void TLCFIClientWithAutoreconnect_StartSessionAsync_WillRetryUponFail()
        {
            var sessionManager = Substitute.For <ITLCFIClientSessionManager>();
            var tryCounter     = 0;

            sessionManager.
            When(x => x.GetNewSession(Arg.Any <IPEndPoint>(), Arg.Any <TLCFIClientStateManager>(), Arg.Any <CancellationToken>())).
            Do(c =>
            {
                Task.Delay(20, c.Arg <CancellationToken>());
                tryCounter++;
            });
            sessionManager.GetNewSession(Arg.Any <IPEndPoint>(), Arg.Any <TLCFIClientStateManager>(), Arg.Any <CancellationToken>()).Returns((TLCFIClientSession)null);

            var cts    = new CancellationTokenSource();
            var client = new TLCFIClient(TLCFITestHelpers.GetClientConfig(55000, autoreconnect: true), cts.Token);

            client.OverrideDefaultSessionManager(sessionManager);

            var t1 = Task.Run(async() =>
            {
                await client.StartSessionAsync(cts.Token);
            }, cts.Token);
            var t2 = Task.Delay(50, cts.Token);

            Task.WaitAny(t1, t2);
            cts.Cancel();

            Assert.Greater(tryCounter, 1);
        }
コード例 #2
0
        public ControllerExample(TLCFIClient fiClient)
        {
            _fiClient = fiClient;

            fiClient.DetectorStateChanged        += OnDetectorStateChanged;
            fiClient.StartControlRequestReceived += OnStartControlRequestReceived;
            fiClient.EndControlRequestReceived   += OnEndControlRequestReceived;
            fiClient.GotControl  += OnGotControl;
            fiClient.LostControl += OnLostControl;
            fiClient.IntersectionStateChanged += OnIntersectionStateChanged;
            // There are more events, not used in this example:
            //     fiClient.ClientInitialized += OnClientInitialized;
            //     fiClient.SignalGroupStateChanged += OnSignalGroupStateChanged;
            //     fiClient.InputStateChanged += OnInputStateChanged;
            //     fiClient.OutputStateChanged += OnOutputStateChanged;
        }
コード例 #3
0
        public void TLCFIClientStartSessionAsync_ExceptionInSessionManager_StopsIfFatal()
        {
            var sessionManager = Substitute.For <ITLCFIClientSessionManager>();
            var tryCounter     = 0;

            sessionManager.
            When(x => x.GetNewSession(Arg.Any <IPEndPoint>(), Arg.Any <TLCFIClientStateManager>(), Arg.Any <CancellationToken>())).
            Do(c =>
            {
                Task.Delay(10, c.Arg <CancellationToken>());
                tryCounter++;
            });
            sessionManager.GetNewSession(Arg.Any <IPEndPoint>(), Arg.Any <TLCFIClientStateManager>(), Arg.Any <CancellationToken>()).Returns(new TLCFIClientSession(new TLCFIClientStateManager(), new IPEndPoint(0, 0), CancellationToken.None));

            var initializer = Substitute.For <ITLCFIClientInitializer>();

            initializer.InitializeSession(
                Arg.Any <TLCFIClientSession>(),
                Arg.Any <TLCFIClientConfig>(),
                Arg.Any <TLCFIClientStateManager>(),
                Arg.Any <CancellationToken>()).Returns(x =>
            {
                throw new TLCFISessionException("", true);
            });

            var cts    = new CancellationTokenSource();
            var client = new TLCFIClient(TLCFITestHelpers.GetClientConfig(55000, autoreconnect: true), cts.Token);

            client.OverrideDefaultSessionManager(sessionManager);
            client.OverrideDefaultInitializer(initializer);

            var t1 = Task.Run(async() =>
            {
                await client.StartSessionAsync(cts.Token);
            }, cts.Token);
            var t2 = Task.Delay(50, cts.Token);

            Task.WaitAny(t1, t2);
            cts.Cancel();

            Assert.AreEqual(tryCounter, 1);
        }
コード例 #4
0
        private static void Main(string[] args)
        {
            var mainTokenSource = new CancellationTokenSource();
            var fiConfig        = Generic.XmlFileSerializer.Deserialize <TLCFIClientConfig>(Path.Combine(_curDir, "fiClientConfig.xml"));
            var fiClient        = new TLCFIClient(fiConfig, mainTokenSource.Token);
            var controller      = new ControllerExample(fiClient);

            var tokenSource = new CancellationTokenSource();

            Task.Run(async() => { await fiClient.StartSessionAsync(tokenSource.Token); }, tokenSource.Token);

            while (true)
            {
                var c = Console.ReadLine();
                if (c == "exit")
                {
                    tokenSource.Cancel();
                    return;
                }
            }
        }