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 
5 using System;
6 using System.Runtime.InteropServices;
7 using System.Threading;
8 using Xunit;
9 
10 public partial class ThreadPoolBoundHandleTests
11 {
12     [Fact]
PreAllocatedOverlapped_NullAsCallback_ThrowsArgumentNullException()13     public unsafe void PreAllocatedOverlapped_NullAsCallback_ThrowsArgumentNullException()
14     {
15         AssertExtensions.Throws<ArgumentNullException>("callback", () =>
16         {
17             new PreAllocatedOverlapped(null, new object(), new byte[256]);
18         });
19 
20         // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
21         GC.Collect();
22         GC.WaitForPendingFinalizers();
23     }
24 
25     [Fact]
PreAllocatedOverlapped_NullAsContext_DoesNotThrow()26     public unsafe void PreAllocatedOverlapped_NullAsContext_DoesNotThrow()
27     {
28         using(new PreAllocatedOverlapped((_, __, ___) => { }, (object)null, new byte[256])) {}
29     }
30 
31     [Fact]
PreAllocatedOverlapped_NullAsPinData_DoesNotThrow()32     public unsafe void PreAllocatedOverlapped_NullAsPinData_DoesNotThrow()
33     {
34         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), (byte[])null)) {}
35     }
36 
37     [Fact]
PreAllocatedOverlapped_EmptyArrayAsPinData_DoesNotThrow()38     public unsafe void PreAllocatedOverlapped_EmptyArrayAsPinData_DoesNotThrow()
39     {
40         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[0])) {}
41     }
42 
43     [Fact]
PreAllocatedOverlapped_NonBlittableTypeAsPinData_Throws()44     public unsafe void PreAllocatedOverlapped_NonBlittableTypeAsPinData_Throws()
45     {
46         AssertExtensions.Throws<ArgumentException>(null, () => new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new NonBlittableType() { s = "foo" }));
47 
48         // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
49         GC.Collect();
50         GC.WaitForPendingFinalizers();
51     }
52 
53     [Fact]
PreAllocatedOverlapped_BlittableTypeAsPinData_DoesNotThrow()54     public unsafe void PreAllocatedOverlapped_BlittableTypeAsPinData_DoesNotThrow()
55     {
56         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new BlittableType() { i = 42 })) {}
57     }
58 
59     [Fact]
PreAllocatedOverlapped_ObjectArrayAsPinData_DoesNotThrow()60     public unsafe void PreAllocatedOverlapped_ObjectArrayAsPinData_DoesNotThrow()
61     {
62         object[] array = new object[]
63         {
64             new BlittableType() { i = 1 },
65             new byte[5],
66         };
67         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), array)) {}
68     }
69 
70     [Fact]
PreAllocatedOverlapped_ObjectArrayWithNonBlittableTypeAsPinData_Throws()71     public unsafe void PreAllocatedOverlapped_ObjectArrayWithNonBlittableTypeAsPinData_Throws()
72     {
73         object[] array = new object[]
74         {
75             new NonBlittableType() { s = "foo" },
76             new byte[5],
77         };
78         AssertExtensions.Throws<ArgumentException>(null, () => new PreAllocatedOverlapped((_, __, ___) => { }, new object(), array));
79 
80         // Make sure the PreAllocatedOverlapped finalizer does the right thing in the case where the .ctor failed.
81         GC.Collect();
82         GC.WaitForPendingFinalizers();
83     }
84 
85     [Fact]
PreAllocatedOverlapped_ReturnedNativeOverlapped_InternalLowAndInternalHighSetToZero()86     public unsafe void PreAllocatedOverlapped_ReturnedNativeOverlapped_InternalLowAndInternalHighSetToZero()
87     {
88         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256])) {}
89     }
90 
91     [Fact]
PreAllocatedOverlapped_ReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()92     public unsafe void PreAllocatedOverlapped_ReturnedNativeOverlapped_OffsetLowAndOffsetHighSetToZero()
93     {
94         using(new PreAllocatedOverlapped((_, __, ___) => { }, new object(), new byte[256])) {}
95     }
96 }
97