public async Task SqliteRenewLeaseTests1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const uint transactionLeaseMs = 1000; using (var messageQueue = new SqliteMessageQueue(messageQueuePath, transactionLeaseMs: transactionLeaseMs)) { messageQueue.Clear(); using (var queueTransaction1 = messageQueue.CreateQueueTransaction()) { var expected = CreateRandomQueueItem(); queueTransaction1.Begin(); messageQueue.Enqueue(expected, queueTransaction1); // Dequeue the item to start the renew lease expiry var dequeued = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1); // Wait for twice the time - the lease should have expired if the renew code is not working await Task.Delay(TimeSpan.FromMilliseconds(transactionLeaseMs * 2)).ConfigureAwait(false); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1)); } using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction)); } } }
public void SqliteTransactionDisposeTest1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; var expected = CreateRandomQueueItem(); using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) { messageQueue.Clear(); // Enqueue the expected item TransactionalEnqueue(messageQueue, expected); using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); // Dequeue but don't call abort specifically - let the dispose method do this var actual = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction); AssertCompare(expected, actual); } using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); var actual = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction); AssertCompare(expected, actual); queueTransaction.Commit(); } } }
public void SqliteTransactionTests1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) { messageQueue.Clear(); using (var queueTransaction1 = messageQueue.CreateQueueTransaction()) { var expected = CreateRandomQueueItem(); queueTransaction1.Begin(); messageQueue.Enqueue(expected, queueTransaction1); var actual = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1); AssertCompare(expected, actual); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1)); queueTransaction1.Commit(); } } }
public void SqliteNestedTransactionTest() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) { messageQueue.Clear(); using (var queueTransaction1 = messageQueue.CreateQueueTransaction()) { queueTransaction1.Begin(); using (var queueTransaction2 = messageQueue.CreateQueueTransaction()) { queueTransaction2.Begin(); messageQueue.Enqueue(CreateRandomQueueItem(0), queueTransaction2); queueTransaction2.Commit(); } var queueItem = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1); Assert.IsNotNull(queueItem); queueTransaction1.Commit(); } } }
public async Task SqliteClearDuringTransaction1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const uint transactionLeaseMs = 1000; using (var messageQueue = new SqliteMessageQueue(messageQueuePath, transactionLeaseMs: transactionLeaseMs)) { messageQueue.Clear(); var expected = CreateRandomQueueItem(); // Also test Abort is called when disposing using (var transaction = messageQueue.CreateQueueTransaction()) { transaction.Begin(); messageQueue.Enqueue(expected, transaction); var actual = messageQueue.DequeueNextMessage <PushQueueItem>(transaction); messageQueue.Clear(); AssertCompare(expected, actual); // Wait for the renew lease task to be called at least once. await Task.Delay(TimeSpan.FromMilliseconds(transactionLeaseMs * 2)).ConfigureAwait(false); } using (var transaction = messageQueue.CreateQueueTransaction()) { transaction.Begin(); messageQueue.Enqueue(expected, transaction); var actual = messageQueue.DequeueNextMessage <PushQueueItem>(transaction); messageQueue.Clear(); AssertCompare(expected, actual); // Wait for the renew lease task to be called at least once. await Task.Delay(TimeSpan.FromMilliseconds(transactionLeaseMs * 2)).ConfigureAwait(false); // Make sure no exception is thrown. transaction.Commit(); } } }
public void SqliteTestDequeueOnly1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction)); queueTransaction.Abort(); } }
public async Task SqliteExpiredLeaseTests1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const uint transactionLeaseMs = 1000; using (var messageQueue = new SqliteMessageQueue(messageQueuePath, transactionLeaseMs: transactionLeaseMs)) { messageQueue.Clear(); var expected = CreateRandomQueueItem(); TransactionalEnqueue(messageQueue, expected); // Create a new transaction with the renew task larger than the time of the transaction lease (not a good idea in practice) using (var queueTransaction1 = new SqliteMessageQueueTransaction(messageQueue.DatabaseConnectionString, 60 * 1000)) { queueTransaction1.Begin(); // Dequeue the item to start the renew lease expiry var actual1 = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction1); AssertCompare(expected, actual1); // Wait for twice the time - the lease should have now expired await Task.Delay(TimeSpan.FromMilliseconds(transactionLeaseMs * 2)).ConfigureAwait(false); // Dequeue on a different transaction var actual2 = TransactionalDequeue <PushQueueItem>(messageQueue); // Check the item was recovered. AssertCompare(expected, actual2); queueTransaction1.Abort(); } } }
public void SqliteTestEnqueueDequeue1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; var expectedItem1 = CreateRandomQueueItem(0); var expectedItem2 = CreateRandomQueueItem(1); using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { // Clear the queue messageQueue.Clear(); queueTransaction.Begin(); messageQueue.Enqueue(expectedItem1, queueTransaction); messageQueue.Enqueue(expectedItem2, queueTransaction); queueTransaction.Commit(); } // Read from queue and then abort using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); var actualItem = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction); AssertCompare(expectedItem1, actualItem); queueTransaction.Abort(); } // Read from queue and the commit using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); var actualItem = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction); AssertCompare(expectedItem1, actualItem); queueTransaction.Commit(); } // Read from queue and check we can dequeue the second item. using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); var actualItem = messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction); AssertCompare(expectedItem2, actualItem); queueTransaction.Commit(); } // Read from queue and check we can dequeue the second item. using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction)); queueTransaction.Abort(); } }