5 // Marek Safar <marek.safar@gmail.com>
6 // Chris F Carroll <chris.carroll@unforgettable.me.uk>
8 // Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
9 // Copyright (C) 2013 Chris F Carroll (http://cafe-encounter.net)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Generic;
34 using NUnit.Framework;
35 using System.Runtime.Remoting.Messaging;
36 using System.Threading;
38 using System.Threading.Tasks;
41 namespace MonoTests.System.Runtime.Remoting.Messaging
44 public class CallContextTest
46 public class Holder : ILogicalThreadAffinative
48 public string Value { get; set; }
51 const string SlotName = "Test";
54 public void CallContextPropagation_Thread ()
57 var t = new Thread (() => {
58 var h = CallContext.GetData (SlotName) as Holder;
60 CallContext.FreeNamedDataSlot (SlotName);
64 Assert.IsTrue (passed, "#1");
66 var holder = new Holder {
69 CallContext.SetData (SlotName, holder);
71 t = new Thread (() => {
72 var h = CallContext.GetData (SlotName) as Holder;
74 CallContext.FreeNamedDataSlot (SlotName);
78 CallContext.FreeNamedDataSlot (SlotName);
80 Assert.IsTrue (passed, "#2");
84 public void CallContextPropagation_ThreadPool ()
86 var holder = new Holder {
89 CallContext.SetData (SlotName, holder);
92 var mre = new ManualResetEvent (false);
93 ThreadPool.QueueUserWorkItem(x => {
94 var h = CallContext.GetData (SlotName) as Holder;
96 CallContext.FreeNamedDataSlot (SlotName);
100 Assert.IsTrue (mre.WaitOne (3000), "#1");
101 Assert.IsTrue (passed, "#2");
103 CallContext.FreeNamedDataSlot (SlotName);
107 public void CallContextPropagation_Not_ThreadPool ()
109 CallContext.SetData (SlotName, "x");
112 var mre = new ManualResetEvent (false);
113 ThreadPool.QueueUserWorkItem(x => {
114 var h = (string)CallContext.GetData (SlotName);
116 CallContext.FreeNamedDataSlot (SlotName);
120 Assert.IsTrue (mre.WaitOne (3000), "#1");
121 Assert.IsTrue (passed, "#2");
123 CallContext.FreeNamedDataSlot (SlotName);
128 public void CallContextPropagation_Task ()
130 var holder = new Holder {
131 Value = "Hello World"
133 CallContext.SetData (SlotName, holder);
136 var t = Task.Factory.StartNew(() => {
137 var h = CallContext.GetData (SlotName) as Holder;
138 passed = h == holder;
139 CallContext.FreeNamedDataSlot (SlotName);
142 Assert.IsTrue (t.Wait (3000), "#1");
143 Assert.IsTrue (passed, "#2");
145 CallContext.FreeNamedDataSlot (SlotName);
149 public void CallContextPropagation_TaskContinuation ()
153 Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId);
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);
164 Assert.IsTrue (t.Wait (3000), "#0");
165 Assert.AreEqual ("logicalData", d1, "#1");
166 Assert.IsNull (d2, "#2");
168 CallContext.FreeNamedDataSlot ("d1");
169 CallContext.FreeNamedDataSlot ("d2");
174 public void FreeNamedDataSlot_ShouldClearLogicalData ()
176 CallContext.LogicalSetData ("slotkey", "illogical");
177 CallContext.FreeNamedDataSlot ("slotkey");
179 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
180 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
184 public void FreeNamedDataSlot_ShouldClearIllogicalData ()
186 CallContext.SetData ("slotkey", "illogical");
187 CallContext.FreeNamedDataSlot ("slotkey");
189 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
190 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
194 public void FreeNamedDataSlot_ShouldClearBothLogicalAndIllogicalData ()
196 CallContext.LogicalSetData ("slotkey","logical");
197 CallContext.SetData ("slotkey", "illogical");
198 CallContext.FreeNamedDataSlot ("slotkey");
200 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
201 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");