TransactionScopeTest.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. //
  2. // Unit tests for TransactionScope and Implicit/Explicit use of
  3. // Transactions
  4. //
  5. // Author:
  6. // Ankit Jain <[email protected]>
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  9. //
  10. using System;
  11. using NUnit.Framework;
  12. using System.Transactions;
  13. namespace MonoTests.System.Transactions
  14. {
  15. [TestFixture]
  16. public class TransactionScopeTest
  17. {
  18. [Test]
  19. public void TransactionScopeCommit ()
  20. {
  21. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  22. using (TransactionScope scope = new TransactionScope ()) {
  23. Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
  24. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status);
  25. scope.Complete ();
  26. }
  27. Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
  28. }
  29. [Test]
  30. public void TransactionScopeAbort ()
  31. {
  32. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  33. IntResourceManager irm = new IntResourceManager (1);
  34. using (TransactionScope scope = new TransactionScope ()) {
  35. Assert.IsNotNull (Transaction.Current, "Ambient transaction does not exist");
  36. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "transaction is not active");
  37. irm.Value = 2;
  38. /* Not completing scope here */
  39. }
  40. irm.Check ( 0, 0, 1, 0, "irm");
  41. Assert.AreEqual (1, irm.Value);
  42. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  43. }
  44. [Test]
  45. [ExpectedException (typeof (InvalidOperationException))]
  46. public void TransactionScopeCompleted1 ()
  47. {
  48. using (TransactionScope scope = new TransactionScope ()) {
  49. scope.Complete ();
  50. /* Can't access ambient transaction after scope.Complete */
  51. TransactionStatus status = Transaction.Current.TransactionInformation.Status;
  52. }
  53. }
  54. [Test]
  55. [ExpectedException (typeof (InvalidOperationException))]
  56. public void TransactionScopeCompleted2 ()
  57. {
  58. using (TransactionScope scope = new TransactionScope ()) {
  59. scope.Complete ();
  60. Transaction.Current = Transaction.Current;
  61. }
  62. }
  63. [Test]
  64. [ExpectedException (typeof (InvalidOperationException))]
  65. public void TransactionScopeCompleted3 ()
  66. {
  67. using (TransactionScope scope = new TransactionScope ()) {
  68. scope.Complete ();
  69. scope.Complete ();
  70. }
  71. }
  72. #region NestedTransactionScope tests
  73. [Test]
  74. public void NestedTransactionScope1 ()
  75. {
  76. IntResourceManager irm = new IntResourceManager (1);
  77. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  78. using (TransactionScope scope = new TransactionScope ()) {
  79. irm.Value = 2;
  80. /* Complete this scope */
  81. scope.Complete ();
  82. }
  83. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  84. /* Value = 2, got committed */
  85. Assert.AreEqual (irm.Value, 2, "#1");
  86. irm.Check ( 1, 1, 0, 0, "irm" );
  87. }
  88. [Test]
  89. public void NestedTransactionScope2 ()
  90. {
  91. IntResourceManager irm = new IntResourceManager (1);
  92. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  93. using (TransactionScope scope = new TransactionScope ()) {
  94. irm.Value = 2;
  95. /* Not-Completing this scope */
  96. }
  97. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  98. /* Value = 2, got rolledback */
  99. Assert.AreEqual (irm.Value, 1, "#2");
  100. irm.Check ( 0, 0, 1, 0, "irm" );
  101. }
  102. [Test]
  103. public void NestedTransactionScope3 ()
  104. {
  105. IntResourceManager irm = new IntResourceManager (1);
  106. IntResourceManager irm2 = new IntResourceManager (10);
  107. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  108. using (TransactionScope scope = new TransactionScope ()) {
  109. irm.Value = 2;
  110. using (TransactionScope scope2 = new TransactionScope ()) {
  111. irm2.Value = 20;
  112. scope2.Complete ();
  113. }
  114. scope.Complete ();
  115. }
  116. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  117. /* Both got committed */
  118. Assert.AreEqual (irm.Value, 2, "#3");
  119. Assert.AreEqual (irm2.Value, 20, "#4");
  120. irm.Check ( 1, 1, 0, 0, "irm" );
  121. irm2.Check ( 1, 1, 0, 0, "irm2" );
  122. }
  123. [Test]
  124. public void NestedTransactionScope4 ()
  125. {
  126. IntResourceManager irm = new IntResourceManager (1);
  127. IntResourceManager irm2 = new IntResourceManager (10);
  128. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  129. using (TransactionScope scope = new TransactionScope ()) {
  130. irm.Value = 2;
  131. using (TransactionScope scope2 = new TransactionScope ()) {
  132. irm2.Value = 20;
  133. /* Inner Tx not completed, Tx should get rolled back */
  134. //scope2.Complete();
  135. }
  136. /* Both rolledback */
  137. irm.Check ( 0, 0, 1, 0, "irm" );
  138. irm2.Check ( 0, 0, 1, 0, "irm2" );
  139. Assert.AreEqual (TransactionStatus.Aborted, Transaction.Current.TransactionInformation.Status, "#5");
  140. //scope.Complete ();
  141. }
  142. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  143. Assert.AreEqual (irm.Value, 1, "#6");
  144. Assert.AreEqual (irm2.Value, 10, "#7");
  145. irm.Check ( 0, 0, 1, 0, "irm" );
  146. }
  147. [Test]
  148. public void NestedTransactionScope5 ()
  149. {
  150. IntResourceManager irm = new IntResourceManager (1);
  151. IntResourceManager irm2 = new IntResourceManager (10);
  152. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  153. using (TransactionScope scope = new TransactionScope ()) {
  154. irm.Value = 2;
  155. using (TransactionScope scope2 = new TransactionScope ()) {
  156. irm2.Value = 20;
  157. scope2.Complete ();
  158. }
  159. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#8");
  160. /* Not completing outer scope
  161. scope.Complete (); */
  162. }
  163. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  164. Assert.AreEqual (irm.Value, 1, "#9");
  165. Assert.AreEqual (irm2.Value, 10, "#10");
  166. irm.Check ( 0, 0, 1, 0, "irm" );
  167. irm2.Check ( 0, 0, 1, 0, "irm2" );
  168. }
  169. [Test]
  170. public void NestedTransactionScope6 ()
  171. {
  172. IntResourceManager irm = new IntResourceManager (1);
  173. IntResourceManager irm2 = new IntResourceManager (10);
  174. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  175. using (TransactionScope scope = new TransactionScope ()) {
  176. irm.Value = 2;
  177. using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
  178. irm2.Value = 20;
  179. scope2.Complete ();
  180. }
  181. /* vr2, committed */
  182. irm2.Check ( 1, 1, 0, 0, "irm2" );
  183. Assert.AreEqual (irm2.Value, 20);
  184. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#11");
  185. scope.Complete ();
  186. }
  187. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  188. Assert.AreEqual (irm.Value, 2, "#12");
  189. irm.Check ( 1, 1, 0, 0, "irm" );
  190. }
  191. [Test]
  192. public void NestedTransactionScope7 ()
  193. {
  194. IntResourceManager irm = new IntResourceManager (1);
  195. IntResourceManager irm2 = new IntResourceManager (10);
  196. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  197. using (TransactionScope scope = new TransactionScope ()) {
  198. irm.Value = 2;
  199. using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
  200. irm2.Value = 20;
  201. /* Not completing
  202. scope2.Complete();*/
  203. }
  204. /* irm2, rolled back*/
  205. irm2.Check ( 0, 0, 1, 0, "irm2" );
  206. Assert.AreEqual (irm2.Value, 10, "#13");
  207. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#14");
  208. scope.Complete ();
  209. }
  210. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  211. /* ..But irm got committed */
  212. Assert.AreEqual (irm.Value, 2, "#15");
  213. irm.Check ( 1, 1, 0, 0, "irm" );
  214. }
  215. [Test]
  216. public void NestedTransactionScope8 ()
  217. {
  218. IntResourceManager irm = new IntResourceManager (1);
  219. IntResourceManager irm2 = new IntResourceManager (10);
  220. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  221. using (TransactionScope scope = new TransactionScope ()) {
  222. irm.Value = 2;
  223. using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
  224. /* Not transactional, so this WONT get committed */
  225. irm2.Value = 20;
  226. scope2.Complete ();
  227. }
  228. irm2.Check ( 0, 0, 0, 0, "irm2" );
  229. Assert.AreEqual (20, irm2.Value, "#16");
  230. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#17");
  231. scope.Complete ();
  232. }
  233. Assert.IsNull (Transaction.Current, "Ambient transaction exists");
  234. Assert.AreEqual (irm.Value, 2, "#18");
  235. irm.Check ( 1, 1, 0, 0, "irm" );
  236. }
  237. [Test]
  238. public void NestedTransactionScope8a ()
  239. {
  240. IntResourceManager irm = new IntResourceManager ( 1 );
  241. IntResourceManager irm2 = new IntResourceManager ( 10 );
  242. Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
  243. using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Suppress )) {
  244. irm.Value = 2;
  245. using (TransactionScope scope2 = new TransactionScope ()) {
  246. irm2.Value = 20;
  247. scope2.Complete ();
  248. }
  249. irm2.Check ( 1, 1, 0, 0, "irm2" );
  250. Assert.AreEqual ( 20, irm2.Value, "#16a" );
  251. scope.Complete ();
  252. }
  253. Assert.IsNull ( Transaction.Current, "Ambient transaction exists" );
  254. Assert.AreEqual ( 2, irm.Value, "#18a" );
  255. irm.Check ( 0, 0, 0, 0, "irm" );
  256. }
  257. [Test]
  258. public void NestedTransactionScope9 ()
  259. {
  260. IntResourceManager irm = new IntResourceManager (1);
  261. IntResourceManager irm2 = new IntResourceManager (10);
  262. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  263. using (TransactionScope scope = new TransactionScope ()) {
  264. irm.Value = 2;
  265. using (TransactionScope scope2 = new TransactionScope (TransactionScopeOption.Suppress)) {
  266. /* Not transactional, so this WONT get committed */
  267. irm2.Value = 4;
  268. scope2.Complete ();
  269. }
  270. irm2.Check ( 0, 0, 0, 0, "irm2" );
  271. using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
  272. irm.Value = 6;
  273. scope3.Complete ();
  274. }
  275. /* vr's value has changed as the inner scope committed = 6 */
  276. irm.Check ( 1, 1, 0, 0, "irm" );
  277. Assert.AreEqual (irm.Value, 6, "#19");
  278. Assert.AreEqual (irm.Actual, 6, "#20");
  279. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#21");
  280. scope.Complete ();
  281. }
  282. Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
  283. Assert.AreEqual (irm.Value, 6, "#22");
  284. irm.Check ( 2, 2, 0, 0, "irm" );
  285. }
  286. [Test]
  287. [ExpectedException (typeof (TransactionAbortedException))]
  288. public void NestedTransactionScope10 ()
  289. {
  290. IntResourceManager irm = new IntResourceManager (1);
  291. bool failed = false;
  292. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  293. using (TransactionScope scope = new TransactionScope ()) {
  294. irm.Value = 2;
  295. using (TransactionScope scope2 = new TransactionScope ()) {
  296. irm.Value = 4;
  297. /* Not completing this, so the transaction will
  298. * get aborted
  299. scope2.Complete (); */
  300. }
  301. using (TransactionScope scope3 = new TransactionScope ()) {
  302. /* Aborted transaction cannot be used for another
  303. * TransactionScope
  304. */
  305. //Assert.Fail ("Should not reach here.");
  306. failed = true;
  307. }
  308. }
  309. Assert.IsFalse ( failed, "Aborted Tx cannot be used for another TransactionScope" );
  310. }
  311. [Test]
  312. public void NestedTransactionScope12 ()
  313. {
  314. IntResourceManager irm = new IntResourceManager (1);
  315. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  316. using (TransactionScope scope = new TransactionScope ()) {
  317. irm.Value = 2;
  318. using (TransactionScope scope2 = new TransactionScope ()) {
  319. irm.Value = 4;
  320. /* Not completing this, so the transaction will
  321. * get aborted
  322. scope2.Complete (); */
  323. }
  324. using (TransactionScope scope3 = new TransactionScope (TransactionScopeOption.RequiresNew)) {
  325. /* Using RequiresNew here, so outer transaction
  326. * being aborted doesn't matter
  327. */
  328. scope3.Complete ();
  329. }
  330. }
  331. }
  332. [Test]
  333. [ExpectedException (typeof (TransactionAbortedException))]
  334. public void NestedTransactionScope13 ()
  335. {
  336. IntResourceManager irm = new IntResourceManager ( 1 );
  337. Assert.IsNull ( Transaction.Current, "Ambient transaction exists (before)" );
  338. using ( TransactionScope scope = new TransactionScope () ) {
  339. irm.Value = 2;
  340. using ( TransactionScope scope2 = new TransactionScope () ) {
  341. irm.Value = 4;
  342. /* Not completing this, so the transaction will
  343. * get aborted
  344. scope2.Complete (); */
  345. }
  346. scope.Complete ();
  347. }
  348. }
  349. #endregion
  350. /* Tests using IntResourceManager */
  351. [Test]
  352. public void RMFail1 ()
  353. {
  354. IntResourceManager irm = new IntResourceManager (1);
  355. IntResourceManager irm2 = new IntResourceManager (10);
  356. IntResourceManager irm3 = new IntResourceManager (12);
  357. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  358. try {
  359. using (TransactionScope scope = new TransactionScope ()) {
  360. irm.Value = 2;
  361. irm2.Value = 20;
  362. irm3.Value = 24;
  363. /* Make second RM fail to prepare, this should throw
  364. * TransactionAbortedException when the scope ends
  365. */
  366. irm2.FailPrepare = true;
  367. scope.Complete ();
  368. }
  369. } catch (TransactionAbortedException) {
  370. irm.Check ( 1, 0, 1, 0, "irm");
  371. irm2.Check ( 1, 0, 0, 0, "irm2");
  372. irm3.Check ( 0, 0, 1, 0, "irm3");
  373. }
  374. Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
  375. }
  376. [Test]
  377. [Category("NotWorking")]
  378. [Ignore("NotWorking")]
  379. public void RMFail2 ()
  380. {
  381. IntResourceManager irm = new IntResourceManager (1);
  382. IntResourceManager irm2 = new IntResourceManager (10);
  383. IntResourceManager irm3 = new IntResourceManager (12);
  384. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  385. try {
  386. using (TransactionScope scope = new TransactionScope (TransactionScopeOption.Required, new TimeSpan (0, 0, 30))) {
  387. irm.Value = 2;
  388. irm2.Value = 20;
  389. irm3.Value = 24;
  390. /* irm2 wont call Prepared or ForceRollback in
  391. * its Prepare (), so TransactionManager will timeout
  392. * waiting for it
  393. */
  394. irm2.IgnorePrepare = true;
  395. scope.Complete ();
  396. }
  397. } catch (TransactionAbortedException e) {
  398. /* FIXME: Not working right now.. no timeout exception thrown! */
  399. Assert.IsNotNull ( e.InnerException, "innerexception is null" );
  400. Assert.AreEqual (typeof (TimeoutException), e.InnerException.GetType (), "#32");
  401. Assert.IsNull (Transaction.Current, "Ambient transaction exists (after)");
  402. return;
  403. }
  404. Assert.Fail ("Expected TransactionAbortedException (TimeoutException)");
  405. }
  406. #region Explicit Transaction Tests
  407. [Test]
  408. public void ExplicitTransactionCommit ()
  409. {
  410. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  411. CommittableTransaction ct = new CommittableTransaction ();
  412. Transaction oldTransaction = Transaction.Current;
  413. Transaction.Current = ct;
  414. IntResourceManager irm = new IntResourceManager (1);
  415. irm.Value = 2;
  416. ct.Commit ();
  417. Assert.AreEqual (2, irm.Value, "#33");
  418. Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
  419. Transaction.Current = oldTransaction;
  420. }
  421. [Test]
  422. public void ExplicitTransactionRollback ()
  423. {
  424. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  425. CommittableTransaction ct = new CommittableTransaction ();
  426. Transaction oldTransaction = Transaction.Current;
  427. Transaction.Current = ct;
  428. IntResourceManager irm = new IntResourceManager (1);
  429. irm.Value = 2;
  430. Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#35");
  431. ct.Rollback ();
  432. Assert.AreEqual (1, irm.Value, "#36");
  433. Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#37");
  434. Transaction.Current = oldTransaction;
  435. }
  436. [Test]
  437. public void ExplicitTransaction1 ()
  438. {
  439. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  440. CommittableTransaction ct = new CommittableTransaction ();
  441. Transaction oldTransaction = Transaction.Current;
  442. Transaction.Current = ct;
  443. IntResourceManager irm = new IntResourceManager (1);
  444. irm.Value = 2;
  445. using (TransactionScope scope = new TransactionScope ()) {
  446. Assert.AreEqual (ct, Transaction.Current, "#38");
  447. irm.Value = 4;
  448. scope.Complete ();
  449. }
  450. Assert.AreEqual (ct, Transaction.Current, "#39");
  451. Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
  452. Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
  453. ct.Commit ();
  454. Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
  455. Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
  456. Transaction.Current = oldTransaction;
  457. }
  458. [Test]
  459. public void ExplicitTransaction2 ()
  460. {
  461. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  462. CommittableTransaction ct = new CommittableTransaction ();
  463. Transaction oldTransaction = Transaction.Current;
  464. Transaction.Current = ct;
  465. IntResourceManager irm = new IntResourceManager (1);
  466. irm.Value = 2;
  467. using (TransactionScope scope = new TransactionScope ()) {
  468. Assert.AreEqual (ct, Transaction.Current, "#44");
  469. /* Not calling scope.Complete
  470. scope.Complete ();*/
  471. }
  472. Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
  473. Assert.AreEqual (ct, Transaction.Current, "#46");
  474. Assert.AreEqual (1, irm.Actual, "#47");
  475. Assert.AreEqual (1, irm.NumRollback, "#48");
  476. irm.Check ( 0, 0, 1, 0, "irm" );
  477. Transaction.Current = oldTransaction;
  478. try {
  479. ct.Commit ();
  480. } catch (TransactionAbortedException) {
  481. return;
  482. }
  483. Assert.Fail ("Commit on an aborted transaction should fail");
  484. }
  485. [Test]
  486. public void ExplicitTransaction3 ()
  487. {
  488. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  489. CommittableTransaction ct = new CommittableTransaction ();
  490. Transaction oldTransaction = Transaction.Current;
  491. Transaction.Current = ct;
  492. IntResourceManager irm = new IntResourceManager (1);
  493. using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
  494. Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
  495. irm.Value = 3;
  496. scope.Complete ();
  497. }
  498. irm.Value = 2;
  499. Assert.AreEqual (3, irm.Actual, "#50");
  500. Assert.AreEqual (ct, Transaction.Current, "#51");
  501. ct.Commit ();
  502. Assert.AreEqual (2, irm.Actual, "#52");
  503. Transaction.Current = oldTransaction;
  504. }
  505. [Test]
  506. public void ExplicitTransaction4 ()
  507. {
  508. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  509. CommittableTransaction ct = new CommittableTransaction ();
  510. Transaction oldTransaction = Transaction.Current;
  511. /* Not setting ambient transaction
  512. Transaction.Current = ct;
  513. */
  514. IntResourceManager irm = new IntResourceManager (1);
  515. using (TransactionScope scope = new TransactionScope (ct)) {
  516. Assert.AreEqual (ct, Transaction.Current, "#53");
  517. irm.Value = 2;
  518. scope.Complete ();
  519. }
  520. Assert.AreEqual (oldTransaction, Transaction.Current, "#54");
  521. Assert.AreEqual (TransactionStatus.Active, ct.TransactionInformation.Status, "#55");
  522. Assert.AreEqual (1, irm.Actual, "#56"); /* Actual value */
  523. ct.Commit ();
  524. Assert.AreEqual (2, irm.Actual, "#57"); /* New committed actual value */
  525. Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#58");
  526. irm.Check ( 1, 1, 0, 0, "irm");
  527. }
  528. [Test]
  529. public void ExplicitTransaction5 ()
  530. {
  531. Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
  532. CommittableTransaction ct = new CommittableTransaction ();
  533. Transaction oldTransaction = Transaction.Current;
  534. /* Not setting ambient transaction
  535. Transaction.Current = ct;
  536. */
  537. IntResourceManager irm = new IntResourceManager (1);
  538. using (TransactionScope scope = new TransactionScope (ct)) {
  539. Assert.AreEqual (ct, Transaction.Current, "#59");
  540. irm.Value = 2;
  541. /* Not completing this scope
  542. scope.Complete (); */
  543. }
  544. Assert.AreEqual (oldTransaction, Transaction.Current, "#60");
  545. Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#61");
  546. Assert.AreEqual (1, irm.Actual, "#62"); /* Actual value */
  547. irm.Check ( 0, 0, 1, 0, "irm");
  548. }
  549. [Test]
  550. [ExpectedException (typeof (InvalidOperationException))]
  551. public void ExplicitTransaction6 ()
  552. {
  553. CommittableTransaction ct = new CommittableTransaction ();
  554. IntResourceManager irm = new IntResourceManager (1);
  555. irm.Value = 2;
  556. ct.Commit ();
  557. ct.Commit ();
  558. }
  559. [Test]
  560. [ExpectedException (typeof (InvalidOperationException))]
  561. public void ExplicitTransaction6a ()
  562. {
  563. CommittableTransaction ct = new CommittableTransaction ();
  564. IntResourceManager irm = new IntResourceManager ( 1 );
  565. irm.Value = 2;
  566. ct.Commit ();
  567. /* Using a already committed transaction in a new
  568. * TransactionScope
  569. */
  570. TransactionScope scope = new TransactionScope ( ct );
  571. }
  572. [Test]
  573. public void ExplicitTransaction6b ()
  574. {
  575. CommittableTransaction ct = new CommittableTransaction ();
  576. IntResourceManager irm = new IntResourceManager ( 1 );
  577. Transaction.Current = ct;
  578. TransactionScope scope1 = new TransactionScope ();
  579. /* Enlist */
  580. irm.Value = 2;
  581. scope1.Complete ();
  582. try {
  583. ct.Commit ();
  584. } catch (TransactionAbortedException) {
  585. irm.Check ( 0, 0, 1, 0, "irm" );
  586. scope1.Dispose ();
  587. Transaction.Current = null;
  588. return;
  589. }
  590. Assert.Fail ( "Commit should've failed" );
  591. }
  592. [Test]
  593. public void ExplicitTransaction6c ()
  594. {
  595. CommittableTransaction ct = new CommittableTransaction ();
  596. IntResourceManager irm = new IntResourceManager ( 1 );
  597. Transaction.Current = ct;
  598. TransactionScope scope1 = new TransactionScope (TransactionScopeOption.RequiresNew);
  599. /* Enlist */
  600. irm.Value = 2;
  601. TransactionScope scope2 = new TransactionScope ();
  602. try {
  603. scope1.Dispose ();
  604. } catch (InvalidOperationException) {
  605. /* Error: TransactionScope nested incorrectly */
  606. irm.Check ( 0, 0, 1, 0, "irm" );
  607. scope2.Dispose ();
  608. Transaction.Current = null;
  609. return;
  610. }
  611. Assert.Fail ("Commit should've failed");
  612. }
  613. [Test]
  614. public void ExplicitTransaction6d ()
  615. {
  616. CommittableTransaction ct = new CommittableTransaction ();
  617. IntResourceManager irm = new IntResourceManager ( 1 );
  618. Transaction.Current = ct;
  619. TransactionScope scope1 = new TransactionScope ();
  620. /* Enlist */
  621. irm.Value = 2;
  622. TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.RequiresNew );
  623. try {
  624. scope1.Dispose ();
  625. } catch (InvalidOperationException) {
  626. /* Error: TransactionScope nested incorrectly */
  627. scope2.Dispose ();
  628. Transaction.Current = null;
  629. return;
  630. }
  631. Assert.Fail ( "Commit should've failed" );
  632. }
  633. [Test]
  634. public void ExplicitTransaction6e ()
  635. {
  636. CommittableTransaction ct = new CommittableTransaction ();
  637. IntResourceManager irm = new IntResourceManager ( 1 );
  638. Transaction.Current = ct;
  639. TransactionScope scope1 = new TransactionScope ();
  640. /* Enlist */
  641. irm.Value = 2;
  642. TransactionScope scope2 = new TransactionScope ( TransactionScopeOption.Suppress);
  643. try {
  644. scope1.Dispose ();
  645. } catch (InvalidOperationException) {
  646. /* Error: TransactionScope nested incorrectly */
  647. scope2.Dispose ();
  648. Transaction.Current = null;
  649. return;
  650. }
  651. Assert.Fail ( "Commit should've failed" );
  652. }
  653. [Test]
  654. [ExpectedException (typeof (TransactionException))]
  655. public void ExplicitTransaction7 ()
  656. {
  657. CommittableTransaction ct = new CommittableTransaction ();
  658. IntResourceManager irm = new IntResourceManager (1);
  659. irm.Value = 2;
  660. ct.Commit ();
  661. /* Cannot accept any new work now, so TransactionException */
  662. ct.Rollback ();
  663. }
  664. [Test]
  665. public void ExplicitTransaction8 ()
  666. {
  667. CommittableTransaction ct = new CommittableTransaction ();
  668. IntResourceManager irm = new IntResourceManager ( 1 );
  669. using ( TransactionScope scope = new TransactionScope (ct) ) {
  670. irm.Value = 2;
  671. /* FIXME: Why TransactionAbortedException ?? */
  672. try {
  673. ct.Commit ();
  674. } catch ( TransactionAbortedException) {
  675. irm.Check ( 0, 0, 1, 0, "irm" );
  676. return;
  677. }
  678. Assert.Fail ( "Should not be reached" );
  679. }
  680. }
  681. [Test]
  682. public void ExplicitTransaction8a ()
  683. {
  684. CommittableTransaction ct = new CommittableTransaction ();
  685. IntResourceManager irm = new IntResourceManager ( 1 );
  686. using ( TransactionScope scope = new TransactionScope ( ct ) ) {
  687. irm.Value = 2;
  688. scope.Complete ();
  689. /* FIXME: Why TransactionAbortedException ?? */
  690. try {
  691. ct.Commit ();
  692. }
  693. catch ( TransactionAbortedException) {
  694. irm.Check ( 0, 0, 1, 0, "irm" );
  695. return;
  696. }
  697. Assert.Fail ( "Should not be reached" );
  698. }
  699. }
  700. [Test]
  701. [ExpectedException (typeof (InvalidOperationException))]
  702. public void ExplicitTransaction9 ()
  703. {
  704. CommittableTransaction ct = new CommittableTransaction ();
  705. IntResourceManager irm = new IntResourceManager ( 1 );
  706. ct.BeginCommit ( null, null );
  707. ct.BeginCommit ( null, null );
  708. }
  709. [Test]
  710. public void ExplicitTransaction10 ()
  711. {
  712. CommittableTransaction ct = new CommittableTransaction ();
  713. IntResourceManager irm = new IntResourceManager ( 1 );
  714. Transaction.Current = ct;
  715. irm.Value = 2;
  716. TransactionScope scope = new TransactionScope ( ct );
  717. Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
  718. //scope.Complete ();
  719. //scope.Dispose ();
  720. try {
  721. ct.Commit ();
  722. } catch ( TransactionAbortedException) {
  723. irm.Check ( 0, 0, 1, 0, "irm" );
  724. Transaction.Current = null;
  725. return;
  726. }
  727. Transaction.Current = null;
  728. Assert.Fail ("Expected TransactionAbortedException");
  729. }
  730. [Test]
  731. public void ExplicitTransaction10a ()
  732. {
  733. CommittableTransaction ct = new CommittableTransaction ();
  734. IntResourceManager irm = new IntResourceManager ( 1 );
  735. Transaction.Current = ct;
  736. irm.Value = 2;
  737. Transaction.Current = null;
  738. TransactionScope scope = new TransactionScope ( ct );
  739. Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
  740. Transaction.Current = null;
  741. //scope2.Complete ();
  742. //scope2.Dispose ();
  743. try {
  744. ct.Commit ();
  745. }
  746. catch ( TransactionAbortedException) {
  747. irm.Check ( 0, 0, 1, 0, "irm" );
  748. Transaction.Current = null;
  749. return;
  750. }
  751. Transaction.Current = null;
  752. Assert.Fail ();
  753. }
  754. [Test]
  755. public void ExplicitTransaction10b ()
  756. {
  757. CommittableTransaction ct = new CommittableTransaction ();
  758. IntResourceManager irm = new IntResourceManager ( 1 );
  759. Transaction.Current = ct;
  760. irm.Value = 2;
  761. Transaction.Current = null;
  762. TransactionScope scope = new TransactionScope ( ct );
  763. Assert.AreEqual ( ct, Transaction.Current, "ambient transaction" );
  764. //scope2.Complete ();
  765. //scope2.Dispose ();
  766. IAsyncResult ar = ct.BeginCommit ( null, null );
  767. try {
  768. ct.EndCommit (ar);
  769. }
  770. catch ( TransactionAbortedException) {
  771. irm.Check ( 0, 0, 1, 0, "irm" );
  772. Transaction.Current = null;
  773. return;
  774. }
  775. Transaction.Current = null;
  776. Assert.Fail ();
  777. }
  778. [Test]
  779. [ExpectedException ( typeof (ArgumentException))]
  780. public void ExplicitTransaction12 ()
  781. {
  782. CommittableTransaction ct = new CommittableTransaction ();
  783. IntResourceManager irm = new IntResourceManager ( 1 );
  784. irm.FailPrepare = true;
  785. ct.BeginCommit ( null, null );
  786. ct.EndCommit ( null );
  787. }
  788. [Test]
  789. public void ExplicitTransaction13 ()
  790. {
  791. CommittableTransaction ct = new CommittableTransaction ();
  792. IntResourceManager irm = new IntResourceManager ( 1 );
  793. Assert.IsNull ( Transaction.Current );
  794. Transaction.Current = ct;
  795. irm.Value = 2;
  796. irm.FailPrepare = true;
  797. try {
  798. ct.Commit ();
  799. } catch ( TransactionAbortedException ) {
  800. Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
  801. try {
  802. ct.BeginCommit ( null, null );
  803. } catch (Exception) {
  804. Transaction.Current = null;
  805. return;
  806. }
  807. Assert.Fail ( "Should not be reached(2)" );
  808. }
  809. Assert.Fail ("Should not be reached");
  810. }
  811. [Test]
  812. public void ExplicitTransaction14 ()
  813. {
  814. CommittableTransaction ct = new CommittableTransaction ();
  815. IntResourceManager irm = new IntResourceManager ( 1 );
  816. Assert.IsNull ( Transaction.Current );
  817. Transaction.Current = ct;
  818. irm.Value = 2;
  819. ct.Commit ();
  820. Assert.AreEqual ( TransactionStatus.Committed, ct.TransactionInformation.Status );
  821. try {
  822. ct.BeginCommit ( null, null );
  823. }
  824. catch ( Exception) {
  825. Transaction.Current = null;
  826. return;
  827. }
  828. Assert.Fail ( "Should not be reached" );
  829. }
  830. [Test]
  831. public void ExplicitTransaction15 ()
  832. {
  833. CommittableTransaction ct = new CommittableTransaction ();
  834. IntResourceManager irm = new IntResourceManager ( 1 );
  835. IntResourceManager irm2 = new IntResourceManager ( 3 );
  836. Assert.IsNull ( Transaction.Current );
  837. Transaction.Current = ct;
  838. try {
  839. using (TransactionScope scope = new TransactionScope ()) {
  840. irm.Value = 2;
  841. Transaction.Current = new CommittableTransaction ();
  842. irm2.Value = 6;
  843. }
  844. } catch (InvalidOperationException) {
  845. irm.Check ( 0, 0, 1, 0, "irm" );
  846. irm2.Check ( 0, 0, 1, 0, "irm2" );
  847. Transaction.Current = null;
  848. return;
  849. }
  850. Assert.Fail ( "Should not be reached" );
  851. }
  852. [Test]
  853. public void ExplicitTransaction16 ()
  854. {
  855. CommittableTransaction ct = new CommittableTransaction ();
  856. IntResourceManager irm0 = new IntResourceManager ( 3 );
  857. IntResourceManager irm = new IntResourceManager ( 1 );
  858. Assert.IsNull ( Transaction.Current );
  859. Transaction.Current = ct;
  860. irm.FailPrepare = true;
  861. irm.FailWithException = true;
  862. irm.Value = 2;
  863. irm0.Value = 6;
  864. try {
  865. ct.Commit ();
  866. } catch (TransactionAbortedException e) {
  867. Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
  868. Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
  869. irm.Check ( 1, 0, 0, 0, "irm" );
  870. irm0.Check ( 0, 0, 1, 0, "irm0" );
  871. Transaction.Current = null;
  872. return;
  873. }
  874. Assert.Fail ( "Should not be reached" );
  875. }
  876. #endregion
  877. }
  878. }