Exemple #1
0
        public async Task FileRepository_FileChanged_UpdatesData()
        {
            IClient  testClient = CreateClient();
            IProduct product    = CreateProduct();

            System.IO.File.Delete(TEST_FILE);
            using FileRepository repo = new FileRepository();
            Assert.IsTrue(repo.OpenRepository(TEST_FILE).GetAwaiter().GetResult());
            await repo.CreateClient(
                testClient.Username,
                testClient.FirstName,
                testClient.LastName,
                testClient.Street,
                testClient.StreetNumber,
                testClient.PhoneNumber);

            await repo.CreateProduct(product.Name, product.Price, product.ProductType);

            string fileData = System.IO.File.ReadAllText(TEST_FILE);
            await repo.RemoveClient(testClient);

            await repo.RemoveProduct((await repo.GetAllProducts()).First());

            Assert.AreEqual(0, (await repo.GetAllClients()).Count);
            Assert.AreEqual(0, (await repo.GetAllProducts()).Count);
            bool         clientsReplaced  = false;
            bool         productsReplaced = false;
            TestObserver obs = new TestObserver();

            using IDisposable clientUnsubscriber  = repo.Subscribe((IObserver <DataChanged <IClient> >)obs);
            using IDisposable productUnsubscriber = repo.Subscribe((IObserver <DataChanged <IProduct> >)obs);
            System.IO.File.WriteAllText(TEST_FILE, fileData);
            SpinWait.SpinUntil(() => {
                clientsReplaced  |= obs.ClientsReplaced();
                productsReplaced |= obs.ProductsReplaced();
                return(clientsReplaced && productsReplaced);
            }, UPDATE_TIMEOUT);
            Assert.IsTrue(clientsReplaced);
            Assert.IsTrue(productsReplaced);
        }