EnlistTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //
  2. // Tests for combination of volatile & durable resource manangers
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. using System;
  10. using System.Transactions;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Transactions {
  13. [TestFixture]
  14. public class EnlistTest {
  15. #region Vol1_Dur0
  16. /* Single volatile resource, SPC happens */
  17. [Test]
  18. public void Vol1_Dur0 ()
  19. {
  20. IntResourceManager irm = new IntResourceManager (1);
  21. irm.UseSingle = true;
  22. using (TransactionScope scope = new TransactionScope ()) {
  23. irm.Value = 2;
  24. scope.Complete ();
  25. }
  26. irm.CheckSPC ("irm");
  27. }
  28. [Test]
  29. public void Vol1_Dur0_2PC ()
  30. {
  31. IntResourceManager irm = new IntResourceManager (1);
  32. using (TransactionScope scope = new TransactionScope ()) {
  33. irm.Value = 2;
  34. scope.Complete ();
  35. }
  36. irm.Check2PC ("irm");
  37. }
  38. /* Single volatile resource, SPC happens */
  39. [Test]
  40. public void Vol1_Dur0_Fail1 ()
  41. {
  42. IntResourceManager irm = new IntResourceManager (1);
  43. irm.UseSingle = true;
  44. using (TransactionScope scope = new TransactionScope ()) {
  45. irm.Value = 2;
  46. /* Not completing this..
  47. scope.Complete ();*/
  48. }
  49. irm.Check ( 0, 0, 0, 1, 0, "irm" );
  50. }
  51. [Test]
  52. [ExpectedException ( typeof ( TransactionAbortedException ) )]
  53. public void Vol1_Dur0_Fail2 ()
  54. {
  55. IntResourceManager irm = new IntResourceManager (1);
  56. irm.FailPrepare = true;
  57. using (TransactionScope scope = new TransactionScope ()) {
  58. irm.Value = 2;
  59. scope.Complete ();
  60. }
  61. }
  62. [Test]
  63. [ExpectedException ( typeof ( TransactionAbortedException ) )]
  64. public void Vol1_Dur0_Fail3 ()
  65. {
  66. IntResourceManager irm = new IntResourceManager (1);
  67. irm.UseSingle = true;
  68. irm.FailSPC = true;
  69. using (TransactionScope scope = new TransactionScope ()) {
  70. irm.Value = 2;
  71. scope.Complete ();
  72. }
  73. }
  74. #endregion
  75. #region Vol2_Dur0
  76. /* >1 volatile, 2PC */
  77. [Test]
  78. public void Vol2_Dur0_SPC ()
  79. {
  80. IntResourceManager irm = new IntResourceManager (1);
  81. IntResourceManager irm2 = new IntResourceManager (3);
  82. irm.UseSingle = true;
  83. irm2.UseSingle = true;
  84. using (TransactionScope scope = new TransactionScope ()) {
  85. irm.Value = 2;
  86. irm2.Value = 6;
  87. scope.Complete ();
  88. }
  89. irm.Check2PC ( "irm" );
  90. irm2.Check2PC ( "irm2" );
  91. }
  92. #endregion
  93. #region Vol0_Dur1
  94. /* 1 durable */
  95. [Test]
  96. public void Vol0_Dur1 ()
  97. {
  98. IntResourceManager irm = new IntResourceManager (1);
  99. irm.Volatile = false;
  100. irm.UseSingle = true;
  101. using (TransactionScope scope = new TransactionScope ()) {
  102. irm.Value = 2;
  103. scope.Complete ();
  104. }
  105. irm.CheckSPC ( "irm" );
  106. }
  107. /* We support only 1 durable with 2PC
  108. * On .net, this becomes a distributed transaction
  109. */
  110. [Test]
  111. [Category ("NotWorking")]
  112. public void Vol0_Dur1_2PC ()
  113. {
  114. IntResourceManager irm = new IntResourceManager (1);
  115. /* Durable resource enlisted with a IEnlistedNotification
  116. * object
  117. */
  118. irm.Volatile = false;
  119. using (TransactionScope scope = new TransactionScope ()) {
  120. irm.Value = 2;
  121. scope.Complete ();
  122. }
  123. }
  124. [Test]
  125. public void Vol0_Dur1_Fail ()
  126. {
  127. IntResourceManager irm = new IntResourceManager ( 1 );
  128. /* Durable resource enlisted with a IEnlistedNotification
  129. * object
  130. */
  131. irm.Volatile = false;
  132. irm.FailSPC = true;
  133. irm.UseSingle = true;
  134. try {
  135. using (TransactionScope scope = new TransactionScope ()) {
  136. irm.Value = 2;
  137. scope.Complete ();
  138. }
  139. }
  140. catch (TransactionAbortedException) {
  141. irm.Check ( 1, 0, 0, 0, 0, "irm" );
  142. return;
  143. }
  144. Assert.Fail ();
  145. }
  146. #endregion
  147. #region Vol2_Dur1
  148. /* >1vol + 1 durable */
  149. [Test]
  150. public void Vol2_Dur1 ()
  151. {
  152. IntResourceManager [] irm = new IntResourceManager [4];
  153. irm [0] = new IntResourceManager ( 1 );
  154. irm [1] = new IntResourceManager ( 3 );
  155. irm [2] = new IntResourceManager ( 5 );
  156. irm [3] = new IntResourceManager ( 7 );
  157. irm [0].Volatile = false;
  158. for ( int i = 0; i < 4; i++ )
  159. irm [i].UseSingle = true;
  160. using (TransactionScope scope = new TransactionScope ()) {
  161. irm [0].Value = 2;
  162. irm [1].Value = 6;
  163. irm [2].Value = 10;
  164. irm [3].Value = 14;
  165. scope.Complete ();
  166. }
  167. irm [0].CheckSPC ( "irm [0]" );
  168. /* Volatile RMs get 2PC */
  169. for (int i = 1; i < 4; i++)
  170. irm [i].Check2PC ( "irm [" + i + "]" );
  171. }
  172. /* >1vol + 1 durable
  173. * Durable fails SPC
  174. */
  175. [Test]
  176. public void Vol2_Dur1_Fail1 ()
  177. {
  178. IntResourceManager [] irm = new IntResourceManager [4];
  179. irm [0] = new IntResourceManager (1);
  180. irm [1] = new IntResourceManager (3);
  181. irm [2] = new IntResourceManager (5);
  182. irm [3] = new IntResourceManager (7);
  183. irm [0].Volatile = false;
  184. irm [0].FailSPC = true;
  185. for ( int i = 0; i < 4; i++ )
  186. irm [i].UseSingle = true;
  187. /* Durable RM irm[0] does Abort on SPC, so
  188. * all volatile RMs get Rollback */
  189. try {
  190. using (TransactionScope scope = new TransactionScope ()) {
  191. irm [0].Value = 2;
  192. irm [1].Value = 6;
  193. irm [2].Value = 10;
  194. irm [3].Value = 14;
  195. scope.Complete ();
  196. }
  197. }
  198. catch (TransactionAbortedException) {
  199. irm [0].CheckSPC ( "irm [0]" );
  200. /* Volatile RMs get 2PC Prepare, and then get rolled back */
  201. for (int i = 1; i < 4; i++)
  202. irm [i].Check ( 0, 1, 0, 1, 0, "irm [" + i + "]" );
  203. }
  204. }
  205. /* >1vol + 1 durable
  206. * durable doesn't complete SPC
  207. */
  208. [Test]
  209. [Ignore ( "Correct this test, it should throw TimeOutException or something" )]
  210. public void Vol2_Dur1_Fail2 ()
  211. {
  212. IntResourceManager [] irm = new IntResourceManager [4];
  213. irm [0] = new IntResourceManager (1);
  214. irm [1] = new IntResourceManager (3);
  215. irm [2] = new IntResourceManager (5);
  216. irm [3] = new IntResourceManager (7);
  217. irm [0].Volatile = false;
  218. irm [0].IgnoreSPC = true;
  219. for ( int i = 0; i < 4; i++ )
  220. irm [i].UseSingle = true;
  221. /* Durable RM irm[2] does on SPC, so
  222. * all volatile RMs get Rollback */
  223. try {
  224. using (TransactionScope scope = new TransactionScope ( TransactionScopeOption.Required, new TimeSpan ( 0, 0, 5 ) )) {
  225. irm [0].Value = 2;
  226. irm [1].Value = 6;
  227. irm [2].Value = 10;
  228. irm [3].Value = 14;
  229. scope.Complete ();
  230. }
  231. }
  232. catch (TransactionAbortedException) {
  233. irm [0].CheckSPC ( "irm [0]" );
  234. /* Volatile RMs get 2PC Prepare, and then get rolled back */
  235. for (int i = 1; i < 4; i++)
  236. irm [i].Check ( 0, 1, 0, 1, 0, "irm [" + i + "]" );
  237. }
  238. }
  239. /* >1vol + 1 durable
  240. * Volatile fails Prepare
  241. */
  242. [Test]
  243. public void Vol2_Dur1_Fail3 ()
  244. {
  245. IntResourceManager [] irm = new IntResourceManager [4];
  246. irm [0] = new IntResourceManager ( 1 );
  247. irm [1] = new IntResourceManager ( 3 );
  248. irm [2] = new IntResourceManager ( 5 );
  249. irm [3] = new IntResourceManager ( 7 );
  250. irm [0].Volatile = false;
  251. irm [2].FailPrepare = true;
  252. for ( int i = 0; i < 4; i++ )
  253. irm [i].UseSingle = true;
  254. /* Durable RM irm[2] does on SPC, so
  255. * all volatile RMs get Rollback */
  256. try {
  257. using (TransactionScope scope = new TransactionScope ()) {
  258. irm [0].Value = 2;
  259. irm [1].Value = 6;
  260. irm [2].Value = 10;
  261. irm [3].Value = 14;
  262. scope.Complete ();
  263. }
  264. }
  265. catch (TransactionAbortedException) {
  266. irm [0].Check ( 0, 0, 0, 1, 0, "irm [0]");
  267. /* irm [1] & [2] get prepare,
  268. * [2] -> ForceRollback,
  269. * [1] & [3] get rollback,
  270. * [0](durable) gets rollback */
  271. irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
  272. irm [2].Check ( 0, 1, 0, 0, 0, "irm [2]" );
  273. irm [3].Check ( 0, 0, 0, 1, 0, "irm [3]" );
  274. return;
  275. }
  276. Assert.Fail ( "Expected TransactionAbortedException" );
  277. }
  278. [Test]
  279. public void Vol2_Dur1_Fail4 ()
  280. {
  281. IntResourceManager [] irm = new IntResourceManager [2];
  282. irm [0] = new IntResourceManager ( 1 );
  283. irm [1] = new IntResourceManager ( 3 );
  284. irm [0].Volatile = false;
  285. irm [0].FailSPC = true;
  286. irm [0].FailWithException = true;
  287. for ( int i = 0; i < 2; i++ )
  288. irm [i].UseSingle = true;
  289. /* Durable RM irm[2] does on SPC, so
  290. * all volatile RMs get Rollback */
  291. try {
  292. using ( TransactionScope scope = new TransactionScope () ) {
  293. irm [0].Value = 2;
  294. irm [1].Value = 6;
  295. scope.Complete ();
  296. }
  297. }
  298. catch ( TransactionAbortedException e) {
  299. Assert.IsNotNull ( e.InnerException, "Expected e.InnerException == NotSupportedException, but got None");
  300. Assert.AreEqual ( typeof ( NotSupportedException ), e.InnerException.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e.GetType () );
  301. irm [0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
  302. irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
  303. return;
  304. }
  305. Assert.Fail ( "Expected TransactionAbortedException" );
  306. }
  307. [Test]
  308. public void Vol2_Dur1_Fail5 ()
  309. {
  310. CommittableTransaction ct = new CommittableTransaction ();
  311. IntResourceManager [] irm = new IntResourceManager [2];
  312. irm [0] = new IntResourceManager ( 1 );
  313. irm [1] = new IntResourceManager ( 3 );
  314. Transaction.Current = ct;
  315. irm [0].Volatile = false;
  316. irm [0].FailSPC = true;
  317. irm [0].FailWithException = true;
  318. for ( int i = 0; i < 2; i++ )
  319. irm [i].UseSingle = true;
  320. /* Durable RM irm[2] does on SPC, so
  321. * all volatile RMs get Rollback */
  322. using ( TransactionScope scope = new TransactionScope () ) {
  323. irm [0].Value = 2;
  324. irm [1].Value = 6;
  325. scope.Complete ();
  326. }
  327. try {
  328. ct.Commit ();
  329. }
  330. catch ( TransactionAbortedException e ) {
  331. Assert.IsNotNull ( e.InnerException, "Expected e.InnerException == NotSupportedException, but got None" );
  332. Assert.AreEqual ( typeof ( NotSupportedException ), e.InnerException.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e.GetType () );
  333. irm [0].Check ( 1, 0, 0, 0, 0, "irm [0]" );
  334. irm [1].Check ( 0, 1, 0, 1, 0, "irm [1]" );
  335. try {
  336. ct.Commit ();
  337. }
  338. catch (InvalidOperationException x ) {
  339. Assert.IsNull ( x.InnerException);
  340. Transaction.Current = null;
  341. return;
  342. }
  343. Assert.Fail ( "Should not be reached" );
  344. }
  345. Assert.Fail ( "Expected TransactionAbortedException" );
  346. }
  347. #endregion
  348. #region Others
  349. /* >1vol
  350. * > 1 durable, On .net this becomes a distributed transaction
  351. * We don't support this in mono yet.
  352. */
  353. [Test]
  354. [Category ("NotWorking")]
  355. public void Vol0_Dur2 ()
  356. {
  357. IntResourceManager [] irm = new IntResourceManager [2];
  358. irm [0] = new IntResourceManager ( 1 );
  359. irm [1] = new IntResourceManager ( 3 );
  360. irm [0].Volatile = false;
  361. irm [1].Volatile = false;
  362. for ( int i = 0; i < 2; i++ )
  363. irm [i].UseSingle = true;
  364. using (TransactionScope scope = new TransactionScope ()) {
  365. irm [0].Value = 2;
  366. irm [1].Value = 6;
  367. scope.Complete ();
  368. }
  369. }
  370. #endregion
  371. }
  372. }