Beispiel #1
0
        public async Task SubscribeToEntityChanges(LifetimeScope lifetimeScope)
        {
            tableDependencyProvider = new ProductsSqlTableDependencyProvider(ConnectionString, ThreadPoolScheduler.Instance, lifetimeScope);

            tableDependencyProvider.SubscribeToEntityChanges();

            await OnInserted();

            await OnUpdated();

            await OnDeleted();
        }
Beispiel #2
0
        private static async Task ReconnectionTest(LifetimeScope lifetimeScope)
        {
            tableDependencyProvider =
                new ProductsSqlTableDependencyProvider(ConnectionString, TaskPoolScheduler.Default, lifetimeScope);

            tableDependencyProvider.SubscribeToEntityChanges();

            Product product = null;

            bool isFirstStart = true;
            var  subscription =
                tableDependencyProvider.WhenStatusChanges
                .Subscribe(c =>
            {
                if (c.IsOneOfFollowing(TableDependencyStatus.Started, TableDependencyStatus.WaitingForNotification) &&
                    isFirstStart)
                {
                    isFirstStart = false;

                    //ProcessProvider.Docker("stop sql");

                    SqlConnectionProvider.KillSessions(ConnectionString);

                    product = InsertNewProduct();
                }
            });

            await tableDependencyProvider.LastExceptionChanged.Select(c => Unit.Default)
            .Merge(tableDependencyProvider.LastInsertedProductChanged.Select(c => Unit.Default))
            .WaitFirst(TimeSpan.FromSeconds(10));

            // ProcessProvider.Docker("start sql");

            await tableDependencyProvider.WhenStatusChanges.Where(c =>
                                                                  c.IsOneOfFollowing(TableDependencyStatus.Started, TableDependencyStatus.WaitingForNotification))
            .FirstOrDefaultAsync().ToTask();

            using (subscription)
            {
            }

            DeleteProduct(product);

            await tableDependencyProvider.LastDeletedProductChanged
            .Where(c => c.Id == product.Id)
            .WaitFirst(tableDependencyProvider.ReconnectionTimeSpan);

            tableDependencyProvider.LastInsertedProduct.Should().NotBeNull();
        }
Beispiel #3
0
        public async Task TestRedisPubSub()
        {
            tableDependencyProvider =
                new ProductsSqlTableDependencyProvider(ConnectionString, TaskPoolScheduler.Default, LifetimeScope.UniqueScope);

            tableDependencyProvider.SubscribeToEntityChanges();

            var schedulersFactory = new SchedulersFactory();

            using var redisPublisher = new ProductSqlTableDependencyRedisProvider(tableDependencyProvider,
                                                                                  new RedisPublisher(RedisUrl), schedulersFactory.TaskPool);

            redisPublisher.StartPublishing();

            var reactiveDataWithStatus = new TestableReactiveData <Product>();

            using var domainEntitiesSubscriber =
                      new DomainEntitiesSubscriber <Product>(new RedisSubscriber(RedisUrl), reactiveDataWithStatus, schedulersFactory);

            await domainEntitiesSubscriber.Subscribe();

            var product = InsertNewProduct();

            var productEntityChange = await reactiveDataWithStatus.WhenDataChanges
                                      .Where(c => c.Entity.Id == product.Id)
                                      .FirstOrDefaultAsync()
                                      .ToTask();

            productEntityChange.ChangeType.Should().BeEquivalentTo(ChangeType.Create);

            product.Name = "Updated";
            AddOrUpdateProduct(product);

            var productUpdated = await reactiveDataWithStatus.WhenDataChanges
                                 .Where(c => c.Entity.Id == product.Id && c.ChangeType == ChangeType.Update)
                                 .FirstOrDefaultAsync()
                                 .ToTask();

            productUpdated.ChangeType.Should().BeEquivalentTo(ChangeType.Update);

            DeleteProduct(product);

            var productDeleted = await reactiveDataWithStatus.WhenDataChanges
                                 .Where(c => c.Entity.Id == product.Id && c.ChangeType == ChangeType.Delete)
                                 .FirstOrDefaultAsync()
                                 .ToTask();

            productDeleted.ChangeType.Should().BeEquivalentTo(ChangeType.Delete);
        }