2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Threading / AutoResetEventTest.cs
blob6f9c23fc5fb660504723d290123b0bbf9d42bee6
1 //
2 // AutoResetEventTest.cs - NUnit test cases for System.Threading.AutoResetEvent
3 //
4 // Author:
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Gert Driesen <gert.driesen@telenet.be>
7 //
8 // Copyright 2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2007 Gert Driesen
12 using NUnit.Framework;
13 using System;
14 using System.Threading;
15 #if NET_2_0
16 using Microsoft.Win32.SafeHandles;
17 #endif
19 namespace MonoTests.System.Threading
21 [TestFixture]
22 public class AutoResetEventTest
24 [Test]
25 public void MultipleSet ()
27 AutoResetEvent evt = new AutoResetEvent (true);
28 Assert.IsTrue (evt.WaitOne (1000, false), "#1");
29 evt.Set ();
30 evt.Set ();
31 Assert.IsTrue (evt.WaitOne (1000, false), "#2");
32 Assert.IsFalse (evt.WaitOne (1000, false), "#3");
35 #if NET_2_0
36 [Test] // bug #81529
37 public void SafeWaitHandle ()
39 AutoResetEvent are1 = new AutoResetEvent (false);
40 AutoResetEvent are2 = new AutoResetEvent (false);
41 SafeWaitHandle swh1 = are1.SafeWaitHandle;
42 SafeWaitHandle swh2 = are2.SafeWaitHandle;
43 are1.SafeWaitHandle = are2.SafeWaitHandle;
44 Assert.AreSame (are1.SafeWaitHandle, are2.SafeWaitHandle, "#1");
45 Assert.AreEqual (are1.Handle, are2.Handle, "#2");
46 Assert.IsFalse (are1.SafeWaitHandle.IsInvalid, "#3");
47 Assert.IsFalse (are1.SafeWaitHandle.IsClosed, "#4");
48 Assert.IsFalse (swh1.IsClosed, "#5");
49 Assert.IsFalse (swh1.IsInvalid, "#6");
50 swh1.Dispose ();
51 are1.Close ();
54 [Test] // bug #81529
55 public void SafeWaitHandle_Null ()
57 AutoResetEvent are1 = new AutoResetEvent (false);
58 SafeWaitHandle swh1 = are1.SafeWaitHandle;
59 are1.SafeWaitHandle = null;
60 Assert.IsNotNull (are1.SafeWaitHandle, "#1");
61 Assert.AreEqual (-1, (int) are1.Handle, "#2");
62 Assert.IsTrue (are1.SafeWaitHandle.IsInvalid, "#3");
63 Assert.IsFalse (are1.SafeWaitHandle.IsClosed, "#4");
64 Assert.IsFalse (swh1.IsClosed, "#5");
65 Assert.IsFalse (swh1.IsInvalid, "#6");
68 [Test] // bug #81529
69 public void Handle_Valid ()
71 AutoResetEvent are1 = new AutoResetEvent (false);
72 SafeWaitHandle swh1 = are1.SafeWaitHandle;
73 Assert.IsFalse (swh1.IsClosed, "#1");
74 Assert.IsFalse (swh1.IsInvalid, "#2");
75 IntPtr dummyHandle = (IntPtr) 2;
76 are1.Handle = dummyHandle;
77 Assert.AreEqual (are1.Handle, dummyHandle, "#3");
78 Assert.IsFalse (swh1.IsClosed, "#4");
79 Assert.IsFalse (swh1.IsClosed, "#5");
80 Assert.IsFalse (swh1.IsInvalid, "#6");
81 Assert.IsFalse (are1.SafeWaitHandle.IsClosed, "#7");
82 Assert.IsFalse (are1.SafeWaitHandle.IsInvalid, "#8");
83 are1.Close ();
84 swh1.Dispose ();
87 [Test] // bug #81529
88 public void Handle_Invalid ()
90 AutoResetEvent are1 = new AutoResetEvent (false);
91 SafeWaitHandle swh1 = are1.SafeWaitHandle;
92 are1.Handle = (IntPtr) (-1);
93 Assert.IsTrue (swh1 != are1.SafeWaitHandle, "#1");
94 Assert.IsFalse (swh1.IsClosed, "#2");
95 Assert.IsFalse (swh1.IsInvalid, "#3");
96 Assert.IsFalse (are1.SafeWaitHandle.IsClosed, "#4");
97 Assert.IsTrue (are1.SafeWaitHandle.IsInvalid, "#5");
98 are1.Close ();
99 swh1.Dispose ();
102 [Test] // bug #81529
103 public void Handle_ZeroPtr ()
105 AutoResetEvent are1 = new AutoResetEvent (false);
106 SafeWaitHandle swh1 = are1.SafeWaitHandle;
107 are1.Handle = IntPtr.Zero;
108 Assert.IsTrue (swh1 != are1.SafeWaitHandle, "#1");
109 Assert.IsFalse (swh1.IsClosed, "#2");
110 Assert.IsFalse (swh1.IsInvalid, "#3");
111 Assert.IsFalse (are1.SafeWaitHandle.IsClosed, "#4");
112 Assert.IsTrue (are1.SafeWaitHandle.IsInvalid, "#5");
113 are1.Close ();
114 swh1.Dispose ();
116 #endif