Repository navigation
Expand file tree
/
Copy pathDelegatingJoinableTaskFactoryTests.cs
More file actions
212 lines (181 loc) · 7.48 KB
/
Copy pathDelegatingJoinableTaskFactoryTests.cs
File metadata and controls
212 lines (181 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//-----------------------------------------------------------------------
// <copyright file="DelegatingJoinableTaskFactoryTests.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.VisualStudio.Threading.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
public class DelegatingJoinableTaskFactoryTests : JoinableTaskTestBase
{
private object logLock;
private IList<FactoryLogEntry> log;
public DelegatingJoinableTaskFactoryTests(ITestOutputHelper logger)
: base(logger)
{
this.logLock = new object();
this.log = new List<FactoryLogEntry>();
}
private enum FactoryLogEntry
{
OuterWaitSynchronously = 1,
InnerWaitSynchronously,
OuterOnTransitioningToMainThread,
InnerOnTransitioningToMainThread,
OuterOnTransitionedToMainThread,
InnerOnTransitionedToMainThread,
OuterPostToUnderlyingSynchronizationContext,
InnerPostToUnderlyingSynchronizationContext,
}
[StaFact]
public void DelegationBehaviors()
{
var innerFactory = new CustomizedFactory(this.context, this.AddToLog);
var delegatingFactory = new DelegatingFactory(innerFactory, this.AddToLog);
delegatingFactory.Run(async delegate
{
await Task.Delay(1);
});
var jt = delegatingFactory.RunAsync(async delegate
{
await TaskScheduler.Default;
await delegatingFactory.SwitchToMainThreadAsync();
});
jt.Join();
lock (this.logLock)
{
while (!ValidateDelegatingLog(this.log.ToList()))
{
this.Logger.WriteLine("Waiting with a count of {0}", this.log.Count);
Assert.True(Monitor.Wait(this.logLock, AsyncDelay));
}
}
}
/// <summary>
/// Verifies that delegating factories add their tasks to the inner factory's collection.
/// </summary>
[StaFact]
public void DelegationSharesCollection()
{
var log = new List<FactoryLogEntry>();
var delegatingFactory = new DelegatingFactory(this.asyncPump, this.AddToLog);
JoinableTask jt = null;
jt = delegatingFactory.RunAsync(async delegate
{
await Task.Yield();
Assert.True(this.joinableCollection.Contains(jt));
});
jt.Join();
}
private static bool ValidateDelegatingLog(IList<FactoryLogEntry> log)
{
Requires.NotNull(log, nameof(log));
// All outer entries must have a pairing inner entry that appears
// after it in the list. Remove all pairs until list is empty.
while (log.Count > 0)
{
// An outer entry always be before its inner entry
if ((int)log[0] % 2 == 0)
{
return false;
}
// An outer entry must have a pairing inner entry
if (!log.Remove(log[0] + 1))
{
return false;
}
log.RemoveAt(0);
}
return true;
}
private void AddToLog(FactoryLogEntry entry)
{
lock (this.logLock)
{
this.Logger.WriteLine($"Adding entry {entry} (#{this.log.Count + 1}) from thread {Environment.CurrentManagedThreadId}");
this.log.Add(entry);
Monitor.Pulse(this.logLock);
}
}
/// <summary>
/// The ordinary customization of a factory.
/// </summary>
private class CustomizedFactory : JoinableTaskFactory
{
private readonly Action<FactoryLogEntry> addToLog;
internal CustomizedFactory(JoinableTaskContext context, Action<FactoryLogEntry> addToLog)
: base(context)
{
Requires.NotNull(addToLog, nameof(addToLog));
this.addToLog = addToLog;
}
internal CustomizedFactory(JoinableTaskCollection collection, Action<FactoryLogEntry> addToLog)
: base(collection)
{
Requires.NotNull(addToLog, nameof(addToLog));
this.addToLog = addToLog;
}
protected override void WaitSynchronously(Task task)
{
this.addToLog(FactoryLogEntry.InnerWaitSynchronously);
base.WaitSynchronously(task);
}
protected override void PostToUnderlyingSynchronizationContext(System.Threading.SendOrPostCallback callback, object state)
{
this.addToLog(FactoryLogEntry.InnerPostToUnderlyingSynchronizationContext);
base.PostToUnderlyingSynchronizationContext(callback, state);
}
protected override void OnTransitioningToMainThread(JoinableTask joinableTask)
{
this.addToLog(FactoryLogEntry.InnerOnTransitioningToMainThread);
base.OnTransitioningToMainThread(joinableTask);
}
protected override void OnTransitionedToMainThread(JoinableTask joinableTask, bool canceled)
{
this.addToLog(FactoryLogEntry.InnerOnTransitionedToMainThread);
base.OnTransitionedToMainThread(joinableTask, canceled);
}
}
/// <summary>
/// A factory that wants to wrap a potentially customized factory and decorate/override
/// its behaviors.
/// </summary>
private class DelegatingFactory : DelegatingJoinableTaskFactory
{
private readonly Action<FactoryLogEntry> addToLog;
internal DelegatingFactory(JoinableTaskFactory innerFactory, Action<FactoryLogEntry> addToLog)
: base(innerFactory)
{
Requires.NotNull(addToLog, nameof(addToLog));
this.addToLog = addToLog;
}
protected override void WaitSynchronously(Task task)
{
this.addToLog(FactoryLogEntry.OuterWaitSynchronously);
base.WaitSynchronously(task);
}
protected override void PostToUnderlyingSynchronizationContext(System.Threading.SendOrPostCallback callback, object state)
{
this.addToLog(FactoryLogEntry.OuterPostToUnderlyingSynchronizationContext);
base.PostToUnderlyingSynchronizationContext(callback, state);
}
protected override void OnTransitioningToMainThread(JoinableTask joinableTask)
{
this.addToLog(FactoryLogEntry.OuterOnTransitioningToMainThread);
base.OnTransitioningToMainThread(joinableTask);
}
protected override void OnTransitionedToMainThread(JoinableTask joinableTask, bool canceled)
{
this.addToLog(FactoryLogEntry.OuterOnTransitionedToMainThread);
base.OnTransitionedToMainThread(joinableTask, canceled);
}
}
}
}