PageRenderTime 273ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Tests/ManagedStorage/Transactions.cs

http://github.com/ravendb/ravendb
C# | 307 lines | 257 code | 45 blank | 5 comment | 1 complexity | 521882f3a5c88e9c4efbf2a29ebfa637 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. //-----------------------------------------------------------------------
  2. // <copyright file="Transactions.cs" company="Hibernating Rhinos LTD">
  3. // Copyright (c) Hibernating Rhinos LTD. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. using System;
  7. using System.Linq;
  8. using Raven.Abstractions.Data;
  9. using Raven.Abstractions.Exceptions;
  10. using Raven.Json.Linq;
  11. using Xunit;
  12. namespace Raven.Tests.ManagedStorage
  13. {
  14. public class Transactions : TxStorageTest
  15. {
  16. [Fact]
  17. public void AddingDocInTxCannotBeReadOutside()
  18. {
  19. var transactionInformation = new TransactionInformation
  20. {
  21. Id = Guid.NewGuid(),
  22. Timeout = TimeSpan.FromDays(7)
  23. };
  24. using (var tx = NewTransactionalStorage())
  25. {
  26. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  27. transactionInformation));
  28. tx.Batch(viewer =>
  29. Assert.True(viewer.Documents.DocumentByKey("Ayende", null).Metadata.Value<bool>(Constants.RavenDocumentDoesNotExists)));
  30. }
  31. }
  32. [Fact]
  33. public void CanModifyTxId()
  34. {
  35. var transactionInformation = new TransactionInformation
  36. {
  37. Id = Guid.NewGuid(),
  38. Timeout = TimeSpan.FromDays(7)
  39. };
  40. using (var tx = NewTransactionalStorage())
  41. {
  42. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  43. transactionInformation));
  44. var txInfo2 = new TransactionInformation
  45. {
  46. Id = Guid.NewGuid(),
  47. Timeout = TimeSpan.FromDays(1)
  48. };
  49. tx.Batch(mutator => mutator.Transactions.ModifyTransactionId(transactionInformation.Id, txInfo2.Id, txInfo2.Timeout));
  50. tx.Batch(viewer =>
  51. Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", txInfo2)));
  52. }
  53. }
  54. [Fact]
  55. public void AfterCommittingCanSeeChangesWithoutTx()
  56. {
  57. var transactionInformation = new TransactionInformation
  58. {
  59. Id = Guid.NewGuid(),
  60. Timeout = TimeSpan.FromDays(7)
  61. };
  62. using (var tx = NewTransactionalStorage())
  63. {
  64. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  65. transactionInformation));
  66. tx.Batch(mutator => mutator.Transactions.CompleteTransaction(transactionInformation.Id, data =>
  67. {
  68. if (data.Delete)
  69. {
  70. RavenJObject metadata;
  71. mutator.Documents.DeleteDocument(data.Key, null, out metadata);
  72. }
  73. else
  74. mutator.Documents.AddDocument(data.Key, null, data.Data, data.Metadata);
  75. }));
  76. tx.Batch(viewer =>
  77. Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", null)));
  78. }
  79. }
  80. [Fact]
  81. public void AfterRollbackCannotSeeChangesEvenInSameTxId()
  82. {
  83. var transactionInformation = new TransactionInformation
  84. {
  85. Id = Guid.NewGuid(),
  86. Timeout = TimeSpan.FromDays(7)
  87. };
  88. using (var tx = NewTransactionalStorage())
  89. {
  90. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  91. transactionInformation));
  92. tx.Batch(viewer =>
  93. Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", transactionInformation)));
  94. tx.Batch(mutator => mutator.Transactions.RollbackTransaction(transactionInformation.Id));
  95. tx.Batch(viewer =>
  96. Assert.Null(viewer.Documents.DocumentByKey("Ayende", transactionInformation)));
  97. }
  98. }
  99. [Fact]
  100. public void AddingDocInTxAndReadingItInSameTx()
  101. {
  102. var transactionInformation = new TransactionInformation
  103. {
  104. Id = Guid.NewGuid(),
  105. Timeout = TimeSpan.FromDays(7)
  106. };
  107. using (var tx = NewTransactionalStorage())
  108. {
  109. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  110. transactionInformation));
  111. tx.Batch(viewer =>
  112. Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", transactionInformation)));
  113. }
  114. }
  115. [Fact]
  116. public void AddingDocInTxWithDifferentTxIdThrows()
  117. {
  118. var transactionInformation = new TransactionInformation
  119. {
  120. Id = Guid.NewGuid(),
  121. Timeout = TimeSpan.FromDays(7)
  122. };
  123. using (var tx = NewTransactionalStorage())
  124. {
  125. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  126. transactionInformation));
  127. tx.Batch(mutator =>
  128. Assert.Throws<ConcurrencyException>(
  129. () =>
  130. mutator.Transactions.AddDocumentInTransaction("Ayende", Guid.NewGuid(),
  131. RavenJObject.FromObject(new {Name = "Rahien"}),
  132. new RavenJObject(),
  133. new TransactionInformation
  134. {
  135. Id = Guid.NewGuid(),
  136. Timeout = TimeSpan.FromMinutes(1)
  137. })));
  138. }
  139. }
  140. [Fact]
  141. public void AddingDocInTxWhenItWasAddedInAnotherWillFail()
  142. {
  143. var transactionInformation = new TransactionInformation
  144. {
  145. Id = Guid.NewGuid(),
  146. Timeout = TimeSpan.FromDays(7)
  147. };
  148. using (var tx = NewTransactionalStorage())
  149. {
  150. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  151. transactionInformation));
  152. Assert.Throws<ConcurrencyException>(
  153. () =>
  154. tx.Batch(
  155. mutator =>
  156. mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }),
  157. new RavenJObject(),
  158. new TransactionInformation
  159. {
  160. Id = Guid.NewGuid(),
  161. Timeout = TimeSpan.FromDays(7)
  162. })));
  163. }
  164. }
  165. [Fact]
  166. public void AddingDocInTxWillReadOldValueOutsideIt()
  167. {
  168. var transactionInformation = new TransactionInformation
  169. {
  170. Id = Guid.NewGuid(),
  171. Timeout = TimeSpan.FromDays(7)
  172. };
  173. using (var tx = NewTransactionalStorage())
  174. {
  175. tx.Batch(mutator => mutator.Documents.AddDocument("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject()));
  176. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien2" }), new RavenJObject(),
  177. transactionInformation));
  178. tx.Batch(viewer =>
  179. {
  180. var doc = viewer.Documents.DocumentByKey("Ayende", null);
  181. Assert.Equal("Rahien", doc.DataAsJson.Value<string>("Name"));
  182. });
  183. }
  184. }
  185. [Fact]
  186. public void AddingDocumentInTxThenAddingWithoutTxThrows()
  187. {
  188. var transactionInformation = new TransactionInformation
  189. {
  190. Id = Guid.NewGuid(),
  191. Timeout = TimeSpan.FromDays(7)
  192. };
  193. using (var tx = NewTransactionalStorage())
  194. {
  195. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject(),
  196. transactionInformation));
  197. tx.Batch(mutator =>
  198. Assert.Throws<ConcurrencyException>(
  199. () =>
  200. mutator.Documents.AddDocument("Ayende", Guid.NewGuid(),
  201. RavenJObject.FromObject(new { Name = "Rahien" }),
  202. new RavenJObject())));
  203. }
  204. }
  205. [Fact]
  206. public void CanDeleteDocumentInTransaction()
  207. {
  208. var transactionInformation = new TransactionInformation
  209. {
  210. Id = Guid.NewGuid(),
  211. Timeout = TimeSpan.FromDays(7)
  212. };
  213. using (var tx = NewTransactionalStorage())
  214. {
  215. tx.Batch(mutator => mutator.Documents.AddDocument("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien" }), new RavenJObject()));
  216. tx.Batch(mutator => mutator.Transactions.DeleteDocumentInTransaction(transactionInformation, "Ayende", null));
  217. tx.Batch(viewer =>
  218. {
  219. Assert.NotNull(viewer.Documents.DocumentByKey("Ayende", null));
  220. Assert.Null(viewer.Documents.DocumentByKey("Ayende", transactionInformation));
  221. });
  222. }
  223. }
  224. [Fact]
  225. public void AddingDocumentInTxThenAddingWithoutTxAfterTxExpiredWorks()
  226. {
  227. var transactionInformation = new TransactionInformation
  228. {
  229. Id = Guid.NewGuid(),
  230. Timeout = TimeSpan.FromDays(-7)
  231. };
  232. using (var tx = NewTransactionalStorage())
  233. {
  234. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, RavenJObject.FromObject(new { Name = "Rahien1" }), new RavenJObject(),
  235. transactionInformation));
  236. tx.Batch(mutator => mutator.Documents.AddDocument("Ayende", null,
  237. RavenJObject.FromObject(new { Name = "Rahien2" }),
  238. new RavenJObject()));
  239. tx.Batch(viewer =>
  240. {
  241. var doc = viewer.Documents.DocumentByKey("Ayende", transactionInformation);
  242. Assert.Equal("Rahien2", doc.DataAsJson.Value<string>("Name"));
  243. });
  244. }
  245. }
  246. [Fact]
  247. public void CanGetTxIdValues()
  248. {
  249. using (var tx = NewTransactionalStorage())
  250. {
  251. var txId = Guid.NewGuid();
  252. tx.Batch(mutator => mutator.Transactions.AddDocumentInTransaction("Ayende", null, new RavenJObject(), new RavenJObject(), new TransactionInformation
  253. {
  254. Id = txId,
  255. Timeout = TimeSpan.FromDays(7)
  256. }));
  257. tx.Batch(viewer =>
  258. Assert.Equal(new[] { txId }, viewer.Transactions.GetTransactionIds().ToArray()));
  259. }
  260. }
  261. }
  262. }