[SRE] Improved token fixups processing.
[mono.git] / mcs / class / corlib / Test / System.Runtime.Remoting.Messaging / CallContextTest.cs
1 //
2 // CallContexTest.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //  Chris F Carroll <chris.carroll@unforgettable.me.uk>
7 //
8 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
9 // Copyright (C) 2013 Chris F Carroll (http://cafe-encounter.net)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using NUnit.Framework;
35 using System.Runtime.Remoting.Messaging;
36 using System.Threading;
37 #if NET_4_0
38 using System.Threading.Tasks;
39 #endif
40
41 namespace MonoTests.System.Runtime.Remoting.Messaging
42 {
43         [TestFixture]
44         public class CallContextTest
45         {
46                 public class Holder : ILogicalThreadAffinative
47                 {
48                         public string Value { get; set; }
49                 }
50
51                 const string SlotName = "Test";
52
53                 [Test]
54                 public void CallContextPropagation_Thread ()
55                 {
56                         bool passed = false;
57                         var t = new Thread (() => {
58                                 var h = CallContext.GetData (SlotName) as Holder;
59                                 passed = h == null;
60                                 CallContext.FreeNamedDataSlot (SlotName);
61                         });
62                         t.Start ();
63                         t.Join ();
64                         Assert.IsTrue (passed, "#1");
65
66                         var holder = new Holder {
67                                 Value = "Hello World"
68                         };
69                         CallContext.SetData (SlotName, holder);
70
71                         t = new Thread (() => {
72                                 var h = CallContext.GetData (SlotName) as Holder;
73                                 passed = h == holder;
74                                 CallContext.FreeNamedDataSlot (SlotName);
75                         });
76                         t.Start ();
77                         t.Join ();
78                         CallContext.FreeNamedDataSlot (SlotName);
79
80                         Assert.IsTrue (passed, "#2");
81                 }
82
83                 [Test]
84                 public void CallContextPropagation_ThreadPool ()
85                 {
86                         var holder = new Holder {
87                                 Value = "Hello World"
88                         };
89                         CallContext.SetData (SlotName, holder);
90
91                         bool passed = false;
92                         var mre = new ManualResetEvent (false);
93                         ThreadPool.QueueUserWorkItem(x => {
94                                 var h = CallContext.GetData (SlotName) as Holder;
95                                 passed = h == holder;
96                                 CallContext.FreeNamedDataSlot (SlotName);
97                                 mre.Set ();
98                         });
99
100                         Assert.IsTrue (mre.WaitOne (3000), "#1");
101                         Assert.IsTrue (passed, "#2");
102
103                         CallContext.FreeNamedDataSlot (SlotName);
104                 }
105
106                 [Test]
107                 public void CallContextPropagation_Not_ThreadPool ()
108                 {
109                         CallContext.SetData (SlotName, "x");
110
111                         bool passed = false;
112                         var mre = new ManualResetEvent (false);
113                         ThreadPool.QueueUserWorkItem(x => {
114                                 var h = (string)CallContext.GetData (SlotName);
115                                 passed = h == null;
116                                 CallContext.FreeNamedDataSlot (SlotName);
117                                 mre.Set ();
118                         });
119
120                         Assert.IsTrue (mre.WaitOne (3000), "#1");
121                         Assert.IsTrue (passed, "#2");
122
123                         CallContext.FreeNamedDataSlot (SlotName);
124                 }
125
126 #if NET_4_0
127                 [Test]
128                 public void CallContextPropagation_Task ()
129                 {
130                         var holder = new Holder {
131                                 Value = "Hello World"
132                         };
133                         CallContext.SetData (SlotName, holder);
134                         
135                         bool passed = false;
136                         var t = Task.Factory.StartNew(() => {
137                                 var h = CallContext.GetData (SlotName) as Holder;
138                                 passed = h == holder;
139                                 CallContext.FreeNamedDataSlot (SlotName);
140                         });
141
142                         Assert.IsTrue (t.Wait (3000), "#1");
143                         Assert.IsTrue (passed, "#2");
144
145                         CallContext.FreeNamedDataSlot (SlotName);
146                 }
147
148                 [Test]
149                 public void CallContextPropagation_TaskContinuation ()
150                 {
151                         string d1 = null;
152                         string d2 = null;
153                         Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId);
154
155                         var ct = Thread.CurrentThread.ManagedThreadId;
156                         CallContext.LogicalSetData ("d1", "logicalData");
157                         CallContext.SetData ("d2", "data2");
158                         var t = Task.Factory.StartNew (() => {
159                                 }).ContinueWith (task => {
160                                         d1 = (string) CallContext.LogicalGetData ("d1");
161                                         d2 = (string) CallContext.GetData ("d2");
162                                 }, TaskContinuationOptions.ExecuteSynchronously);
163
164                         Assert.IsTrue (t.Wait (3000), "#0");
165                         Assert.AreEqual ("logicalData", d1, "#1");
166                         Assert.IsNull (d2, "#2");
167
168                         CallContext.FreeNamedDataSlot ("d1");
169                         CallContext.FreeNamedDataSlot ("d2");
170                 }
171 #endif
172
173                 [Test]
174                 public void FreeNamedDataSlot_ShouldClearLogicalData ()
175                 {
176                         CallContext.LogicalSetData ("slotkey", "illogical");
177                         CallContext.FreeNamedDataSlot ("slotkey");
178
179                         Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
180                         Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
181                 }
182
183                 [Test]
184                 public void FreeNamedDataSlot_ShouldClearIllogicalData ()
185                 {
186                         CallContext.SetData ("slotkey", "illogical");
187                         CallContext.FreeNamedDataSlot ("slotkey");
188
189                         Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
190                         Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
191                 }
192
193                 [Test]
194                 public void FreeNamedDataSlot_ShouldClearBothLogicalAndIllogicalData ()
195                 {
196                         CallContext.LogicalSetData ("slotkey","logical");
197                         CallContext.SetData ("slotkey", "illogical");
198                         CallContext.FreeNamedDataSlot ("slotkey");
199
200                         Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
201                         Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
202                 }
203         }
204 }