Ejemplo n.º 1
0
        public async Task SubscriptionDeliversFutureCommands(MemstateSettings settings)
        {
            const int NumRecords = 5;

            settings.LoggerFactory.AddProvider(new TestOutputLoggingProvider(_log));
            settings.StreamName = _randomStreamName;

            var provider  = settings.CreateStorageProvider();
            var records   = new List <JournalRecord>();
            var subSource = provider.CreateJournalSubscriptionSource();
            var sub       = subSource.Subscribe(0, records.Add);
            var writer    = provider.CreateJournalWriter(0);

            for (var i = 0; i < NumRecords; i++)
            {
                writer.Send(new AddStringCommand(i.ToString()));
            }

            await writer.DisposeAsync().ConfigureAwait(false);

            await WaitForConditionOrThrow(() => records.Count == 5).ConfigureAwait(false);

            sub.Dispose();

            Assert.Equal(5, records.Count);
        }
Ejemplo n.º 2
0
        public async Task SubscriptionDeliversPreExistingCommands(MemstateSettings settings)
        {
            settings.LoggerFactory.AddProvider(new TestOutputLoggingProvider(_log));
            settings.StreamName = _randomStreamName;

            var       provider      = settings.CreateStorageProvider();
            const int NumRecords    = 50;
            var       journalWriter = provider.CreateJournalWriter(0);

            for (var i = 0; i < NumRecords; i++)
            {
                journalWriter.Send(new AddStringCommand(i.ToString()));
            }

            await journalWriter.DisposeAsync().ConfigureAwait(false);

            var records   = new List <JournalRecord>();
            var subSource = provider.CreateJournalSubscriptionSource();

            if (!provider.SupportsCatchupSubscriptions())
            {
                Assert.Throws <NotSupportedException>(() => subSource.Subscribe(0, records.Add));
            }
            else
            {
                subSource.Subscribe(0, records.Add);
                await WaitForConditionOrThrow(() => records.Count == NumRecords).ConfigureAwait(false);

                Assert.Equal(Enumerable.Range(0, NumRecords), records.Select(r => (int)r.RecordNumber));
            }
        }
        public async Task MapsToCommand()
        {
            //Arrange
            var settings = new MemstateSettings();

            settings.FileSystem = new InMemoryFileSystem();
            var storageProvider = settings.CreateStorageProvider();
            var builder         = new EngineBuilder(settings, storageProvider);
            var engine          = builder.Build <ITestModel>(new TestModel());
            var client          = new LocalClient <ITestModel>(engine);
            var proxy           = client.GetDispatchProxy();

            //Act
            proxy.SetCustomer(new Customer());

            //release the lock on the journal
            await engine.DisposeAsync();

            var journalEntry = storageProvider.CreateJournalReader().GetRecords().FirstOrDefault();

            // If MapTo is correct, a SetCustomerCommand will be written to the journal
            // if not, then a ProxyCommand will be written
            Assert.NotNull(journalEntry);
            Assert.IsInstanceOf <SetCustomerCommand>(journalEntry.Command);
        }
        public ProxyOverloadingTests()
        {
            var settings        = new MemstateSettings().WithInmemoryStorage();
            var storageProvider = settings.CreateStorageProvider();
            var engine          = new EngineBuilder(settings, storageProvider).Build <ModelWithOverloads>();
            var client          = new LocalClient <ModelWithOverloads>(engine);

            _db = client.GetDispatchProxy();
        }
Ejemplo n.º 5
0
        public async Task One_journal_entry_per_line_when_using_json_format()
        {
            const int    NumRecords = 100;
            const string Stream     = "test";
            const string FileName   = Stream + ".journal";

            var settings = new MemstateSettings().WithInmemoryStorage();

            settings.Serializers.Register("newtonsoft.json", _ => new JsonSerializerAdapter(settings));
            settings.Serializer = "newtonsoft.json";

            settings.StreamName = Stream;
            var provider = settings.CreateStorageProvider();

            //Write NumRecords entries
            var writer = provider.CreateJournalWriter(0);

            foreach (var i in Enumerable.Range(1, NumRecords))
            {
                writer.Send(new Set <int>("key" + i, i));
            }

            //wait for the writes to complete
            await writer.DisposeAsync().ConfigureAwait(false);

            //Read back all the entries, should be NumRecords
            var reader = provider.CreateJournalReader();

            Assert.AreEqual(NumRecords, reader.GetRecords().Count());
            await reader.DisposeAsync().ConfigureAwait(false);

            //Count the actual lines in the file
            Assert.IsTrue(settings.FileSystem.Exists(FileName));
            var streamReader = new StreamReader(settings.FileSystem.OpenRead(FileName));
            var lines        = 0;

            while (true)
            {
                var line = streamReader.ReadLine();
                if (line == null)
                {
                    break;
                }
                Console.WriteLine("> " + line);
                lines++;
            }
            Assert.AreEqual(NumRecords, lines);
        }
Ejemplo n.º 6
0
        public void MapsToCommand()
        {
            var config = new MemstateSettings();

            config.StorageProvider = typeof(InMemoryStorageProvider).FullName;
            var storageProvider = config.CreateStorageProvider();
            var builder         = new EngineBuilder(config, storageProvider);
            var engine          = builder.Build <ITestModel>(new TestModel());
            var client          = new LocalClient <ITestModel>(engine);
            var proxy           = client.GetDispatchProxy();

            proxy.SetCustomer(new Customer());
            var journalEntry = storageProvider.CreateJournalReader().GetRecords().FirstOrDefault();

            Assert.NotNull(journalEntry);
            Assert.IsType(typeof(SetCustomerCommand), journalEntry.Command);
        }
Ejemplo n.º 7
0
        public void MapsToCommand()
        {
            var settings = new MemstateSettings();

            settings.FileSystem = new InMemoryFileSystem();
            var storageProvider = settings.CreateStorageProvider();
            var builder         = new EngineBuilder(settings, storageProvider);
            var engine          = builder.Build <ITestModel>(new TestModel());
            var client          = new LocalClient <ITestModel>(engine);
            var proxy           = client.GetDispatchProxy();

            proxy.SetCustomer(new Customer());
            var journalEntry = storageProvider.CreateJournalReader().GetRecords().FirstOrDefault();

            Assert.NotNull(journalEntry);
            Assert.IsType <SetCustomerCommand>(journalEntry.Command);
        }
Ejemplo n.º 8
0
        public async Task CanWriteOne(MemstateSettings settings)
        {
            settings.LoggerFactory.AddProvider(new TestOutputLoggingProvider(_log));
            settings.StreamName = _randomStreamName;
            _log.WriteLine(settings.ToString());

            var provider = settings.CreateStorageProvider();

            provider.Initialize();
            var writer = provider.CreateJournalWriter(0);

            writer.Send(new AddStringCommand("hello"));
            await writer.DisposeAsync().ConfigureAwait(false);

            var reader  = provider.CreateJournalReader();
            var records = reader.GetRecords().ToArray();
            await reader.DisposeAsync().ConfigureAwait(false);

            Assert.Equal(1, records.Length);
        }
Ejemplo n.º 9
0
        public void SmokeTest()
        {
            var config = new MemstateSettings();

            config.FileSystem = new InMemoryFileSystem();
            var initialModel       = new List <string>();
            var provider           = config.CreateStorageProvider();
            var commandStore       = provider.CreateJournalWriter(0);
            var subscriptionSource = provider.CreateJournalSubscriptionSource();
            var engine             = new Engine <List <string> >(config, initialModel, subscriptionSource, commandStore, 0);
            var tasks = Enumerable.Range(10, 10000)
                        .Select(n => engine.ExecuteAsync(new AddStringCommand(n.ToString())))
                        .ToArray();

            int expected = 1;

            foreach (var task in tasks)
            {
                Assert.Equal(expected++, task.Result);
            }
        }
Ejemplo n.º 10
0
        public async Task WriteAndReadCommands(MemstateSettings settings)
        {
            settings.LoggerFactory.AddProvider(new TestOutputLoggingProvider(_log));
            settings.StreamName = _randomStreamName;

            var provider = settings.CreateStorageProvider();

            provider.Initialize();

            var journalWriter = provider.CreateJournalWriter(0);

            for (var i = 0; i < 10000; i++)
            {
                journalWriter.Send(new AddStringCommand(i.ToString()));
            }

            await journalWriter.DisposeAsync().ConfigureAwait(false);

            var journalReader = provider.CreateJournalReader();
            var records       = journalReader.GetRecords().ToArray();
            await journalReader.DisposeAsync().ConfigureAwait(false);

            Assert.Equal(10000, records.Length);
        }