protected Action<object> SetupProxy(Type eventType, IEnumerable<Type> constraintHandlerTypes = null)
        {
            ids = new ConcurrentBag<string>();
            events = new ConcurrentBag<object>();
            typeName = eventType.FullName;
            typeNames = new[] { typeName }.Select(t => new EventType { Type = t }).ToList();

            Action<object> handler = null;
            WhenCalling<ITypeFinder>(x => x.ListEventTypes()).Return(new[] { eventType });
            WhenCalling<ITypeFinder>(x => x.GetEventType(Arg<string>.Is.Anything)).Return(eventType);
            if (constraintHandlerTypes != null)
                constraintHandlerTypes.ForEach(t => Register(Activator.CreateInstance((Type) t)));
            WhenCalling<ITypeFinder>(x => x.GetConstraintHandlerTypes(Arg<Type>.Is.Anything))
                    .Return(constraintHandlerTypes != null ? constraintHandlerTypes : Enumerable.Empty<Type>());

            WhenCalling<IEventAggregator>(x => x.Subscribe(Arg<Action<object>>.Is.Anything)).Callback<Action<object>>(h =>
            {
                handler = h;
                return true;
            });
            WhenAccessing<IRequest, IPrincipal>(x => x.User).Return(Thread.CurrentPrincipal);

            var client = new Client(events);

            WhenCalling<IHubConnectionContext<dynamic>>(x => x.Client(Arg<string>.Is.Anything)).Return(client);
            WhenAccessing<IHubContext, IHubConnectionContext<dynamic>>(x => x.Clients).Return(Get<IHubConnectionContext<dynamic>>());
            Mock<IConnectionManager>();
            WhenCalling<IConnectionManager>(x => x.GetHubContext<EventAggregatorProxyHub>()).Return(Get<IHubContext>());

            eventProxy = new EventProxy();

            return handler;
        }
 static EventAggregatorProxyHub()
 {
     eventProxy = new EventProxy();
     try
     {
         commandProxy = GlobalHost.DependencyResolver.Resolve<CommandProxy>();
     }
     catch
     {
     }
     commandProxy = commandProxy ?? new CommandProxy();
 }
        public void Context()
        {
            ids = new ConcurrentBag<string>();
            events = new ConcurrentBag<object>();
            reset = new AutoResetEvent(false);
            var running = true;
            var count = 0;

            var type = typeof (MembersEvent);
            var typeName = type.FullName;
            var typeNames = new[] {typeName}.Select(t => new EventType { Type = t }).ToList();

            Action<object> handler = null;
            WhenCalling<ITypeFinder>(x => x.ListEventTypes()).Return(new[] {type});
            WhenCalling<ITypeFinder>(x => x.GetEventType(Arg<string>.Is.Anything)).Return(type);
            WhenCalling<IEventAggregator>(x => x.Subscribe(Arg<Action<object>>.Is.Anything)).Callback<Action<object>>(h =>
            {
                handler = h;
                return true;
            });
            WhenAccessing<IRequest, IPrincipal>(x => x.User).Return(Thread.CurrentPrincipal);

            var client = new Client(events);

            WhenCalling<IHubConnectionContext>(x => x.Client(Arg<string>.Is.Anything)).Return(client);
            WhenAccessing<IHubContext, IHubConnectionContext>(x => x.Clients).Return(Get<IHubConnectionContext>());
            Mock<IConnectionManager>();
            WhenCalling<IConnectionManager>(x => x.GetHubContext<EventAggregatorProxyHub>()).Return(Get<IHubContext>());

            var eventProxy = new EventProxy();

            //for (var i = 0; i < 100; i++)
            //{
            //    eventProxy.Subscribe(CreateHubContext(), typeName, new string[0], null, null);
            //}

            FailIfThreadCrashes(() => eventProxy.Subscribe(CreateHubContext(), typeName, new string[0], null, null));
            FailIfThreadCrashes(() =>
                {
                    if (ids.Count < 100) return;

                    string id;
                    if (ids.TryTake(out id))
                    {
                        eventProxy.Unsubscribe(id, typeNames);
                    }

                });
            FailIfThreadCrashes(() =>
                {
                    if (ids.Count < 100) return;

                    string id;
                    if (ids.TryTake(out id))
                    {
                        eventProxy.UnsubscribeConnection(id);
                    }
                });

            FailIfThreadCrashes(() => handler(new MembersEvent()));
            FailIfThreadCrashes(() => handler(new MembersEvent()));
            FailIfThreadCrashes(() => handler(new MembersEvent()));
            FailIfThreadCrashes(() => handler(new MembersEvent()));

            var timer = new System.Timers.Timer(benchmarkTime.TotalMilliseconds);
            timer.Elapsed += (s, e) => reset.Set();
            var start = DateTime.Now;
            timer.Start();

            reset.WaitOne();
            running = false;
            Console.WriteLine("Catched events: {0} in {1}", events.Count, DateTime.Now - start);
        }
 static EventAggregatorProxyHub()
 {
     eventProxy = new EventProxy();
 }