PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Labs/Microsoft.Activities.UnitTesting/Microsoft.Activities.UnitTesting/DurableInstancing/SqlWorkflowInstanceStoreTest.cs

#
C# | 235 lines | 150 code | 28 blank | 57 comment | 3 complexity | 4c311c34f872dd4642e30774f3241135 MD5 | raw file
  1. namespace Microsoft.Activities.UnitTesting.DurableInstancing
  2. {
  3. using System;
  4. using System.Activities.DurableInstancing;
  5. using System.Runtime.DurableInstancing;
  6. using Microsoft.Activities.Extensions;
  7. using Microsoft.Activities.Extensions.DurableInstancing;
  8. using Microsoft.Activities.Extensions.Tracking;
  9. /// <summary>
  10. /// A test instance store
  11. /// </summary>
  12. public class SqlWorkflowInstanceStoreTest : SqlDatabaseTest
  13. {
  14. #region Fields
  15. /// <summary>
  16. /// The instance store
  17. /// </summary>
  18. private SqlWorkflowInstanceStore instanceStore;
  19. /// <summary>
  20. /// The instance view
  21. /// </summary>
  22. private InstanceView instanceView = null;
  23. #endregion
  24. #region Constructors and Destructors
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="SqlWorkflowInstanceStoreTest" /> class.
  27. /// </summary>
  28. public SqlWorkflowInstanceStoreTest()
  29. {
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="SqlWorkflowInstanceStoreTest" /> class.
  33. /// </summary>
  34. /// <param name="databaseName"> The database name. </param>
  35. public SqlWorkflowInstanceStoreTest(string databaseName)
  36. : base(databaseName)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="SqlWorkflowInstanceStoreTest" /> class.
  41. /// </summary>
  42. /// <param name="databaseName"> The database name. </param>
  43. /// <param name="dataSource"> The data source. </param>
  44. public SqlWorkflowInstanceStoreTest(string databaseName, string dataSource)
  45. : base(databaseName, dataSource)
  46. {
  47. }
  48. #endregion
  49. #region Public Properties
  50. /// <summary>
  51. /// Gets the instance store
  52. /// </summary>
  53. public SqlWorkflowInstanceStore InstanceStore
  54. {
  55. get
  56. {
  57. return this.instanceStore ?? (this.instanceStore = new SqlWorkflowInstanceStore(this.ConnectionString));
  58. }
  59. }
  60. #endregion
  61. #region Public Methods and Operators
  62. /// <summary>
  63. /// Asserts that the database scripts have run
  64. /// </summary>
  65. /// <remarks>
  66. /// This does not check everything
  67. /// </remarks>
  68. public void AssertDatabaseIsConfigured()
  69. {
  70. this.AssertProcs();
  71. this.AssertTables();
  72. this.AssertViews();
  73. }
  74. /// <summary>
  75. /// Asserts that the procedures exists
  76. /// </summary>
  77. public void AssertProcs()
  78. {
  79. AssertHelper.IsTrue(
  80. this.ProcedureExists,
  81. "InsertRunnableInstanceEntry",
  82. "Could not find StoredProcedure InsertRunnableInstanceEntry");
  83. AssertHelper.IsTrue(
  84. this.ProcedureExists, "RecoverInstanceLocks", "Could find StoredProcedure RecoverInstanceLocks");
  85. AssertHelper.IsTrue(
  86. this.ProcedureExists, "InsertDefinitionIdentity", "Could find StoredProcedure InsertDefinitionIdentity");
  87. AssertHelper.IsTrue(this.ProcedureExists, "CreateLockOwner", "Could find StoredProcedure CreateLockOwner");
  88. AssertHelper.IsTrue(this.ProcedureExists, "DeleteLockOwner", "Could find StoredProcedure DeleteLockOwner");
  89. AssertHelper.IsTrue(this.ProcedureExists, "ExtendLock", "Could find StoredProcedure ExtendLock");
  90. AssertHelper.IsTrue(this.ProcedureExists, "AssociateKeys", "Could find StoredProcedure AssociateKeys");
  91. AssertHelper.IsTrue(this.ProcedureExists, "CompleteKeys", "Could find StoredProcedure CompleteKeys");
  92. AssertHelper.IsTrue(this.ProcedureExists, "FreeKeys", "Could find StoredProcedure FreeKeys");
  93. AssertHelper.IsTrue(this.ProcedureExists, "CreateInstance", "Could find StoredProcedure CreateInstance");
  94. AssertHelper.IsTrue(this.ProcedureExists, "LockInstance", "Could find StoredProcedure LockInstance");
  95. AssertHelper.IsTrue(this.ProcedureExists, "UnlockInstance", "Could find StoredProcedure UnlockInstance");
  96. AssertHelper.IsTrue(
  97. this.ProcedureExists, "DetectRunnableInstances", "Could find StoredProcedure DetectRunnableInstances");
  98. AssertHelper.IsTrue(
  99. this.ProcedureExists,
  100. "GetActivatableWorkflowsActivationParameters",
  101. "Could find StoredProcedure GetActivatableWorkflowsActivationParameters");
  102. AssertHelper.IsTrue(this.ProcedureExists, "LoadInstance", "Could find StoredProcedure LoadInstance");
  103. AssertHelper.IsTrue(
  104. this.ProcedureExists, "TryLoadRunnableInstance", "Could find StoredProcedure TryLoadRunnableInstance");
  105. AssertHelper.IsTrue(this.ProcedureExists, "DeleteInstance", "Could find StoredProcedure DeleteInstance");
  106. AssertHelper.IsTrue(
  107. this.ProcedureExists, "CreateServiceDeployment", "Could find StoredProcedure CreateServiceDeployment");
  108. AssertHelper.IsTrue(this.ProcedureExists, "SaveInstance", "Could find StoredProcedure SaveInstance");
  109. AssertHelper.IsTrue(
  110. this.ProcedureExists, "InsertPromotedProperties", "Could find StoredProcedure InsertPromotedProperties");
  111. AssertHelper.IsTrue(
  112. this.ProcedureExists,
  113. "GetWorkflowInstanceStoreVersion",
  114. "Could find StoredProcedure GetWorkflowInstanceStoreVersion");
  115. }
  116. /// <summary>
  117. /// Asserts that the tables exist
  118. /// </summary>
  119. public void AssertTables()
  120. {
  121. AssertHelper.IsTrue(this.TableExists, "InstancesTable", "Could not find InstancesTable");
  122. AssertHelper.IsTrue(this.TableExists, "RunnableInstancesTable", "Could not find RunnableInstancesTable");
  123. AssertHelper.IsTrue(this.TableExists, "KeysTable", "Could not find KeysTable");
  124. AssertHelper.IsTrue(this.TableExists, "LockOwnersTable", "Could not find LockOwnersTable");
  125. AssertHelper.IsTrue(
  126. this.TableExists, "InstanceMetadataChangesTable", "Could not find InstanceMetadataChangesTable");
  127. AssertHelper.IsTrue(this.TableExists, "ServiceDeploymentsTable", "Could not find ServiceDeploymentsTable");
  128. AssertHelper.IsTrue(
  129. this.TableExists, "InstancePromotedPropertiesTable", "Could not find InstancePromotedPropertiesTable");
  130. AssertHelper.IsTrue(
  131. this.TableExists,
  132. "SqlWorkflowInstanceStoreVersionTable",
  133. "Could not find SqlWorkflowInstanceStoreVersionTable");
  134. AssertHelper.IsTrue(this.TableExists, "DefinitionIdentityTable", "Could not find DefinitionIdentityTable");
  135. AssertHelper.IsTrue(this.TableExists, "IdentityOwnerTable", "Could not find IdentityOwnerTable");
  136. AssertHelper.IsTrue(this.TableExists, "InstancesTable", "Could not find InstancesTable");
  137. }
  138. /// <summary>
  139. /// Asserts that the views exist
  140. /// </summary>
  141. public void AssertViews()
  142. {
  143. AssertHelper.IsTrue(this.ViewExists, "ServiceDeployments", "Could not find ServiceDeployments");
  144. AssertHelper.IsTrue(
  145. this.ViewExists, "InstancePromotedProperties", "Could not find InstancePromotedProperties");
  146. AssertHelper.IsTrue(this.ViewExists, "Instances", "Could not find Instances");
  147. }
  148. /// <summary>
  149. /// Runs the schema and logic scripts on an existing test database
  150. /// </summary>
  151. public void ConfigureInstanceStore()
  152. {
  153. SqlWorkflowInstanceStoreManager.ConfigureInstanceStore(this.DatabaseName, this.ConnectionString);
  154. }
  155. /// <summary>
  156. /// Creates an instance store
  157. /// </summary>
  158. /// <param name="dropIfExists"> Drop the database if it exists </param>
  159. /// <returns> The SqlWorkflowInstanceStore. </returns>
  160. public SqlWorkflowInstanceStore CreateInstanceStore(bool dropIfExists = false)
  161. {
  162. this.instanceStore = SqlWorkflowInstanceStoreManager.CreateInstanceStore(
  163. this.DatabaseName, this.ConnectionString, dropIfExists);
  164. return this.instanceStore;
  165. }
  166. /// <summary>
  167. /// Initializes the instance store
  168. /// </summary>
  169. public void CreateOwner()
  170. {
  171. using (var instanceHandle = new DisposableInstanceHandle(this.InstanceStore))
  172. {
  173. this.instanceView = this.InstanceStore.Execute(
  174. instanceHandle, new CreateWorkflowOwnerCommand(), Global.Timeout);
  175. this.InstanceStore.DefaultInstanceOwner = this.instanceView.InstanceOwner;
  176. }
  177. }
  178. /// <summary>
  179. /// Delete the owner
  180. /// </summary>
  181. public void DeleteOwner()
  182. {
  183. using (var instanceHandle = new DisposableInstanceHandle(this.InstanceStore))
  184. {
  185. this.InstanceStore.Execute(instanceHandle, new DeleteWorkflowOwnerCommand(), Global.Timeout);
  186. }
  187. }
  188. #endregion
  189. #region Methods
  190. /// <summary>
  191. /// Dispose of the resource
  192. /// </summary>
  193. /// <param name="disposing"> The disposing flag. </param>
  194. protected override void Dispose(bool disposing)
  195. {
  196. if (disposing)
  197. {
  198. if (this.instanceView != null)
  199. {
  200. this.DeleteOwner();
  201. }
  202. base.Dispose(true);
  203. }
  204. }
  205. #endregion
  206. }
  207. }