PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_AllocateNativeOverlappedTests.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 272 lines | 223 code | 46 blank | 3 comment | 5 complexity | 8687789818d608d37a1b1923ba7d9d07 MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using Xunit;
  8. public partial class ThreadPoolBoundHandleTests
  9. {
  10. [Fact]
  11. public unsafe void AllocateNativeOverlapped_NullAsCallback_ThrowsArgumentNullException()
  12. {
  13. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  14. {
  15. Assert.Throws<ArgumentNullException>("callback", () =>
  16. {
  17. handle.AllocateNativeOverlapped(null, new object(), new byte[256]);
  18. });
  19. }
  20. }
  21. [Fact]
  22. public unsafe void AllocateNativeOverlapped_PreAllocated_ThrowsArgumentNullException()
  23. {
  24. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  25. {
  26. Assert.Throws<ArgumentNullException>("preAllocated", () =>
  27. {
  28. handle.AllocateNativeOverlapped((PreAllocatedOverlapped)null);
  29. });
  30. }
  31. }
  32. [Fact]
  33. public unsafe void AllocateNativeOverlapped_NullAsContext_DoesNotThrow()
  34. {
  35. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  36. {
  37. NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, (object)null, new byte[256]);
  38. Assert.True(result != null);
  39. handle.FreeNativeOverlapped(result);
  40. }
  41. }
  42. [Fact]
  43. public unsafe void AllocateNativeOverlapped_NullAsPinData_DoesNotThrow()
  44. {
  45. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  46. {
  47. NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), (byte[])null);
  48. Assert.True(result != null);
  49. handle.FreeNativeOverlapped(result);
  50. }
  51. }
  52. [Fact]
  53. public unsafe void AllocateNativeOverlapped_EmptyArrayAsPinData_DoesNotThrow()
  54. {
  55. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  56. {
  57. NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[0]);
  58. Assert.True(result != null);
  59. handle.FreeNativeOverlapped(result);
  60. }
  61. }
  62. [Fact]
  63. public unsafe void AllocateNativeOverlapped_NonBlittableTypeAsPinData_Throws()
  64. {
  65. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  66. {
  67. Assert.Throws<ArgumentException>(() => handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new NonBlittableType() { s = "foo" }));
  68. }
  69. }
  70. [Fact]
  71. public unsafe void AllocateNativeOverlapped_BlittableTypeAsPinData_DoesNotThrow()
  72. {
  73. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  74. {
  75. NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new BlittableType() { i = 42 });
  76. Assert.True(result != null);
  77. handle.FreeNativeOverlapped(result);
  78. }
  79. }
  80. [Fact]
  81. public unsafe void AllocateNativeOverlapped_ObjectArrayAsPinData_DoesNotThrow()
  82. {
  83. object[] array = new object[]
  84. {
  85. new BlittableType() { i = 1 },
  86. new byte[5],
  87. };
  88. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  89. {
  90. NativeOverlapped* result = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), array);
  91. Assert.True(result != null);
  92. handle.FreeNativeOverlapped(result);
  93. }
  94. }
  95. [Fact]
  96. public unsafe void AllocateNativeOverlapped_ObjectArrayWithNonBlittableTypeAsPinData_Throws()
  97. {
  98. object[] array = new object[]
  99. {
  100. new NonBlittableType() { s = "foo" },
  101. new byte[5],
  102. };
  103. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  104. {
  105. Assert.Throws<ArgumentException>(() => handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), array));
  106. }
  107. }
  108. [Fact]
  109. public unsafe void AllocateNativeOverlapped_ReturnedNativeOverlapped_AllFieldsZero()
  110. {
  111. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  112. {
  113. NativeOverlapped* overlapped = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);
  114. Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
  115. Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
  116. Assert.Equal(0, overlapped->OffsetLow);
  117. Assert.Equal(0, overlapped->OffsetHigh);
  118. Assert.Equal(IntPtr.Zero, overlapped->EventHandle);
  119. handle.FreeNativeOverlapped(overlapped);
  120. }
  121. }
  122. [Fact]
  123. public unsafe void AllocateNativeOverlapped_PreAllocated_ReturnedNativeOverlapped_AllFieldsZero()
  124. {
  125. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  126. {
  127. using(PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]))
  128. {
  129. NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);
  130. Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
  131. Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
  132. Assert.Equal(0, overlapped->OffsetLow);
  133. Assert.Equal(0, overlapped->OffsetHigh);
  134. Assert.Equal(IntPtr.Zero, overlapped->EventHandle);
  135. handle.FreeNativeOverlapped(overlapped);
  136. }
  137. }
  138. }
  139. [Fact]
  140. public unsafe void AllocateNativeOverlapped_PossibleReusedReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
  141. { // The CLR reuses NativeOverlapped underneath, check to make sure that they reset fields back to zero
  142. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  143. {
  144. NativeOverlapped* overlapped = handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);
  145. overlapped->OffsetHigh = 1;
  146. overlapped->OffsetLow = 1;
  147. handle.FreeNativeOverlapped(overlapped);
  148. overlapped = handle.AllocateNativeOverlapped((errorCode, numBytes, overlap) => { }, new object(), new byte[256]);
  149. Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
  150. Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
  151. Assert.Equal(0, overlapped->OffsetLow);
  152. Assert.Equal(0, overlapped->OffsetHigh);
  153. Assert.Equal(IntPtr.Zero, overlapped->EventHandle);
  154. handle.FreeNativeOverlapped(overlapped);
  155. }
  156. }
  157. [Fact]
  158. public unsafe void AllocateNativeOverlapped_PreAllocated_ReusedReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
  159. { // The CLR reuses NativeOverlapped underneath, check to make sure that they reset fields back to zero
  160. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  161. {
  162. PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256]);
  163. NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);
  164. overlapped->OffsetHigh = 1;
  165. overlapped->OffsetLow = 1;
  166. handle.FreeNativeOverlapped(overlapped);
  167. overlapped = handle.AllocateNativeOverlapped(preAlloc);
  168. Assert.Equal(IntPtr.Zero, overlapped->InternalLow);
  169. Assert.Equal(IntPtr.Zero, overlapped->InternalHigh);
  170. Assert.Equal(0, overlapped->OffsetLow);
  171. Assert.Equal(0, overlapped->OffsetHigh);
  172. Assert.Equal(IntPtr.Zero, overlapped->EventHandle);
  173. handle.FreeNativeOverlapped(overlapped);
  174. }
  175. }
  176. [Fact]
  177. public unsafe void AllocateNativeOverlapped_WhenDisposed_ThrowsObjectDisposedException()
  178. {
  179. ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle();
  180. handle.Dispose();
  181. Assert.Throws<ObjectDisposedException>(() =>
  182. {
  183. handle.AllocateNativeOverlapped((_, __, ___) => { }, new object(), new byte[256]);
  184. });
  185. }
  186. [Fact]
  187. public unsafe void AllocateNativeOverlapped_PreAllocated_WhenDisposed_ThrowsObjectDisposedException()
  188. {
  189. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  190. {
  191. PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null);
  192. preAlloc.Dispose();
  193. Assert.Throws<ObjectDisposedException>(() =>
  194. {
  195. handle.AllocateNativeOverlapped(preAlloc);
  196. });
  197. }
  198. }
  199. [Fact]
  200. public unsafe void AllocateNativeOverlapped_PreAllocated_WhenHandleDisposed_ThrowsObjectDisposedException()
  201. {
  202. ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle();
  203. handle.Dispose();
  204. PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null);
  205. Assert.Throws<ObjectDisposedException>(() =>
  206. {
  207. handle.AllocateNativeOverlapped(preAlloc);
  208. });
  209. }
  210. [Fact]
  211. public unsafe void AllocateNativeOverlapped_PreAllocated_WhenAlreadyAllocated_ThrowsArgumentException()
  212. {
  213. using(ThreadPoolBoundHandle handle = CreateThreadPoolBoundHandle())
  214. {
  215. using(PreAllocatedOverlapped preAlloc = new PreAllocatedOverlapped(delegate { }, null, null))
  216. {
  217. NativeOverlapped* overlapped = handle.AllocateNativeOverlapped(preAlloc);
  218. Assert.Throws<ArgumentException>(() =>
  219. {
  220. handle.AllocateNativeOverlapped(preAlloc);
  221. });
  222. handle.FreeNativeOverlapped(overlapped);
  223. }
  224. }
  225. }
  226. }