summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/src/baseservices/threading/coverage/OSThreadId/OSThreadId.cs98
-rw-r--r--tests/src/baseservices/threading/coverage/OSThreadId/osthreadid.csproj31
-rw-r--r--tests/src/tracing/runtimeeventsource/RuntimeEventSourceTest.cs23
3 files changed, 151 insertions, 1 deletions
diff --git a/tests/src/baseservices/threading/coverage/OSThreadId/OSThreadId.cs b/tests/src/baseservices/threading/coverage/OSThreadId/OSThreadId.cs
new file mode 100644
index 0000000000..85895a7189
--- /dev/null
+++ b/tests/src/baseservices/threading/coverage/OSThreadId/OSThreadId.cs
@@ -0,0 +1,98 @@
+using System;
+using System.Diagnostics;
+using System.Reflection;
+using System.Threading;
+
+namespace Threading.Tests
+{
+ public sealed class OSThreadId
+ {
+ private const int NumThreads = 10;
+ private static MethodInfo s_osThreadIdGetMethod;
+ private static ManualResetEvent s_resetEvent = new ManualResetEvent(false);
+ private static ulong[] s_threadIds = new ulong[NumThreads];
+
+ public static int Main(string[] args)
+ {
+ // The property to be tested is internal.
+ Type runtimeThreadType = typeof(object).Assembly.GetType("Internal.Runtime.Augments.RuntimeThread");
+ Assert(runtimeThreadType != null);
+ PropertyInfo osThreadIdProperty = runtimeThreadType.GetProperty("CurrentOSThreadId", BindingFlags.NonPublic | BindingFlags.Static);
+ Assert(osThreadIdProperty != null);
+ s_osThreadIdGetMethod = osThreadIdProperty.GetGetMethod(true);
+ Assert(s_osThreadIdGetMethod != null);
+
+ // Test the main thread.
+ Assert(GetCurrentThreadId() > 0);
+
+ // Create more threads.
+ Thread[] threads = new Thread[NumThreads];
+ for (int i = 0; i < NumThreads; i++)
+ {
+ threads[i] = new Thread(new ParameterizedThreadStart(ThreadProc));
+ threads[i].Start(i);
+ }
+
+ // Now that all threads have been created, allow them to run.
+ s_resetEvent.Set();
+
+ // Wait for all threads to complete.
+ for (int i = 0; i < NumThreads; i++)
+ {
+ threads[i].Join();
+ }
+
+ // Check for duplicate thread IDs.
+ Array.Sort(s_threadIds);
+ ulong previousThreadId = 0;
+ for (int i = 0; i < NumThreads; i++)
+ {
+ if (i == 0)
+ {
+ previousThreadId = s_threadIds[i];
+ }
+ else
+ {
+ Assert(s_threadIds[i] > 0);
+ Assert(previousThreadId != s_threadIds[i]);
+ previousThreadId = s_threadIds[i];
+ }
+ }
+
+ return 100;
+ }
+
+ private static ulong GetCurrentThreadId()
+ {
+ return (ulong)s_osThreadIdGetMethod.Invoke(null, null);
+ }
+
+ private static void ThreadProc(object state)
+ {
+ // Get the thread index.
+ int threadIndex = (int)state;
+ Assert(threadIndex >= 0 && threadIndex < NumThreads);
+
+ // Wait for all threads to be created.
+ s_resetEvent.WaitOne();
+
+ // We now know that all threads were created before GetCurrentThread is called.
+ // Thus, no thread IDs can be duplicates.
+ ulong threadId = GetCurrentThreadId();
+
+ // Ensure that the thread ID is valid.
+ Assert(threadId > 0);
+
+ // Save the thread ID so that it can be checked for duplicates.
+ s_threadIds[threadIndex] = threadId;
+ }
+
+ private static void Assert(bool condition)
+ {
+ if (!condition)
+ {
+ throw new Exception("Assertion failed.");
+ }
+ }
+ }
+}
diff --git a/tests/src/baseservices/threading/coverage/OSThreadId/osthreadid.csproj b/tests/src/baseservices/threading/coverage/OSThreadId/osthreadid.csproj
new file mode 100644
index 0000000000..6d263047e1
--- /dev/null
+++ b/tests/src/baseservices/threading/coverage/OSThreadId/osthreadid.csproj
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{8E3244CB-407F-4142-BAAB-E7A55901A5FA}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+ <CLRTestKind>BuildAndRun</CLRTestKind>
+ <DefineConstants>$(DefineConstants);STATIC</DefineConstants>
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+ <CLRTestPriority>0</CLRTestPriority>
+ </PropertyGroup>
+ <!-- Default configurations to help VS understand the configurations -->
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
+ </PropertyGroup>
+ <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
+ </PropertyGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="OSThreadId.cs" />
+ </ItemGroup>
+ <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+</Project>
diff --git a/tests/src/tracing/runtimeeventsource/RuntimeEventSourceTest.cs b/tests/src/tracing/runtimeeventsource/RuntimeEventSourceTest.cs
index 94d358515f..fdaae9ede0 100644
--- a/tests/src/tracing/runtimeeventsource/RuntimeEventSourceTest.cs
+++ b/tests/src/tracing/runtimeeventsource/RuntimeEventSourceTest.cs
@@ -1,3 +1,5 @@
+#define REFLECTION
+
using System;
using System.IO;
using System.Diagnostics.Tracing;
@@ -5,6 +7,10 @@ using System.Runtime.CompilerServices;
using System.Threading;
using Tracing.Tests.Common;
+#if REFLECTION
+using System.Reflection;
+#endif
+
namespace Tracing.Tests
{
public sealed class RuntimeEventSourceTest
@@ -76,7 +82,22 @@ namespace Tracing.Tests
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
- Console.WriteLine($"[{m_name}] ID = {eventData.EventId} Name = {eventData.EventName}");
+ long osThreadId = -1;
+ DateTime timeStamp;
+#if REFLECTION
+ PropertyInfo threadProperty = typeof(EventWrittenEventArgs).GetProperty("OSThreadId");
+ MethodInfo threadMethod = threadProperty.GetGetMethod();
+ osThreadId = (long)threadMethod.Invoke(eventData, null);
+ PropertyInfo timeStampProperty = typeof(EventWrittenEventArgs).GetProperty("TimeStamp");
+ MethodInfo timeStampMethod = timeStampProperty.GetGetMethod();
+ timeStamp = (DateTime)timeStampMethod.Invoke(eventData, null);
+#endif
+
+ Console.WriteLine($"[{m_name}] ThreadID = {osThreadId} ID = {eventData.EventId} Name = {eventData.EventName}");
+ Console.WriteLine($"TimeStamp: {timeStamp.ToLocalTime()}");
+ Console.WriteLine($"LocalTime: {DateTime.Now}");
+ Console.WriteLine($"Difference: {DateTime.UtcNow - timeStamp}");
+ Assert.True("timeStamp < DateTime.UtcNow", timeStamp < DateTime.UtcNow);
for (int i = 0; i < eventData.Payload.Count; i++)
{
string payloadString = eventData.Payload[i] != null ? eventData.Payload[i].ToString() : string.Empty;