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;
37 using System.Threading.Tasks;
39 namespace MonoTests.System.Runtime.Remoting.Messaging
42 public class CallContextTest
44 public class Holder : ILogicalThreadAffinative
46 public string Value { get; set; }
49 const string SlotName = "Test";
52 public void CallContextPropagation_Thread ()
55 var t = new Thread (() => {
56 var h = CallContext.GetData (SlotName) as Holder;
58 CallContext.FreeNamedDataSlot (SlotName);
62 Assert.IsTrue (passed, "#1");
64 var holder = new Holder {
67 CallContext.SetData (SlotName, holder);
69 t = new Thread (() => {
70 var h = CallContext.GetData (SlotName) as Holder;
72 CallContext.FreeNamedDataSlot (SlotName);
76 CallContext.FreeNamedDataSlot (SlotName);
78 Assert.IsTrue (passed, "#2");
82 public void CallContextPropagation_ThreadPool ()
84 var holder = new Holder {
87 CallContext.SetData (SlotName, holder);
90 var mre = new ManualResetEvent (false);
91 ThreadPool.QueueUserWorkItem(x => {
92 var h = CallContext.GetData (SlotName) as Holder;
94 CallContext.FreeNamedDataSlot (SlotName);
98 Assert.IsTrue (mre.WaitOne (3000), "#1");
99 Assert.IsTrue (passed, "#2");
101 CallContext.FreeNamedDataSlot (SlotName);
105 public void CallContextPropagation_Not_ThreadPool ()
107 CallContext.SetData (SlotName, "x");
110 var mre = new ManualResetEvent (false);
111 ThreadPool.QueueUserWorkItem(x => {
112 var h = (string)CallContext.GetData (SlotName);
114 CallContext.FreeNamedDataSlot (SlotName);
118 Assert.IsTrue (mre.WaitOne (3000), "#1");
119 Assert.IsTrue (passed, "#2");
121 CallContext.FreeNamedDataSlot (SlotName);
125 public void CallContextPropagation_Task ()
127 var holder = new Holder {
128 Value = "Hello World"
130 CallContext.SetData (SlotName, holder);
133 var t = Task.Factory.StartNew(() => {
134 var h = CallContext.GetData (SlotName) as Holder;
135 passed = h == holder;
136 CallContext.FreeNamedDataSlot (SlotName);
139 Assert.IsTrue (t.Wait (3000), "#1");
140 Assert.IsTrue (passed, "#2");
142 CallContext.FreeNamedDataSlot (SlotName);
146 public void CallContextPropagation_TaskContinuation ()
150 Console.WriteLine("Current thread: {0}", Thread.CurrentThread.ManagedThreadId);
152 var ct = Thread.CurrentThread.ManagedThreadId;
153 CallContext.LogicalSetData ("d1", "logicalData");
154 CallContext.SetData ("d2", "data2");
155 var t = Task.Factory.StartNew (() => {
156 }).ContinueWith (task => {
157 d1 = (string) CallContext.LogicalGetData ("d1");
158 d2 = (string) CallContext.GetData ("d2");
159 }, TaskContinuationOptions.ExecuteSynchronously);
161 Assert.IsTrue (t.Wait (3000), "#0");
162 Assert.AreEqual ("logicalData", d1, "#1");
163 Assert.IsNull (d2, "#2");
165 CallContext.FreeNamedDataSlot ("d1");
166 CallContext.FreeNamedDataSlot ("d2");
170 public void FreeNamedDataSlot_ShouldClearLogicalData ()
172 CallContext.LogicalSetData ("slotkey", "illogical");
173 CallContext.FreeNamedDataSlot ("slotkey");
175 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
176 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
180 public void FreeNamedDataSlot_ShouldClearIllogicalData ()
182 CallContext.SetData ("slotkey", "illogical");
183 CallContext.FreeNamedDataSlot ("slotkey");
185 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
186 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");
190 public void FreeNamedDataSlot_ShouldClearBothLogicalAndIllogicalData ()
192 CallContext.LogicalSetData ("slotkey","logical");
193 CallContext.SetData ("slotkey", "illogical");
194 CallContext.FreeNamedDataSlot ("slotkey");
196 Assert.IsNull (CallContext.LogicalGetData ("slotkey"), "Illogical slot should be null");
197 Assert.IsNull (CallContext.GetData ("slotkey"), "Illogical slot should be null");