Esempio n. 1
0
        public void TestCreateSource()
        {
            var idleInvoked = false;

            using (var source = Idle.CreateSource()) {
                Assert.That(source, Is.Not.Null);


                using (var context = new MainContext())
                    using (var mainLoop = new MainLoop(context)) {
                        source.SetCallback(() => {
                            mainLoop.Quit();
                            idleInvoked = true;
                            return(Source.Remove_);
                        });
                        source.Attach(context);

                        Task.Run(() => {
                            context.PushThreadDefault();
                            mainLoop.Run();
                            context.PopThreadDefault();
                        }).Wait(100);
                    }

                source.Destroy();
            }

            Assert.That(idleInvoked, Is.True);

            Utility.AssertNoGLibLog();
        }
Esempio n. 2
0
        public void TestCurrent()
        {
            lock (MainContextTests.MainContextLock) {
                // there is no main loop running, so there should be no current source
                Assert.That(Source.Current, Is.Null);

                // if we are in a callback, there should be a source.
                var callbackInvoked = false;
                Task.Run(() => {
                    var context = new MainContext();
                    context.PushThreadDefault();
                    var mainLoop = new MainLoop(context);
                    var source   = Idle.CreateSource();
                    source.SetCallback(() => {
                        try {
                            Assert.That(Source.Current.Handle, Is.EqualTo(source.Handle));
                            callbackInvoked = true;
                            return(Source.Remove_);
                        } finally {
                            mainLoop.Quit();
                        }
                    });
                    source.Attach(context);
                    mainLoop.Run();
                    context.PopThreadDefault();
                }).Wait(1000);
                Assert.That(callbackInvoked, Is.True);
            }

            Utility.AssertNoGLibLog();
        }
Esempio n. 3
0
        public void TestSyncronizationContextPost()
        {
            var invokedOnMainThread = false;

            var context  = new MainContext();
            var mainLoop = new MainLoop(context);
            // use Idle.CreateSource to run stuff on the same thread as
            // the main loop after mainLoop.Run has been called.
            var source = Idle.CreateSource();

            source.SetCallback(() => {
                // this gets the MainLoopSyncronizationContext that was
                // set when mainLoop.Run was called. If it wasn't set, this
                // will throw an exception.
                var x2        = SynchronizationContext.Current;
                var scheduler = TaskScheduler.FromCurrentSynchronizationContext();

                var mainLoopThread = Thread.CurrentThread;

                // start another background thread to that we can try calling back
                // to the mainLoop thread using the scheduler.
                // This implicitly calls MainLoopSyncronzationContext.Post()
                Task.Run(() => {
                    Assume.That(mainLoopThread, Is.Not.EqualTo(Thread.CurrentThread));
                    Task.Factory.StartNew(() => {
                        mainLoop.Quit();
                        // NUnit does not catch the error here since it is on another thread.
                        // But this is OK, we just check invokedOnMainThread later.
                        Assert.That(mainLoopThread, Is.EqualTo(Thread.CurrentThread));
                        invokedOnMainThread = true;
                    },
                                          CancellationToken.None,
                                          TaskCreationOptions.None,
                                          scheduler);
                });
                return(Source.Remove_);
            });
            source.Attach(context);

            var task = Task.Run(() => {
                context.PushThreadDefault();
                mainLoop.Run();
                context.PopThreadDefault();
            });

            task.ConfigureAwait(false);
            var x = SynchronizationContext.Current;

            task.Wait(100);
            source.Destroy();

            Assert.That(invokedOnMainThread, Is.True);

            Utility.AssertNoGLibLog();
        }
Esempio n. 4
0
        public void TestFindSourceById()
        {
            using (var context = new MainContext()) {
                var source      = Idle.CreateSource();
                var id          = source.Attach(context);
                var foundSource = context.FindSourceById(id);
                Assert.That(foundSource.Handle, Is.EqualTo(source.Handle));
            }

            Utility.AssertNoGLibLog();
        }