Ejemplo n.º 1
0
        public void EnqueuingBeforeDequeuing()
        {
            var q         = new SingleActionQueue();
            var didAction = false;

            q.Enqueue(() => didAction = true);
            q.Dequeue()();
            Assert.That(didAction, Is.True);
        }
Ejemplo n.º 2
0
        public void OnlyDequeuesActionOnce()
        {
            var q        = new SingleActionQueue();
            int dequeues = 0;

            Task.Run(() => q.Dequeue()());
            Task.Run(() => q.Dequeue()());
            q.Enqueue(() => Interlocked.Increment(ref dequeues));
            Thread.Sleep(1000);             //Again, sorry :(
            Assert.That(dequeues, Is.EqualTo(1));
        }
Ejemplo n.º 3
0
        public void EnqueuingReplacesActionInQueue()
        {
            var q     = new SingleActionQueue();
            var value = 0;

            q.Enqueue(() => { value = 1; });
            q.Enqueue(() => { value = 2; });
            q.Enqueue(() => { value = 3; });
            q.Dequeue()();
            Assert.That(value, Is.EqualTo(3));
        }
Ejemplo n.º 4
0
        public void DequeuingBeforeEnqueuing()
        {
            var q         = new SingleActionQueue();
            var didAction = false;
            var dequeue   = Task.Run(
                () =>
            {
                q.Dequeue()();
            });

            Thread.Sleep(1000);             //Sorry :( Need to give task time to start and block, to avoid false positives
            q.Enqueue(() => didAction = true);
            dequeue.Wait();
            Assert.That(didAction, Is.True);
        }
Ejemplo n.º 5
0
        public IDisposable Watch(IProject project)
        {
            var queue        = new SingleActionQueue();
            var subscription = project.FilePath
                               .Select(SketchImportUtils.SketchListFilePath)
                               .WhenChangedOrStarted(_fileSystem)
                               .Where(path => File.Exists(path.NativePath))
                               .Select(SketchFilePaths)
                               .CombineLatest(project.RootDirectory, SketchImportUtils.MakeAbsolute)
                               .WhenAnyChangedOrStarted(_fileSystem)
                               .CombineLatest(project.RootDirectory)  // todo redundant as we already combined with the root directory to make paths absolute
                               .Subscribe(
                x => queue.Enqueue(() => Convert(x.Item1, x.Item2 / SketchImportUtils.OutputDirName)),
                e => { _logger.Error("Sketch importer error: " + e.Message); });

            var tokenSource = new CancellationTokenSource();

            new Thread(
                () =>
            {
                while (!tokenSource.Token.IsCancellationRequested)
                {
                    queue.Dequeue()();
                }
                _logger.Info("Stopping thread");
            })
            {
                Name = "Sketch conversion", IsBackground = true
            }.Start();

            Action stopThread = () =>
            {
                queue.Enqueue(() => { tokenSource.Cancel(); });
            };

            // TODO: This is very messy, should extract dialog to its own class
            CreateDialog(project);

            return(Disposable.Combine(
                       subscription,
                       Disposable.Create(stopThread)));
        }