Exemplo n.º 1
0
        public void Runs( )
        {
            var edcTenantId = TenantHelper.GetTenantId("EDC", true);

            //static string result = "";
            _result = "";

            using (CountdownEvent evt = new CountdownEvent(1))
            {
                Action <DummyParam> act = (p) =>
                {
                    _result += p.S;
                    // ReSharper disable once AccessToDisposedClosure
                    evt.Signal( );
                };


                var handler = new DummyHandler {
                    Action = act
                };
                var qFactory = new RedisTenantQueueFactory("BackgroundTaskManagerTests " + Guid.NewGuid( ));
                var manager  = new BackgroundTaskManager(qFactory, handlers: handler.ToEnumerable( ))
                {
                    IsActive = true
                };

                try
                {
                    manager.EnqueueTask(edcTenantId, BackgroundTask.Create("DummyHandler", new DummyParam {
                        S = "a"
                    }));
                    Assert.That(_result, Is.Empty);

                    manager.Start( );

                    evt.Wait(DefaultTimeout);
                    evt.Reset( );

                    Assert.That(_result, Is.EqualTo("a"));

                    manager.EnqueueTask(edcTenantId, BackgroundTask.Create("DummyHandler", new DummyParam {
                        S = "b"
                    }));

                    evt.Wait(DefaultTimeout);
                    evt.Reset( );

                    Assert.That(_result, Is.EqualTo("ab"));

                    manager.Stop(5000);

                    manager.EnqueueTask(edcTenantId, BackgroundTask.Create("DummyHandler", new DummyParam {
                        S = "c"
                    }));

                    Assert.That(_result, Is.EqualTo("ab"));                              // c not processed
                }
                finally
                {
                    manager.Stop( );
                    var items = manager.EmptyQueue(edcTenantId);
                    Assert.That(items.Count( ), Is.EqualTo(1));
                }
            }
        }