//--- Methods ---
        protected override Yield Start(XDoc config, Result result) {
            yield return Coroutine.Invoke(base.Start, config, new Result());
            _varnish = Plug.New(Config["uri.varnish"].AsUri);
            _deki = Plug.New(Config["uri.deki"].AsUri);
            _apikey = Config["apikey"].AsText;
            _delayPurgeTimespan = TimeSpan.FromSeconds(config["varnish-purge-delay"].AsInt ?? 10);
            var dispatcher = new UpdateRecordDispatcher(OnQueueExpire);
            _updateDelayQueue = new UpdateDelayQueue(_delayPurgeTimespan, dispatcher);

            // set up subscription for pubsub
            XDoc subscriptionSet = new XDoc("subscription-set")
                .Elem("uri.owner", Self.Uri)
                .Start("subscription")
                    .Add(DreamCookie.NewSetCookie("service-key", InternalAccessKey, Self.Uri).AsSetCookieDocument)
                    .Elem("channel", "event://*/deki/pages/create")
                    .Elem("channel", "event://*/deki/pages/move")
                    .Elem("channel", "event://*/deki/pages/update")
                    .Elem("channel", "event://*/deki/pages/delete")
                    .Elem("channel", "event://*/deki/pages/revert")
                    .Elem("channel", "event://*/deki/pages/createalias")
                    .Elem("channel", "event://*/deki/pages/tags/update")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/comments/create")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/comments/update")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/comments/delete")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/files/create")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/files/update")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/files/delete")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/files/move")
                    .Elem("channel", "event://*/deki/pages/dependentschanged/files/restore")
                    .Elem("channel", "event://*/deki/files/create")
                    .Elem("channel", "event://*/deki/files/update")
                    .Elem("channel", "event://*/deki/files/delete")
                    .Elem("channel", "event://*/deki/files/move")
                    .Elem("channel", "event://*/deki/files/restore")
                    .Start("recipient")
                        .Attr("authtoken", _apikey)
                        .Elem("uri", Self.Uri.At("queue"))
                    .End()
                .End();
            Result<DreamMessage> subscriptionResult;
            yield return subscriptionResult = PubSub.At("subscribers").PostAsync(subscriptionSet);
            string accessKey = subscriptionResult.Value.ToDocument()["access-key"].AsText;
            XUri location = subscriptionResult.Value.Headers.Location;
            Cookies.Update(DreamCookie.NewSetCookie("access-key", accessKey, location), null);
            _subscriptionLocation = location.AsLocalUri().WithoutQuery();
            _log.DebugFormat("subscribed VarnishPurgeService for events at {0}", _subscriptionLocation);
            result.Return();
        }
        public void Callback_in_delay_time() {
            var dispatcher = new MockUpdateRecordDispatcher(1);
            var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(1), dispatcher);
            XDoc queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/update")
                .Elem("pageid", "1")
                .Elem("path", "bar");

            DateTime queueTime = DateTime.Now;
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);
            if(!dispatcher.ResetEvent.WaitOne(5000, true)) {
                Assert.Fail("callback didn't happen");
            }
            Assert.AreEqual(1, dispatcher.Dispatches.Count);
            Assert.AreEqual(1, dispatcher.Dispatches[0].Item2.Id);
            Assert.AreEqual("abc", dispatcher.Dispatches[0].Item2.WikiId);
            Assert.AreEqual("bar", dispatcher.Dispatches[0].Item2.Path);
            Assert.AreEqual(RecordType.Page, dispatcher.Dispatches[0].Item2.Type);
            Assert.GreaterOrEqual(dispatcher.Dispatches[0].Item1, queueTime.Add(TimeSpan.FromMilliseconds(1000)));
        }
        public void Multiple_events_for_same_page_fire_once_on_first_plus_delay() {
            var dispatcher = new MockUpdateRecordDispatcher(1);
            var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(2), dispatcher);
            XDoc queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/create")
                .Elem("pageid", "1")
                .Elem("path", "bar");
            DateTime queueTime = DateTime.Now;
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            Thread.Sleep(300);
            queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/update")
                .Elem("pageid", "1")
                .Elem("path", "bar");
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            Thread.Sleep(300);
            queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/foop")
                .Elem("pageid", "1")
                .Elem("path", "bar");
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            if(!dispatcher.ResetEvent.WaitOne(3000, true)) {
                Assert.Fail("callback didn't happen");
            }
            Assert.AreEqual(1, dispatcher.Dispatches.Count);
            Assert.AreEqual(1, dispatcher.Dispatches[0].Item2.Id);
            Assert.AreEqual("abc", dispatcher.Dispatches[0].Item2.WikiId);
            Assert.AreEqual("bar", dispatcher.Dispatches[0].Item2.Path);
            Assert.AreEqual(RecordType.Page, dispatcher.Dispatches[0].Item2.Type);
            Assert.GreaterOrEqual(dispatcher.Dispatches[0].Item1, queueTime.Add(TimeSpan.FromMilliseconds(2000)));
            Assert.LessOrEqual(dispatcher.Dispatches[0].Item1, queueTime.Add(TimeSpan.FromMilliseconds(2200)));
            Thread.Sleep(1000);
            Assert.AreEqual(1, dispatcher.Dispatches.Count);

        }
 public void QueueSize_queries_dispatcher_QueueSize() {
     var dispatcher = new MockUpdateRecordDispatcher(1);
     var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(1), dispatcher);
     Assert.AreEqual(1, queue.QueueSize);
     Assert.AreEqual(1, dispatcher.QueueSizeCalled);
 }
 public void Can_dispatch_using_coroutine_dispatcher() {
     var helper = new CallbackHelper();
     var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(1), new UpdateRecordDispatcher(helper.Invoke));
     var queued = new XDoc("deki-event")
         .Attr("wikiid", "abc")
         .Elem("channel", "event://abc/deki/pages/update")
         .Elem("pageid", "1")
         .Elem("path", "bar");
     queue.Enqueue(queued);
     Assert.AreEqual(0, helper.Callbacks.Count);
     if(!helper.ResetEvent.WaitOne(2000, true)) {
         Assert.Fail("callback didn't happen");
     }
     Assert.AreEqual(1, helper.Callbacks.Count);
     Assert.AreEqual(1, helper.Callbacks[0].Id);
     Assert.AreEqual("abc", helper.Callbacks[0].WikiId);
     Assert.AreEqual("bar", helper.Callbacks[0].Path);
     Assert.AreEqual(RecordType.Page, helper.Callbacks[0].Type);
 }
        public void Multiple_events_for_same_page_update_path_in_dispatched_callback() {
            var dispatcher = new MockUpdateRecordDispatcher(1);
            var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(1), dispatcher);
            XDoc queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/create")
                .Elem("pageid", "1")
                .Elem("path", "bar");
            DateTime queueTime = DateTime.Now;
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            Thread.Sleep(100);
            queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/update")
                .Elem("pageid", "1")
                .Elem("path", "baz");
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            if(!dispatcher.ResetEvent.WaitOne(2000, true)) {
                Assert.Fail("callbacks didn't happen");
            }
            Assert.AreEqual(1, dispatcher.Dispatches.Count);
            Assert.AreEqual(1, dispatcher.Dispatches[0].Item2.Id);
            Assert.AreEqual("abc", dispatcher.Dispatches[0].Item2.WikiId);
            Assert.AreEqual("baz", dispatcher.Dispatches[0].Item2.Path);
            Assert.AreEqual(RecordType.Page, dispatcher.Dispatches[0].Item2.Type);
        }
        public void Multiple_events_for_different_types_fire_for_each_type() {
            var dispatcher = new MockUpdateRecordDispatcher(3);
            var queue = new UpdateDelayQueue(TimeSpan.FromSeconds(1), dispatcher);
            XDoc queued = new XDoc("deki-event")
                .Attr("wikiid", "abc")
                .Elem("channel", "event://abc/deki/pages/create")
                .Elem("pageid", "1")
                .Elem("path", "bar");
            DateTime queueTime = DateTime.Now;
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            Thread.Sleep(100);
            queued = new XDoc("deki-event")
                .Attr("wikiid", "abd")
                .Elem("channel", "event://abc/deki/pages/update")
                .Elem("pageid", "2")
                .Elem("path", "baz");
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            Thread.Sleep(100);
            queued = new XDoc("deki-event")
                .Attr("wikiid", "abf")
                .Elem("channel", "event://abc/deki/files/foop")
                .Elem("fileid", "1");
            queue.Enqueue(queued);
            Assert.AreEqual(0, dispatcher.Dispatches.Count);

            if(!dispatcher.ResetEvent.WaitOne(2000, true)) {
                Assert.Fail("callbacks didn't happen");
            }
            Assert.AreEqual(3, dispatcher.Dispatches.Count);
            Assert.AreEqual(1, dispatcher.Dispatches[0].Item2.Id);
            Assert.AreEqual("abc", dispatcher.Dispatches[0].Item2.WikiId);
            Assert.AreEqual("bar", dispatcher.Dispatches[0].Item2.Path);
            Assert.AreEqual(RecordType.Page, dispatcher.Dispatches[0].Item2.Type);
            Assert.AreEqual(2, dispatcher.Dispatches[1].Item2.Id);
            Assert.AreEqual("abd", dispatcher.Dispatches[1].Item2.WikiId);
            Assert.AreEqual("baz", dispatcher.Dispatches[1].Item2.Path);
            Assert.AreEqual(RecordType.Page, dispatcher.Dispatches[1].Item2.Type);
            Assert.AreEqual(1, dispatcher.Dispatches[2].Item2.Id);
            Assert.AreEqual("abf", dispatcher.Dispatches[2].Item2.WikiId);
            Assert.IsTrue(string.IsNullOrEmpty(dispatcher.Dispatches[2].Item2.Path));
            Assert.AreEqual(RecordType.File, dispatcher.Dispatches[2].Item2.Type);

        }
 protected override Yield Stop(Result result) {
     _updateDelayQueue.Cleanup();
     _updateDelayQueue = null;
     yield return Plug.New(_subscriptionLocation).DeleteAsync().Catch();
     yield return Coroutine.Invoke(base.Stop, new Result());
     result.Return();
 }