Exemplo n.º 1
0
        public void CanSubscribeToIndexChanges()
        {
            using (var store = GetDocumentStore())
            {
                store.Changes().Task.Result
                .ForAllIndexes().Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.IndexAdded)
                    {
                        output.Add("passed_forallindexesadded");
                    }
                }));

                new Companies_CompanyByType().Execute(store);
                WaitForIndexing(store);
                WaitUntilOutput("passed_forallindexesadded");

                var usersByName = new Users_ByName();
                usersByName.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                .ForIndex(usersByName.IndexName).Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.MapCompleted)
                    {
                        output.Add("passed_forindexmapcompleted");
                    }
                }));

                var companiesSompanyByType = new Companies_CompanyByType();
                companiesSompanyByType.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                .ForIndex(companiesSompanyByType.IndexName).Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    Console.WriteLine(JsonConvert.SerializeObject(change));
                    if (change.Type == IndexChangeTypes.RemoveFromIndex)
                    {
                        output2.Add("passed_forindexremovecompleted");
                    }
                    if (change.Type == IndexChangeTypes.ReduceCompleted)
                    {
                        output.Add("passed_forindexreducecompleted");
                    }
                }));

                using (var session = store.OpenSession())
                {
                    session.Store(new User {
                        Name = "user", LastName = "user"
                    });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexmapcompleted");

                    session.Store(new Company {
                        Id = "companies/1", Name = "company", Type = Company.CompanyType.Public
                    });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexreducecompleted");

                    session.Delete("companies/1");
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput2("passed_forindexremovecompleted");
                }


                store.Changes().Task.Result
                .ForAllIndexes().Task.Result
                .Subscribe(new IndexChangeActionObserver(change =>
                {
                    if (change.Type == IndexChangeTypes.IndexRemoved)
                    {
                        output.Add("passed_forallindexesremoved");
                    }
                }));
                store.DatabaseCommands.DeleteIndex("Companies/CompanyByType");
                WaitForIndexing(store);
                Assert.Contains("passed_forallindexesremoved", output);
            }
        }
Exemplo n.º 2
0
        public void CanSubscribeToIndexChanges()
        {
            using (var store = GetDocumentStore())
            {
                store.Changes().Task.Result
                    .ForAllIndexes().Task.Result
                    .Subscribe(change => 
                    {
                        if (change.Type == IndexChangeTypes.IndexAdded)
                        {
                            output = "passed_forallindexesadded";
                        }
                    });

                new Companies_CompanyByType().Execute(store);
                WaitForIndexing(store);
                WaitUntilOutput("passed_forallindexesadded");

                var usersByName = new Users_ByName();
                usersByName.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                    .ForIndex(usersByName.IndexName).Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.MapCompleted)
                        {
                            output = "passed_forindexmapcompleted";
                        }
                    });

                var companiesSompanyByType = new Companies_CompanyByType();
                companiesSompanyByType.Execute(store);
                WaitForIndexing(store);
                store.Changes().Task.Result
                    .ForIndex(companiesSompanyByType.IndexName).Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.RemoveFromIndex)
                        {
                            output2 = "passed_forindexremovecompleted";
                        }
                        if (change.Type == IndexChangeTypes.ReduceCompleted)
                        {
                            output = "passed_forindexreducecompleted";
                        }
                    });

                using (var session = store.OpenSession())
                {
                    session.Store(new User { Name = "user", LastName = "user" });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexmapcompleted");

                    session.Store(new Company { Id = "companies/1", Name = "company", Type = Company.CompanyType.Public });
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput("passed_forindexreducecompleted");

                    session.Delete("companies/1");
                    session.SaveChanges();
                    WaitForIndexing(store);
                    WaitUntilOutput2("passed_forindexremovecompleted");
                }


                store.Changes().Task.Result
                    .ForAllIndexes().Task.Result
                    .Subscribe(change =>
                    {
                        if (change.Type == IndexChangeTypes.IndexRemoved)
                        {
                            output = "passed_forallindexesremoved";
                        }
                    });
                store.DatabaseCommands.DeleteIndex("Companies/CompanyByType");
                WaitForIndexing(store);
                Assert.Equal("passed_forallindexesremoved", output);
            }
        }
Exemplo n.º 3
0
        public void IndexStartupBehaviorType_Should_Work_Correctly(IndexingConfiguration.IndexStartupBehaviorType type)
        {
            DoNotReuseServer();

            using (var store = GetDocumentStore(new Options
            {
                RunInMemory = false,
                ModifyDatabaseRecord = r => r.Settings[RavenConfiguration.GetKey(x => x.Indexing.IndexStartupBehavior)] = type.ToString()
            }))
            {
                string autoIndexName;
                using (var session = store.OpenSession())
                {
                    session.Query <Company>()
                    .Statistics(out var stats)
                    .Where(x => x.Name == "HR")
                    .ToList();     // create auto-index

                    autoIndexName = stats.IndexName;
                }

                var index = new Companies_CompanyByType();
                index.Execute(store);
                var staticIndexName = index.IndexName;

                // IndexStartupBehaviorType should not affect newly created indexes
                var indexStats = store.Maintenance.Send(new GetIndexStatisticsOperation(autoIndexName));
                Assert.Equal(IndexRunningStatus.Running, indexStats.Status);

                indexStats = store.Maintenance.Send(new GetIndexStatisticsOperation(staticIndexName));
                Assert.Equal(IndexRunningStatus.Running, indexStats.Status);

                Server.ServerStore.DatabasesLandlord.UnloadDirectly(store.Database);

                if (type == IndexingConfiguration.IndexStartupBehaviorType.Delay)
                {
                    Server.ServerStore.DatabasesLandlord.ForTestingPurposesOnly().OnBeforeDocumentDatabaseInitialization = database =>
                    {
                        database.IndexStore.ForTestingPurposesOnly().AfterIndexesOpen = () =>
                        {
                            foreach (var index in database.IndexStore.GetIndexes())
                            {
                                Assert.Equal(IndexRunningStatus.Paused, index.Status);
                            }
                        };
                    };
                }

                IndexRunningStatus expectedStatus;
                switch (type)
                {
                case IndexingConfiguration.IndexStartupBehaviorType.Default:
                case IndexingConfiguration.IndexStartupBehaviorType.Immediate:
                case IndexingConfiguration.IndexStartupBehaviorType.Delay:
                    expectedStatus = IndexRunningStatus.Running;
                    break;

                case IndexingConfiguration.IndexStartupBehaviorType.Pause:
                    expectedStatus = IndexRunningStatus.Paused;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
                }

                Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(store.Database); // start loading the database

                indexStats = store.Maintenance.Send(new GetIndexStatisticsOperation(autoIndexName));
                Assert.Equal(expectedStatus, indexStats.Status);

                indexStats = store.Maintenance.Send(new GetIndexStatisticsOperation(staticIndexName));
                Assert.Equal(expectedStatus, indexStats.Status);
            }
        }