Example #1
0
        public TestContext()
        {
            var queue = new InMemoryQueues();
            var store = new InMemoryStore(Users.Values);

            var eventNotificationSender = new EventNotificationSender();
            var notificationHandlers    = new List <Type>()
            {
                typeof(ChatNotifier),
                typeof(ChatMessageNotifier),
                typeof(ChatParticipantNotifier)
            };
            var notificationBus = new InMemoryBus(queue, r => r.NotificationQueue, type => _chatNotificationModuleProvider.GetService(type), notificationHandlers);
            var chatNotificationModuleServiceCollection = new ServiceCollection();

            chatNotificationModuleServiceCollection.RegisterDotChat(new TestChatNotificationModule(notificationBus, store, eventNotificationSender));
            _chatNotificationModuleProvider = chatNotificationModuleServiceCollection.BuildServiceProvider();

            var workerHandlers = new List <Type>()
            {
                typeof(ChatsWorker),
                typeof(ChatMessagesWorker),
                typeof(ChatMessageIndexationWorker),
                typeof(ChatParticipantsWorker),
                typeof(ChatSystemMessagesWorker)
            };
            var workerBus = new InMemoryBus(queue, r => r.WorkerQueue, type => _chatWorkerModuleProvider.GetService(type), workerHandlers);
            var chatWorkerModuleServiceCollection = new ServiceCollection();

            chatWorkerModuleServiceCollection.RegisterDotChat(new TestChatWorkerModule(workerBus, store));
            _chatWorkerModuleProvider = chatWorkerModuleServiceCollection.BuildServiceProvider();

            foreach (var user in Users)
            {
                _userContexts.Add(user.Key, new TestUserContext(user.Value.UserId, _chatNotificationModuleProvider.GetService <IDotChat>(), eventNotificationSender, _chatIds));
            }

            NotificationBusTask = notificationBus.Start();
            WorkerBusTask       = workerBus.Start();
        }
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(r =>
            {
                var notificationHandlers = new List <Type>()
                {
                    typeof(ChatNotifier),
                    typeof(ChatMessageNotifier),
                    typeof(ChatParticipantNotifier)
                };

                var workerHandlers = new List <Type>()
                {
                    typeof(ChatsWorker),
                    typeof(ChatMessagesWorker),
                    typeof(ChatMessageIndexationWorker),
                    typeof(ChatParticipantsWorker),
                    typeof(ChatSystemMessagesWorker)
                };

                var queue = new InMemoryQueues();
                var bus   = new InMemoryBus(queue, r => r.WorkerQueue, type => r.GetService(type), workerHandlers.Concat(notificationHandlers));
                return(bus);
            });

            services.AddSingleton(r =>
            {
                var store = new InMemoryStore(Users);
                return(store);
            });

            services.RegisterDotChat(new TestChatWorkerModule());

            services.RegisterDotChat(new TestChatNotificationModule());

            services.AddSingleton <IUserIdProvider, UserIdProvider>();

            services.AddSignalR();
        }
Example #3
0
        public void Configuration(IAppBuilder app)
        {
            app.UseStaticFiles("/wwwroot");

            app.Use((context, next) =>
            {
                var userId = context.Request.Query.Get("userId");
                if (!string.IsNullOrEmpty(userId) && Guid.TryParse(userId, out var userIdGuid))
                {
                    context.Authentication.User = new ClaimsPrincipal(new List <ClaimsIdentity>()
                    {
                        new ClaimsIdentity(new List <Claim> {
                            new Claim("name", userIdGuid.ToString())
                        }, "Demo", "name", "role")
                    });
                }
                return(next.Invoke());
            });

            var        builder   = new ContainerBuilder();
            IContainer container = null;

            var notificationHandlers = new List <Type>()
            {
                typeof(ChatNotifier),
                typeof(ChatMessageNotifier),
                typeof(ChatParticipantNotifier)
            };

            var workerHandlers = new List <Type>()
            {
                typeof(ChatsWorker),
                typeof(ChatMessagesWorker),
                typeof(ChatMessageIndexationWorker),
                typeof(ChatParticipantsWorker),
                typeof(ChatSystemMessagesWorker)
            };

            var queue   = new InMemoryQueues();
            var store   = new InMemoryStore(Users);
            var bus     = new InMemoryBus(queue, r => r.WorkerQueue, type => container?.Resolve(type), workerHandlers.Concat(notificationHandlers));
            var busTask = bus.Start();

            app.Use((context, next) =>
            {
                if (busTask.IsFaulted)
                {
                    throw busTask.Exception;
                }
                return(next.Invoke());
            });

            builder.RegisterDotChat(new TestChatWorkerModule(bus, store));
            builder.RegisterDotChat(new TestChatNotificationModule());

            var settings = new JsonSerializerSettings();

            settings.ContractResolver = new SignalRContractResolver();
            var serializer = JsonSerializer.Create(settings);

            builder.RegisterInstance(serializer).As <JsonSerializer>();

            builder.RegisterType <UserIdProvider>().As <IUserIdProvider>().SingleInstance();

            var config = new HubConfiguration();

            config.EnableDetailedErrors = true;
            builder.RegisterHubs(Assembly.GetExecutingAssembly());
            builder.Register <IConnectionManagerAccessor>(r => new ConnectionManagerAccessor(config.Resolver.Resolve <IConnectionManager>()));

            container = builder.Build();

            var dotChat = container.Resolve <IDotChat>();

            dotChat.Chats.Add(Users[0].UserId, null, new ChatInfo
            {
                Name = "TestChat"
            }, new Basic.Participants.ParticipationCandidates(new List <ParticipationCandidate> {
                new ParticipationCandidate(Users[0].UserId, Participants.ChatParticipantType.Admin),
                new ParticipationCandidate(Users[3].UserId, Participants.ChatParticipantType.Admin)
            }, new List <ParticipationCandidate> {
            })).ContinueWith(async r => {
                await Task.Delay(TimeSpan.FromSeconds(10));
                await dotChat.ChatParticipants.Append(Users[0].UserId, r.Result, new List <ParticipationCandidate> {
                    new ParticipationCandidate(Users[1].UserId, Participants.ChatParticipantType.Participant),
                    new ParticipationCandidate(Users[2].UserId, Participants.ChatParticipantType.Participant)
                }, new List <ParticipationCandidate>());
            });

            Task.Delay(TimeSpan.FromSeconds(10))
            .ContinueWith((t) =>
                          dotChat.Chats.Add(Users[0].UserId, null, new ChatInfo
            {
                Name = "TestChat2"
            }, new Basic.Participants.ParticipationCandidates(new List <ParticipationCandidate> {
                new ParticipationCandidate(Users[0].UserId, Participants.ChatParticipantType.Admin),
                new ParticipationCandidate(Users[1].UserId, Participants.ChatParticipantType.Admin)
            }, new List <ParticipationCandidate> {
            })));

            foreach (var item in Enumerable.Range(3, 10))
            {
                dotChat.Chats.Add(Users[0].UserId, null, new ChatInfo
                {
                    Name = $"TestChat{item}"
                }, new Basic.Participants.ParticipationCandidates(new List <ParticipationCandidate> {
                    new ParticipationCandidate(Users[0].UserId, Participants.ChatParticipantType.Admin),
                    new ParticipationCandidate(Users[3].UserId, Participants.ChatParticipantType.Admin)
                }, new List <ParticipationCandidate> {
                }));
            }

            config.Resolver = new AutofacDependencyResolver(container);

            app.UseAutofacMiddleware(container);
            app.MapSignalR("/signalr", config);
        }