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 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 SqliteTransactionTests2() { 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); using (var queueTransaction2 = messageQueue.CreateQueueTransaction()) { queueTransaction2.Begin(); Assert.ThrowsException <MessageQueueReadException>(() => messageQueue.DequeueNextMessage <PushQueueItem>(queueTransaction2)); } queueTransaction1.Abort(); } } }
public void SqliteTransactionTests1() { string messageQueuePath = $@".\Private$\{Guid.NewGuid().ToString()}"; 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 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(); } } }
private static void Enqueue(PushQueueItem queueItem, SqliteMessageQueue messageQueue) { using (var queueTransaction = messageQueue.CreateQueueTransaction()) { queueTransaction.Begin(); messageQueue.Enqueue(queueItem, queueTransaction); queueTransaction.Commit(); } }
public void SqliteTestConcurrentReadWrite4() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const int numberMessages = 10; using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) { messageQueue.Clear(); } var expectedResults = new PushQueueItem[numberMessages]; var actualResults = new PushQueueItem[numberMessages]; for (var i = 0; i < numberMessages; i++) { expectedResults[i] = CreateRandomQueueItem(i); } using (var messageQueue = new SqliteMessageQueue(messageQueuePath)) { using (var messageQueueTransaction = messageQueue.CreateQueueTransaction()) { messageQueueTransaction.Begin(); Parallel.For(0, numberMessages * 2, i => { if (i % 2 == 0) { messageQueue.Enqueue(expectedResults[i / 2], messageQueueTransaction); } else { var dequeueResult = TryDequeue <PushQueueItem>(messageQueue, messageQueueTransaction, timeoutMs: 120 * 1000); if (actualResults[dequeueResult.DequeueCount] != null) { throw new ArgumentException("This item has already been dequeued. Something is wrong."); } actualResults[dequeueResult.DequeueCount] = dequeueResult; } }); messageQueueTransaction.Commit(); } } for (var i = 0; i < numberMessages; i++) { AssertCompare(expectedResults[i], actualResults[i]); } }
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 void SqliteTransactionExceptionTests2() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const uint transactionLeaseMs = 3000; using (var messageQueue = new SqliteMessageQueue(messageQueuePath, transactionLeaseMs: transactionLeaseMs)) { var queueTransaction1 = messageQueue.CreateQueueTransaction(); queueTransaction1.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => queueTransaction1.Begin()); Assert.ThrowsException <ObjectDisposedException>(() => queueTransaction1.Commit()); Assert.ThrowsException <ObjectDisposedException>(() => queueTransaction1.Abort()); } }
public void SqliteTransactionExceptionTests1() { var messageQueuePath = $@".\Private$\{Guid.NewGuid()}"; const uint transactionLeaseMs = 3000; using (var messageQueue = new SqliteMessageQueue(messageQueuePath, transactionLeaseMs: transactionLeaseMs)) { messageQueue.Clear(); using (var queueTransaction1 = messageQueue.CreateQueueTransaction()) { Assert.ThrowsException <InvalidOperationException>(() => queueTransaction1.Commit()); Assert.ThrowsException <InvalidOperationException>(() => 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(); } }