Skip to content

Commit 116628b

Browse files
Val/988 add mstest4 configuration (#243)
* fix: 988-add-mstest4-configuration - add MSTestV4MockingContextResolver.cs - create a base class for all mstest resolvers closes #988 Co-authored-by: Ivo Stoilov <[email protected]>
1 parent 6271c13 commit 116628b

File tree

5 files changed

+116
-122
lines changed

5 files changed

+116
-122
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
JustMock Lite
3+
Copyright © 2010-2015 Progress Software Corporation
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
using System;
19+
using System.Threading;
20+
21+
namespace Telerik.JustMock.Core.Context
22+
{
23+
internal abstract class MSTestBaseMockingContextResolver : HierarchicalTestFrameworkContextResolver
24+
{
25+
protected MSTestBaseMockingContextResolver(string assertionFailedName, string assemblyName)
26+
: base(assertionFailedName)
27+
{
28+
this.SetupStandardHierarchicalTestStructure(
29+
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute, " + assemblyName },
30+
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute, " + assemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute, " + assemblyName },
31+
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute, " + assemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute, " + assemblyName },
32+
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute, " + assemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute, " + assemblyName },
33+
FixtureConstuctorSemantics.InstanceConstructorCalledOncePerFixture);
34+
}
35+
36+
#if !SILVERLIGHT
37+
private const int DefaultGcFrequency = 50;
38+
private int createdRepoCount;
39+
private int lastGcCount;
40+
private int gcFrequency = GetGcFrequency();
41+
private bool synchronousGc = GetSynchronousGc();
42+
43+
private static int GetGcFrequency()
44+
{
45+
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "MSTestGcFrequency");
46+
int value;
47+
if (!String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value >= 1)
48+
return value;
49+
50+
return DefaultGcFrequency;
51+
}
52+
53+
private static bool GetSynchronousGc()
54+
{
55+
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "SynchronousGc");
56+
int value;
57+
return !String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value == 1;
58+
}
59+
60+
protected override void OnMocksRepositoryCreated(MocksRepository repo)
61+
{
62+
// MSTest runs every test in a different thread. We'd like to collect Thread objects often so that their handle is released.
63+
// At every N created repos (we assume that each test creates a single repo, so the number of repos created is close to
64+
// the number of threads created) do a garbage collection, but only if it hasn't been already done in this interval.
65+
66+
createdRepoCount++;
67+
68+
if (createdRepoCount % gcFrequency == 0)
69+
{
70+
var gen2Collections = GC.CollectionCount(GC.MaxGeneration);
71+
if (gen2Collections == lastGcCount)
72+
{
73+
if (synchronousGc)
74+
{
75+
GC.Collect();
76+
}
77+
else
78+
{
79+
ThreadPool.QueueUserWorkItem(_ => GC.Collect());
80+
}
81+
82+
gen2Collections++;
83+
}
84+
lastGcCount = gen2Collections;
85+
}
86+
}
87+
#endif
88+
}
89+
}

Telerik.JustMock/Core/Context/MSTestMockingContextResolver.cs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ limitations under the License.
2121

2222
namespace Telerik.JustMock.Core.Context
2323
{
24-
internal class MSTestMockingContextResolver : HierarchicalTestFrameworkContextResolver
24+
internal class MSTestMockingContextResolver : MSTestBaseMockingContextResolver
2525
{
2626
#if SILVERLIGHT
2727
private const string MstestAssemblyName = "Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight";
@@ -32,72 +32,13 @@ internal class MSTestMockingContextResolver : HierarchicalTestFrameworkContextRe
3232
private const string MstestAssertionFailedName = "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException, " + MstestAssemblyName;
3333

3434
public MSTestMockingContextResolver()
35-
: base(MstestAssertionFailedName)
35+
: base(MstestAssertionFailedName, MstestAssemblyName)
3636
{
37-
this.SetupStandardHierarchicalTestStructure(
38-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute, " + MstestAssemblyName },
39-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute, " + MstestAssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute, " + MstestAssemblyName },
40-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute, " + MstestAssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute, " + MstestAssemblyName },
41-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute, " + MstestAssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute, " + MstestAssemblyName },
42-
FixtureConstuctorSemantics.InstanceConstructorCalledOncePerFixture);
4337
}
4438

4539
public static bool IsAvailable
4640
{
4741
get { return FindType(MstestAssertionFailedName, false) != null; }
4842
}
49-
50-
#if !SILVERLIGHT
51-
private const int DefaultGcFrequency = 50;
52-
private int createdRepoCount;
53-
private int lastGcCount;
54-
private int gcFrequency = GetGcFrequency();
55-
private bool synchronousGc = GetSynchronousGc();
56-
57-
private static int GetGcFrequency()
58-
{
59-
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "MSTestGcFrequency");
60-
int value;
61-
if (!String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value >= 1)
62-
return value;
63-
64-
return DefaultGcFrequency;
65-
}
66-
67-
private static bool GetSynchronousGc()
68-
{
69-
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "SynchronousGc");
70-
int value;
71-
return !String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value == 1;
72-
}
73-
74-
protected override void OnMocksRepositoryCreated(MocksRepository repo)
75-
{
76-
// MSTest runs every test in a different thread. We'd like to collect Thread objects often so that their handle is released.
77-
// At every N created repos (we assume that each test creates a single repo, so the number of repos created is close to
78-
// the number of threads created) do a garbage collection, but only if it hasn't been already done in this interval.
79-
80-
createdRepoCount++;
81-
82-
if (createdRepoCount % gcFrequency == 0)
83-
{
84-
var gen2Collections = GC.CollectionCount(GC.MaxGeneration);
85-
if (gen2Collections == lastGcCount)
86-
{
87-
if (synchronousGc)
88-
{
89-
GC.Collect();
90-
}
91-
else
92-
{
93-
ThreadPool.QueueUserWorkItem(_ => GC.Collect());
94-
}
95-
96-
gen2Collections++;
97-
}
98-
lastGcCount = gen2Collections;
99-
}
100-
}
101-
#endif
10243
}
10344
}

Telerik.JustMock/Core/Context/MSTestV2MockingContextResolver.cs

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,78 +20,19 @@ limitations under the License.
2020

2121
namespace Telerik.JustMock.Core.Context
2222
{
23-
internal class MSTestV2MockingContextResolver : HierarchicalTestFrameworkContextResolver
23+
internal class MSTestV2MockingContextResolver : MSTestBaseMockingContextResolver
2424
{
2525
private const string Mstestv2AssemblyName = "Microsoft.VisualStudio.TestPlatform.TestFramework";
2626
private const string Mstestv2AssertionFailedName = "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException, " + Mstestv2AssemblyName;
2727

2828
public MSTestV2MockingContextResolver()
29-
: base(Mstestv2AssertionFailedName)
29+
: base(Mstestv2AssertionFailedName, Mstestv2AssemblyName)
3030
{
31-
this.SetupStandardHierarchicalTestStructure(
32-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute, " + Mstestv2AssemblyName },
33-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute, " + Mstestv2AssemblyName },
34-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute, " + Mstestv2AssemblyName },
35-
new[] { "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute, " + Mstestv2AssemblyName, "Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute, " + Mstestv2AssemblyName },
36-
FixtureConstuctorSemantics.InstanceConstructorCalledOncePerFixture);
3731
}
3832

3933
public static bool IsAvailable
4034
{
4135
get { return FindType(Mstestv2AssertionFailedName, false) != null; }
4236
}
43-
44-
private const int DefaultGcFrequency = 50;
45-
private int createdRepoCount;
46-
private int lastGcCount;
47-
private int gcFrequency = GetGcFrequency();
48-
private bool synchronousGc = GetSynchronousGc();
49-
50-
private static int GetGcFrequency()
51-
{
52-
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "MSTestGcFrequency");
53-
int value;
54-
if (!String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value >= 1)
55-
return value;
56-
57-
return DefaultGcFrequency;
58-
}
59-
60-
private static bool GetSynchronousGc()
61-
{
62-
var valueStr = SecuredRegistryMethods.GetValue(false, @"Software\Telerik\JustMock", "SynchronousGc");
63-
int value;
64-
return !String.IsNullOrEmpty(valueStr) && int.TryParse(valueStr, out value) && value == 1;
65-
}
66-
67-
#if !SILVERLIGHT
68-
protected override void OnMocksRepositoryCreated(MocksRepository repo)
69-
{
70-
// MSTest runs every test in a different thread. We'd like to collect Thread objects often so that their handle is released.
71-
// At every N created repos (we assume that each test creates a single repo, so the number of repos created is close to
72-
// the number of threads created) do a garbage collection, but only if it hasn't been already done in this interval.
73-
74-
createdRepoCount++;
75-
76-
if (createdRepoCount % gcFrequency == 0)
77-
{
78-
var gen2Collections = GC.CollectionCount(GC.MaxGeneration);
79-
if (gen2Collections == lastGcCount)
80-
{
81-
if (synchronousGc)
82-
{
83-
GC.Collect();
84-
}
85-
else
86-
{
87-
ThreadPool.QueueUserWorkItem(_ => GC.Collect());
88-
}
89-
90-
gen2Collections++;
91-
}
92-
lastGcCount = gen2Collections;
93-
}
94-
}
95-
#endif
9637
}
9738
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace Telerik.JustMock.Core.Context
5+
{
6+
internal class MSTestV4MockingContextResolver : MSTestBaseMockingContextResolver
7+
{
8+
private const string Mstestv4AssemblyName = "MSTest.TestFramework";
9+
private const string Mstestv4AssertionFailedName = "Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException, " + Mstestv4AssemblyName;
10+
11+
public MSTestV4MockingContextResolver()
12+
: base(Mstestv4AssertionFailedName, Mstestv4AssemblyName)
13+
{
14+
}
15+
16+
public static bool IsAvailable
17+
{
18+
get { return FindType(Mstestv4AssertionFailedName, false) != null; }
19+
}
20+
}
21+
}

Telerik.JustMock/Core/Context/MockingContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ static MockingContext()
234234
registeredContextResolvers.Add(new MSTestMockingContextResolver());
235235
if (MSTestV2MockingContextResolver.IsAvailable)
236236
registeredContextResolvers.Add(new MSTestV2MockingContextResolver());
237+
if (MSTestV4MockingContextResolver.IsAvailable)
238+
registeredContextResolvers.Add(new MSTestV4MockingContextResolver());
237239
#endif
238240

239241
foreach (var resolver in registeredContextResolvers)

0 commit comments

Comments
 (0)