public Service()
 {
     this.ExpressionCompiler = new ExpressionCompiler();
     this.DependencyResolver = Substitute.For <IDependencyResolver>();
     this.Storage            = Substitute.For <ITransactionStorage>();
     this.StateSerializer    = Substitute.For <IStateSerializer>();
     this.StateDeserializer  = Substitute.For <IStateDeserializer>();
     this.DependencyResolver.GetService <Transaction>().Returns(TransactionFactory.CreateNew(this.Storage));
 }
        public async Task CreateTransaction_ReturnsTransactionWithSameState()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);

            // Act
            var committedTransaction = await storage.CreateTransactionAsync(transaction);

            // Assert
            Assert.Equal(transaction.Status, committedTransaction.Status);
        }
        public async Task CreateTransaction_InvokedTransactionCommitted()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage).With((ref TransactionData t) => { t.Revision = 2; });

            // Act
            var ev = await Assert.RaisesAsync <TransactionCommittedEventArgs>(v => storage.TransactionCommitted += v, v => storage.TransactionCommitted -= v, () => storage.CreateTransactionAsync(transaction));

            // Assert
            Assert.Equal(transaction.Id, ev.Arguments.Transaction.Id);
        }
        public async Task CreateTransaction_RevisionIsIgnored()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage).With((ref TransactionData t) => { t.Revision = 2; });

            // Act
            var committedTransaction = await storage.CreateTransactionAsync(transaction);

            // Assert
            Assert.Equal(1, committedTransaction.Revision);
        }
        public async Task CreateTransaction_ReturnsTransactionWithSameState_StateSpecified(TransactionStatus state)
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage).With((ref TransactionData t) => t.Status = state);

            // Act
            var committedTransaction = (await storage.CreateTransactionAsync(transaction));

            // Assert
            Assert.Equal(state, committedTransaction.Status);
        }
        public async Task CreateTransaction_TransactionAlreadyExists_ThrowsInvalidOperationException()
        {
            // Arrange
            var storage              = this.CreateStorage();
            var transaction          = TransactionFactory.CreateNew(storage);
            var committedTransaction = await storage.CreateTransactionAsync(transaction);

            // Act
            await Assert.ThrowsAsync <TransactionExistsException>(() => storage.CreateTransactionAsync(transaction));

            // Assert
            Assert.Equal(transaction.Status, committedTransaction.Status);
        }
        public async Task CommitTransactionDelta_RevisionExists_ThrowsTransactionRevisionExistsException()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = await storage.CreateTransactionAsync(TransactionFactory.CreateNew(storage));

            // Act
            var ex = await Assert.ThrowsAsync <TransactionRevisionExistsException>(() => storage.CommitTransactionDeltaAsync(transaction, transaction));

            // Assert
            Assert.Equal(transaction.Id, ex.TransactionId);
            Assert.Equal(transaction.Revision, ex.Revision);
        }
        public async Task FetchTransaction_RetrievesCommittedTransaction()
        {
            // Arrange
            var storage              = this.CreateStorage();
            var transaction          = TransactionFactory.CreateNew(storage);
            var committedTransaction = await storage.CreateTransactionAsync(transaction);

            // Act
            var fetchedTransaction = await storage.FetchTransactionAsync(transaction.Id);

            // Assert
            Assert.Equal(committedTransaction, fetchedTransaction);
        }
        public async Task StoreTransactionState_NoStateDefined_ReturnsEmptyState()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(transaction);

            // Act
            var result = await storage.GetTransactionStateAsync(transaction.Id, transaction.Revision);

            // Assert
            Assert.Empty(result);
        }
Esempio n. 10
0
        public async Task TransactionExists_ReturnsTrue()
        {
            // Arrange
            var storage = this.CreateStorage();
            var trans   = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(trans);

            // Act
            var result = await storage.TransactionExistsAsync(trans.Id);

            // Assert
            Assert.True(result);
        }
Esempio n. 11
0
        public async Task StoreTransactionState_SameStateReturned()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(transaction);

            // Act
            storage.SaveTransactionState(transaction.Id, transaction.Revision, new byte[] { 1, 2, 3 });
            var result = await storage.GetTransactionStateAsync(transaction.Id, transaction.Revision);

            // Assert
            Assert.Equal(new byte[] { 1, 2, 3 }, result);
        }
Esempio n. 12
0
        public async Task CommitTransactionDelta_RevisionInvalid_ThrowsArgumentException(int revision)
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = await storage.CreateTransactionAsync(TransactionFactory.CreateNew(storage));

            var delta = transaction.With((ref TransactionData t) => { t.Revision = revision; });

            // Act
            var ex = await Assert.ThrowsAsync <ArgumentException>(() => storage.CommitTransactionDeltaAsync(transaction, delta));

            // Assert
            Assert.Equal("next", ex.ParamName);
        }
Esempio n. 13
0
        public async Task Query_GetById_ReturnsTransaction()
        {
            // Arrange
            var storage = this.CreateStorage();
            var tr      = TransactionFactory.CreateNew(storage);
            var guid    = tr.Id;

            tr = await storage.CreateTransactionAsync(tr);

            // Act
            var result = (await storage.QueryAsync()).Where(x => x.Id == guid).ToArray();

            // Assert
            Assert.Equal(tr, result.Single());
        }
Esempio n. 14
0
        public async Task CommitTransactionDelta_CreatesDelta()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = await storage.CreateTransactionAsync(TransactionFactory.CreateNew(storage));

            var delta = transaction.With((ref TransactionData t) => { t.Status = TransactionStatus.Authorized; t.Revision = 2; });

            // Act
            var result = await storage.CommitTransactionDeltaAsync(transaction, delta);

            // Assert
            Assert.Equal(2, result.Revision);
            Assert.Equal(TransactionStatus.Authorized, result.Status);
        }
Esempio n. 15
0
        public async Task CreateTransaction_UsesTimeService_AsCreatedTime()
        {
            // Arrange
            var timeService = Substitute.For <ITimeService>();
            var created     = new DateTime(1999, 01, 05, 12, 0, 0, DateTimeKind.Utc);

            timeService.Now().Returns(created);
            var storage = this.CreateStorage(timeService);

            // Act
            var tr = await storage.CreateTransactionAsync(TransactionFactory.CreateNew(storage));

            // Assert
            Assert.Equal(created, tr.Created);
        }
Esempio n. 16
0
        public async Task GetChain_ReturnsChain()
        {
            // Arrange
            var storage = this.CreateStorage();
            var tr1     = TransactionFactory.CreateNew(storage);
            var tr2     = tr1.With((ref TransactionData tr) => { tr.Revision = 2; });

            tr1 = await storage.CreateTransactionAsync(tr1);

            tr2 = await storage.CommitTransactionDeltaAsync(tr1, tr2);

            // Act
            var chain = (await storage.GetChainAsync(tr1.Id)).ToArray();

            // Assert
            Assert.Equal(new[] { tr1, tr2 }, chain);
        }
Esempio n. 17
0
        public async Task GetExpiringTransactions_RetrivesExpiredTransacions()
        {
            // Arrange
            var timeService = Substitute.For <ITimeService>();

            timeService.Now().Returns(new DateTime(1999, 05, 15));
            var storage = this.CreateStorage(timeService);
            var trans   = TransactionFactory.CreateNew(storage).With((ref TransactionData t) => { t.Expires = new DateTime(1999, 05, 14); });

            trans = await storage.CreateTransactionAsync(trans);

            // Act
            var expiringTransactions = await storage.GetExpiringTransactionsAsync(CancellationToken.None);

            // Assert
            Assert.Contains(trans, expiringTransactions);
        }
Esempio n. 18
0
        public async Task IsTransactionLocked_ReturnsTrue_IfTransactionIsLocked()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(transaction);

            // Act
            await storage.LockTransactionAsync(transaction.Id);

            var result = await storage.IsTransactionLockedAsync(transaction.Id);

            await storage.FreeTransactionAsync(transaction.Id);

            // Assert
            Assert.True(result);
        }
Esempio n. 19
0
        public async Task TryLockTransaction_TransactionAldreadyLocked_ReturnsFalse()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(transaction);

            // Act
            await storage.LockTransactionAsync(transaction.Id);

            var tryLockStatus = await storage.TryLockTransactionAsync(transaction.Id, LockFlags.None, 0);

            await storage.FreeTransactionAsync(transaction.Id);

            // Assert
            Assert.False(tryLockStatus);
        }
Esempio n. 20
0
        public async Task TryLockTransaction_LocksTransaction_ReturnsTrue()
        {
            // Arrange
            var storage     = this.CreateStorage();
            var transaction = TransactionFactory.CreateNew(storage);
            await storage.CreateTransactionAsync(transaction);

            // Act
            var tryLockStatus = await storage.TryLockTransactionAsync(transaction.Id);

            var isLocked = await storage.IsTransactionLockedAsync(transaction.Id);

            await storage.FreeTransactionAsync(transaction.Id);

            // Assert
            Assert.True(tryLockStatus);
            Assert.True(isLocked);
        }
Esempio n. 21
0
        public async Task Query_GetByCreated_ReturnsTransaction()
        {
            // Arrange
            var created     = DateTime.UtcNow;
            var timeService = Substitute.For <ITimeService>();

            timeService.Now().Returns(created);
            var storage = this.CreateStorage(timeService);
            var tr      = TransactionFactory.CreateNew(storage);
            var guid    = tr.Id;

            tr = await storage.CreateTransactionAsync(tr);

            // Act
            var result = (await storage.QueryAsync()).Where(x => x.Created == created).ToArray();

            // Assert
            Assert.Contains(tr, result);
        }
Esempio n. 22
0
        public async Task Commit_CommitsTransaction()
        {
            // Arrange
            var service = new Service();
            var storage = new MemoryStorage();
            var trans   = TransactionFactory.CreateNew(storage);

            trans = await storage.CreateTransactionAsync(trans);

            service.Transaction = trans;

            // Act
            service.CompileAndRun(@"module foo;

await commit this;
");

            // Assert
            Assert.Equal(2, (await storage.FetchTransactionAsync(trans.Id)).Revision);
        }
Esempio n. 23
0
        public async Task Commit_WithExpression_CommitsTransaction()
        {
            // Arrange
            var service = new Service();
            var storage = new MemoryStorage();
            var trans   = TransactionFactory.CreateNew(storage);

            trans = await storage.CreateTransactionAsync(trans);

            service.Transaction = trans;

            // Act
            service.CompileAndRun(@"module foo;

await commit this with { Script: ""Foo"" };
");

            // Assert
            Assert.Equal("Foo", (await storage.FetchTransactionAsync(trans.Id)).Script);
        }
Esempio n. 24
0
        public void Compile_Greeting_Success()
        {
            // Arrange
            var muteCompiler = new Compiler();
            var result       = muteCompiler.Compile("module foo;\r\nlet s <- import 'greeting' as string;");

            var serializer         = new StateSerializer();
            var deserializer       = new StateDeserializer();
            var dependencyResolver = new DefaultDependencyResolver();

            dependencyResolver.Register <Transaction>(TransactionFactory.CreateNew(null), null);
            dependencyResolver.Register <ITimeService>(Substitute.For <ITimeService>(), null);
            dependencyResolver.Register("Hello World!", "greeting");

            // Act
            var state = result.Result(serializer, deserializer, dependencyResolver);

            // Assert
            Assert.True(result.Success);
        }
Esempio n. 25
0
        public async Task CommitTransactionDelta_UsesTimeService_AsCreatedTime()
        {
            // Arrange
            var timeService = Substitute.For <ITimeService>();
            var created     = new DateTime(1999, 01, 05, 12, 0, 0, DateTimeKind.Utc);
            var updated     = new DateTime(2000, 01, 05, 12, 0, 0, DateTimeKind.Utc);

            timeService.Now().Returns(created, updated);
            var storage = this.CreateStorage(timeService);
            var tr      = await storage.CreateTransactionAsync(TransactionFactory.CreateNew(storage));

            var trNext = tr.Data;

            trNext.Revision += 1;

            // Act
            var result = await storage.CommitTransactionDeltaAsync(tr, new Transaction(trNext, storage));

            // Assert
            Assert.Equal(updated, result.Created);
        }