Exemple #1
0
 public void DispatcherSyncContext_HasStandard()
 {
     using (var x = new ScopedSynchronizationContext(new DispatcherSynchronizationContext()))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.Standard);
     }
 }
 public void ThreadPoolSyncContext_HasNonreentrantPost()
 {
     using (var x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
     }
 }
Exemple #3
0
 public void NullSynchContext_HasNonreentrantPost()
 {
     using (var x = new ScopedSynchronizationContext(null))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
     }
 }
Exemple #4
0
        private void LoadScene()
        {
            this.scene = this.newHostSceneRequest.Scene;

            using (var sync = new ScopedSynchronizationContext(true))
            {
                this.scene.HostInitialise(this);
                sync.Join();

                this.scene.SharedInitialise(this, this.client);
                sync.Join();

                this.scene.InternalHostInitialise();
                sync.Join();
            }

            var initState = this.newHostSceneRequest.InitState;

            this.scene.HostInitialised(initState);

            this.scene.SharedInitialised(initState);

            this.metrics.TickMetricCounters         = this.scene.TickMetricCounters;
            this.metrics.ParallelTickMetricCounters = this.scene.ParallelTickMetricCounters;

            this.newHostSceneRequest = default;
            this.state = HostState.WaitingOnClients;
        }
 public void WindowsFormsSyncContext_HasStandard()
 {
     using (var x = new ScopedSynchronizationContext(new WindowsFormsSynchronizationContext()))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.Standard);
     }
 }
Exemple #6
0
 public void WindowsFormsSyncContext_HasStandard()
 {
     using (var x = new ScopedSynchronizationContext(new WindowsFormsSynchronizationContext()))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.Standard);
     }
 }
 public void DispatcherSyncContext_HasStandard()
 {
     using (var x = new ScopedSynchronizationContext(new DispatcherSynchronizationContext()))
     {
         SynchronizationContextRegister.Verify(SynchronizationContextProperties.Standard);
     }
 }
 public void CurrentSyncContext_WithinNullScopedSyncContext_IsNull()
 {
     using (var x = new ScopedSynchronizationContext(null))
     {
         Assert.IsNull(SynchronizationContext.Current, "ScopedSynchronizationContext did not replace SynchronizationContext.Current");
     }
 }
Exemple #9
0
 public void CurrentSyncContext_WithinNullScopedSyncContext_IsNull()
 {
     using (var x = new ScopedSynchronizationContext(null))
     {
         Assert.IsNull(SynchronizationContext.Current, "ScopedSynchronizationContext did not replace SynchronizationContext.Current");
     }
 }
 public void FailingAction_InvokedThroughThreadPoolGSO_ThrowsTargetInvocationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         test.Invoke((MethodInvoker)(() => { throw new MyException(); }), null);
     }
 }
 public void UnknownGSO_NeverRequiresInvoke()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new MySynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         Assert.IsFalse(test.InvokeRequired, "Unknown GenericSynchronizingObject does require invoke");
     }
 }
Exemple #12
0
 public void GSO_FromThreadPool_DoesNotRequireInvoke()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         Assert.IsFalse(test.InvokeRequired, "GenericSynchronizingObject does require invoke within thread pool");
     }
 }
Exemple #13
0
        public void PreviousSyncContext_WithinScopedSyncContext_IsPreserved()
        {
            var oldSyncContext = SynchronizationContext.Current;

            using (var x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                Assert.AreSame(oldSyncContext, x.PreviousContext, "ScopedSynchronizationContext did not save the old SynchronizationContext");
            }
        }
        public void RegisteredSyncContext_HasRegisteredProperty()
        {
            SynchronizationContextRegister.Register(typeof(MySynchronizationContext), SynchronizationContextProperties.NonReentrantPost);

            using (var x = new ScopedSynchronizationContext(new MySynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
            }
        }
 public void Constructor_SyncContextNotSupportingSynchronized_ThrowsInvalidOperationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
     {
         using (Timer timer = new Timer())
         {
         }
     }
 }
Exemple #16
0
        public void CurrentSyncContext_WithinScopedSyncContext_IsReplaced()
        {
            var newSyncContext = new SynchronizationContext();

            using (var x = new ScopedSynchronizationContext(newSyncContext))
            {
                Assert.AreSame(newSyncContext, SynchronizationContext.Current, "ScopedSynchronizationContext did not replace SynchronizationContext.Current");
            }
        }
        public void PreviousSyncContext_WithinScopedSyncContext_IsPreserved()
        {
            var oldSyncContext = SynchronizationContext.Current;

            using (var x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                Assert.AreSame(oldSyncContext, x.PreviousContext, "ScopedSynchronizationContext did not save the old SynchronizationContext");
            }
        }
Exemple #18
0
        public void RegisteredSyncContext_HasRegisteredProperty()
        {
            SynchronizationContextRegister.Register(typeof(MySynchronizationContext), SynchronizationContextProperties.NonReentrantPost);

            using (var x = new ScopedSynchronizationContext(new MySynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
            }
        }
        public void CurrentSyncContext_WithinScopedSyncContext_IsReplaced()
        {
            var newSyncContext = new SynchronizationContext();

            using (var x = new ScopedSynchronizationContext(newSyncContext))
            {
                Assert.AreSame(newSyncContext, SynchronizationContext.Current, "ScopedSynchronizationContext did not replace SynchronizationContext.Current");
            }
        }
Exemple #20
0
 public void Constructor_SyncContextNotSupportingSynchronized_ThrowsInvalidOperationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
     {
         using (Timer timer = new Timer())
         {
         }
     }
 }
 public void PreviousNullSyncContext_WithinScopedSyncContext_IsPreserved()
 {
     using (var x = new ScopedSynchronizationContext(null))
     {
         using (var y = new ScopedSynchronizationContext(new SynchronizationContext()))
         {
             Assert.IsNull(y.PreviousContext, "ScopedSynchronizationContext did not save the old null SynchronizationContext.Current");
         }
     }
 }
Exemple #22
0
        public void RegisteredSyncContext_ReregisteringProperties_ReplacesOldValue()
        {
            SynchronizationContextRegister.Register(typeof(MyThirdSynchronizationContext), SynchronizationContextProperties.NonReentrantPost);
            SynchronizationContextRegister.Register(typeof(MyThirdSynchronizationContext), SynchronizationContextProperties.NonReentrantSend);

            using (var x = new ScopedSynchronizationContext(new MyThirdSynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantSend);
            }
        }
        public void RegisteredSyncContext_ReregisteringProperties_DoesNotMergeWithOldValue()
        {
            SynchronizationContextRegister.Register(typeof(MyOtherSynchronizationContext), SynchronizationContextProperties.NonReentrantPost);
            SynchronizationContextRegister.Register(typeof(MyOtherSynchronizationContext), SynchronizationContextProperties.NonReentrantSend);

            using (var x = new ScopedSynchronizationContext(new MyOtherSynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
            }
        }
        public void RegisteredSyncContext_ReregisteringProperties_ReplacesOldValue()
        {
            SynchronizationContextRegister.Register(typeof(MyThirdSynchronizationContext), SynchronizationContextProperties.NonReentrantPost);
            SynchronizationContextRegister.Register(typeof(MyThirdSynchronizationContext), SynchronizationContextProperties.NonReentrantSend);

            using (var x = new ScopedSynchronizationContext(new MyThirdSynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantSend);
            }
        }
Exemple #25
0
        public void RegisteredSyncContext_ReregisteringProperties_DoesNotMergeWithOldValue()
        {
            SynchronizationContextRegister.Register(typeof(MyOtherSynchronizationContext), SynchronizationContextProperties.NonReentrantPost);
            SynchronizationContextRegister.Register(typeof(MyOtherSynchronizationContext), SynchronizationContextProperties.NonReentrantSend);

            using (var x = new ScopedSynchronizationContext(new MyOtherSynchronizationContext()))
            {
                SynchronizationContextRegister.Verify(SynchronizationContextProperties.NonReentrantPost);
            }
        }
Exemple #26
0
 public void PreviousNullSyncContext_WithinScopedSyncContext_IsPreserved()
 {
     using (var x = new ScopedSynchronizationContext(null))
     {
         using (var y = new ScopedSynchronizationContext(new SynchronizationContext()))
         {
             Assert.IsNull(y.PreviousContext, "ScopedSynchronizationContext did not save the old null SynchronizationContext.Current");
         }
     }
 }
Exemple #27
0
        public void CurrentSyncContext_AfterScopedSyncContext_IsRestored()
        {
            var oldSyncContext = SynchronizationContext.Current;

            using (var x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
            }

            Assert.AreSame(oldSyncContext, SynchronizationContext.Current, "ScopedSynchronizationContext did not restore the old SynchronizationContext.Current");
        }
        public void CurrentSyncContext_AfterScopedSyncContext_IsRestored()
        {
            var oldSyncContext = SynchronizationContext.Current;

            using (var x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
            }

            Assert.AreSame(oldSyncContext, SynchronizationContext.Current, "ScopedSynchronizationContext did not restore the old SynchronizationContext.Current");
        }
 public void ThreadPoolGSO_FromChildThread_DoesRequireInvoke()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
         using (ActionThread thread = new ActionThread())
         {
             GenericSynchronizingObject test = new GenericSynchronizingObject();
             thread.Start();
             bool invokeRequired = thread.DoGet(() => test.InvokeRequired);
             Assert.IsTrue(invokeRequired, "ThreadPool GenericSynchronizingObject does not require invoke from within a child thread");
         }
 }
        public void ThreadPoolGSOFromNullSyncContext_Invoked_ExecutesSynchronously()
        {
            int threadId = ~Thread.CurrentThread.ManagedThreadId;

            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(null))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();
                test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null);
            }

            Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously");
        }
        public void ThreadPoolGSO_FromThreadPool_DoesNotRequireInvoke()
        {
            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();

                bool invokeRequired = true;
                using (ManualResetEvent evt = new ManualResetEvent(false))
                {
                    ThreadPool.QueueUserWorkItem(_ => { invokeRequired = test.InvokeRequired; evt.Set(); }, null);
                    evt.WaitOne();
                }

                Assert.IsFalse(invokeRequired, "ThreadPool GenericSynchronizingObject does require invoke from within thread pool");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result             = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }
        public void FailingAction_InvokedThroughThreadPoolGSO_PreservesExceptionAsInnerException()
        {
            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();

                Exception errorToThrow     = new MyException();
                Exception innerErrorCaught = null;
                try
                {
                    test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null);
                }
                catch (TargetInvocationException ex)
                {
                    innerErrorCaught = ex.InnerException;
                }

                Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_Runs()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool sawAction = false;
                thread.DoSynchronously(() =>
                {
                    using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                    {
                        GenericSynchronizingObject test = new GenericSynchronizingObject();
                        IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { sawAction = true; }), null);
                        test.EndInvoke(result);
                    }
                });

                Assert.IsTrue(sawAction, "BeginInvoke did not execute action.");
            }
        }
        public void Action_BeginInvokeThroughThreadPoolGSO_RunsOnAThreadPoolThread()
        {
            using (ActionThread thread = new ActionThread())
            {
                thread.Start();

                bool threadPoolThreadIsThreadPoolThread = false;
                thread.DoSynchronously(() =>
                    {
                        using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
                        {
                            GenericSynchronizingObject test = new GenericSynchronizingObject();
                            IAsyncResult result = test.BeginInvoke((MethodInvoker)(() => { threadPoolThreadIsThreadPoolThread = Thread.CurrentThread.IsThreadPoolThread; }), null);
                            test.EndInvoke(result);
                        }
                    });

                Assert.IsTrue(threadPoolThreadIsThreadPoolThread, "ThreadPool thread is not a thread pool thread");
            }
        }
        public void FailingAction_InvokedThroughThreadPoolGSO_PreservesExceptionAsInnerException()
        {
            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();

                Exception errorToThrow = new MyException();
                Exception innerErrorCaught = null;
                try
                {
                    test.Invoke((MethodInvoker)(() => { throw errorToThrow; }), null);
                }
                catch (TargetInvocationException ex)
                {
                    innerErrorCaught = ex.InnerException;
                }

                Assert.AreSame(errorToThrow, innerErrorCaught, "Exception not preserved");
            }
        }
 public void FailingAction_InvokedThroughThreadPoolGSO_ThrowsTargetInvocationException()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         test.Invoke((MethodInvoker)(() => { throw new MyException(); }), null);
     }
 }
 public void GSO_FromThreadPool_DoesNotRequireInvoke()
 {
     using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
     {
         GenericSynchronizingObject test = new GenericSynchronizingObject();
         Assert.IsFalse(test.InvokeRequired, "GenericSynchronizingObject does require invoke within thread pool");
     }
 }
        public void ThreadPoolGSO_Invoked_ExecutesSynchronously()
        {
            int threadId = ~Thread.CurrentThread.ManagedThreadId;

            using (ScopedSynchronizationContext x = new ScopedSynchronizationContext(new SynchronizationContext()))
            {
                GenericSynchronizingObject test = new GenericSynchronizingObject();
                test.Invoke((MethodInvoker)(() => { threadId = Thread.CurrentThread.ManagedThreadId; }), null);
            }

            Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, threadId, "ThreadPool invoke did not operate synchronously");
        }
Exemple #41
0
        private void Run()
        {
            var hostThread = Thread.CurrentThread;

            hostThread.Name    = $"{this.Application.Name} Host Thread";
            Threads.HostThread = hostThread;
            this.tickCounter.Start();

            var runner = this as IRunner <object, object>;

            while (true)
            {
                var state = this.state;

                switch (state)
                {
                case HostState.RunningScene:
                    var tickDuration = runner.Run(null, null);
                    this.Metrics.TickLag = 1d * (tickDuration.TotalMilliseconds / this.tickTime.TotalMilliseconds);
                    continue;

                case HostState.LoadingScene:
                    runner.StopRun();
                    this.LoadScene();
                    continue;

                case HostState.UnloadingScene:
                    runner.StopRun();

                    using (var sync = new ScopedSynchronizationContext(true))
                    {
                        this.scene.HostDispose(this);
                        sync.Join();

                        this.scene.SharedDispose(this, this.client);
                        sync.Join();
                    }

                    this.scene.SharedDisposed();

                    this.dispatcher.ClearChannels();
                    this.scene = null;
                    this.state = HostState.Constructed;
                    continue;

                case HostState.WaitingOnClients:

                    if (this.client.State == ClientState.WaitingOnHost)
                    {
                        this.tick  = 0;
                        this.state = HostState.RunningScene;
                        runner.BeginRun();
                    }
                    else
                    {
                        Thread.Yield();
                    }
                    continue;

                case HostState.Constructed:
                    Thread.Yield();
                    continue;

                case HostState.BeginShutdown:
                    runner.StopRun();
                    this.Dispose();
                    return;

                case HostState.Shutdown:
                    return;
                }
            }
        }