コード例 #1
0
        public void TestSetThreadProperites()
        {
            OcConfiguration.SaveInstantiationStackTrace = true;
            OcDispatcher dispatcher = new OcDispatcher(2);

            Assert.AreEqual(dispatcher.GetQueueCount(0), 0);
            Assert.AreEqual(dispatcher.GetQueueCount(), 0);
            Assert.IsTrue(dispatcher.InstantiationStackTrace != null);
            ApartmentState apartmentState = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?  ApartmentState.Unknown : ApartmentState.MTA;

            Assert.AreEqual(dispatcher.NewInvocationBehaviour, NewInvocationBehaviour.Accept);
            Assert.AreEqual(dispatcher.Status, OcDispatcherStatus.ExecutingOrWait);
            Assert.AreEqual(dispatcher.GetThreadApartmentState(), apartmentState);
            Assert.AreEqual(dispatcher.PrioritiesNumber, 2);
            CultureInfo culture = CultureInfo.GetCultureInfo("ru-RU");

            dispatcher.ThreadIsBackground = true;
            dispatcher.ThreadName         = "ThreadName";
            Assert.AreEqual(dispatcher.ToString(), "(ObservableComputations.OcDispatcher (Thread.Name = 'ThreadName'))");
            dispatcher.ThreadPriority = ThreadPriority.Highest;
            int managedThreadId = dispatcher.ManagedThreadId;

            Assert.IsTrue(dispatcher.ThreadState == (ThreadState.WaitSleepJoin | ThreadState.Background) || dispatcher.ThreadState == (ThreadState.Running | ThreadState.Background));

            Assert.AreEqual(dispatcher.ThreadIsBackground, true);
            Assert.AreEqual(dispatcher.ThreadName, "ThreadName");
            Assert.AreEqual(dispatcher.ThreadPriority, ThreadPriority.Highest);
            Assert.AreEqual(dispatcher.ThreadIsAlive, true);
            Assert.AreEqual(dispatcher.ManagedThreadId, managedThreadId);
            Assert.AreEqual(dispatcher.GetThreadApartmentState(), apartmentState);


            dispatcher.Invoke(() =>
            {
                dispatcher.ThreadCurrentCulture   = culture;
                dispatcher.ThreadCurrentUICulture = culture;
                Assert.AreEqual(Thread.CurrentThread.CurrentCulture, culture);
                Assert.AreEqual(Thread.CurrentThread.CurrentUICulture, culture);
                Assert.AreEqual(dispatcher.ThreadCurrentCulture, culture);
                Assert.AreEqual(dispatcher.ThreadCurrentUICulture, culture);
                Assert.AreEqual(Thread.CurrentThread.IsBackground, true);
                Assert.AreEqual(Thread.CurrentThread.Name, "ThreadName");
                Assert.AreEqual(Thread.CurrentThread.Priority, ThreadPriority.Highest);
                Assert.AreEqual(Thread.CurrentThread.IsAlive, true);
                Assert.AreEqual(dispatcher.ThreadExecutionContext, Thread.CurrentThread.ExecutionContext);
                Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, managedThreadId);
                Assert.AreEqual(Thread.CurrentThread.GetApartmentState(), apartmentState);
            });

            ManualResetEventSlim mreDisposed = new ManualResetEventSlim(false);

            dispatcher.PropertyChanged += (sender, args) =>
            {
                if (args.PropertyName == nameof(OcDispatcher.Status) &&
                    dispatcher.Status == OcDispatcherStatus.Disposed)
                {
                    mreDisposed.Set();
                }
            };

            dispatcher.Dispose();
            Assert.AreEqual(dispatcher.NewInvocationBehaviour, NewInvocationBehaviour.Cancel);

            Exception exception = null;

            try
            {
                dispatcher.NewInvocationBehaviour = NewInvocationBehaviour.Accept;
            }
            catch (Exception e)
            {
                exception = e;
            }

            Assert.IsNotNull(exception);

            mreDisposed.Wait();
            Assert.AreEqual(dispatcher.Status, OcDispatcherStatus.Disposed);
        }