public void Send_ExecutesDelegate_NoThrow() { var syncContext = new SingleThreadedSynchronizationContext(); int?executingThread = null; syncContext.Send(s => executingThread = Environment.CurrentManagedThreadId, null); Assert.Equal(Environment.CurrentManagedThreadId, executingThread); }
public void TesSend() { var sendComplete = false; var sync = new SingleThreadedSynchronizationContext(); sync.Send(state => { sendComplete = true; }, null); Assert.IsTrue(sendComplete); }
public void Send_OnDifferentThread_ExecutesDelegateAndWaits() { int originalThreadId = Environment.CurrentManagedThreadId; var syncContext = new SingleThreadedSynchronizationContext(); var frame = new SingleThreadedSynchronizationContext.Frame(); var task = Task.Run(delegate { try { int?observedThreadId = null; syncContext.Send(s => observedThreadId = Environment.CurrentManagedThreadId, null); Assert.Equal(originalThreadId, observedThreadId); } finally { frame.Continue = false; } }); syncContext.PushFrame(frame); task.GetAwaiter().GetResult(); }
public static void SendAsyncHttpClientRequests(HttpClient client, bool tracingDisabled, string url, string requestContent) { void Invoke() { // Insert a call to the Tracer.Instance to include an AssemblyRef to Datadog.Trace assembly in the final executable Console.WriteLine($"[HttpClient] sending requests to {url}"); if (tracingDisabled) { client.DefaultRequestHeaders.Add(HttpHeaderNames.TracingEnabled, "false"); } using (Tracer.Instance.StartActive("HttpClientRequestAsync")) { using (Tracer.Instance.StartActive("DeleteAsync")) { client.DeleteAsync(url).Wait(); Console.WriteLine("Received response for client.DeleteAsync(String)"); client.DeleteAsync(new Uri(url)).Wait(); Console.WriteLine("Received response for client.DeleteAsync(Uri)"); client.DeleteAsync(url, CancellationToken.None).Wait(); Console.WriteLine("Received response for client.DeleteAsync(String, CancellationToken)"); client.DeleteAsync(new Uri(url), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.DeleteAsync(Uri, CancellationToken)"); } using (Tracer.Instance.StartActive("GetAsync")) { client.GetAsync(url).Wait(); Console.WriteLine("Received response for client.GetAsync(String)"); client.GetAsync(new Uri(url)).Wait(); Console.WriteLine("Received response for client.GetAsync(Uri)"); client.GetAsync(url, CancellationToken.None).Wait(); Console.WriteLine("Received response for client.GetAsync(String, CancellationToken)"); client.GetAsync(new Uri(url), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.GetAsync(Uri, CancellationToken)"); client.GetAsync(url, HttpCompletionOption.ResponseContentRead).Wait(); Console.WriteLine("Received response for client.GetAsync(String, HttpCompletionOption)"); client.GetAsync(new Uri(url), HttpCompletionOption.ResponseContentRead).Wait(); Console.WriteLine("Received response for client.GetAsync(Uri, HttpCompletionOption)"); client.GetAsync(url, HttpCompletionOption.ResponseContentRead, CancellationToken.None).Wait(); Console.WriteLine("Received response for client.GetAsync(String, HttpCompletionOption, CancellationToken)"); client.GetAsync(new Uri(url), HttpCompletionOption.ResponseContentRead, CancellationToken.None).Wait(); Console.WriteLine("Received response for client.GetAsync(Uri, HttpCompletionOption, CancellationToken)"); } using (Tracer.Instance.StartActive("GetByteArrayAsync")) { client.GetByteArrayAsync(url).Wait(); Console.WriteLine("Received response for client.GetByteArrayAsync(String)"); client.GetByteArrayAsync(new Uri(url)).Wait(); Console.WriteLine("Received response for client.GetByteArrayAsync(Uri)"); } using (Tracer.Instance.StartActive("GetStreamAsync")) { using Stream stream1 = client.GetStreamAsync(url).Result; Console.WriteLine("Received response for client.GetStreamAsync(String)"); using Stream stream2 = client.GetStreamAsync(new Uri(url)).Result; Console.WriteLine("Received response for client.GetStreamAsync(Uri)"); } using (Tracer.Instance.StartActive("GetStringAsync")) { client.GetStringAsync(url).Wait(); Console.WriteLine("Received response for client.GetStringAsync(String)"); client.GetStringAsync(new Uri(url)).Wait(); Console.WriteLine("Received response for client.GetStringAsync(Uri)"); } #if NETCOREAPP using (Tracer.Instance.StartActive("PatchAsync")) { client.PatchAsync(url, new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PatchAsync(String, HttpContent)"); client.PatchAsync(new Uri(url), new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PatchAsync(Uri, HttpContent)"); client.PatchAsync(url, new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PatchAsync(String, HttpContent, CancellationToken)"); client.PatchAsync(new Uri(url), new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PatchAsync(Uri, HttpContent, CancellationToken)"); } #endif using (Tracer.Instance.StartActive("PostAsync")) { client.PostAsync(url, new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PostAsync(String, HttpContent)"); client.PostAsync(new Uri(url), new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PostAsync(Uri, HttpContent)"); client.PostAsync(url, new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PostAsync(String, HttpContent, CancellationToken)"); client.PostAsync(new Uri(url), new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PostAsync(Uri, HttpContent, CancellationToken)"); } using (Tracer.Instance.StartActive("PutAsync")) { client.PutAsync(url, new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PutAsync(String, HttpContent)"); client.PutAsync(new Uri(url), new StringContent(requestContent, Utf8)).Wait(); Console.WriteLine("Received response for client.PutAsync(Uri, HttpContent)"); client.PutAsync(url, new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PutAsync(String, HttpContent, CancellationToken)"); client.PutAsync(new Uri(url), new StringContent(requestContent, Utf8), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.PutAsync(Uri, HttpContent, CancellationToken)"); } using (Tracer.Instance.StartActive("SendAsync")) { client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url)).Wait(); Console.WriteLine("Received response for client.SendAsync(HttpRequestMessage)"); client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), CancellationToken.None).Wait(); Console.WriteLine("Received response for client.SendAsync(HttpRequestMessage, CancellationToken)"); client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), HttpCompletionOption.ResponseContentRead).Wait(); Console.WriteLine("Received response for client.SendAsync(HttpRequestMessage, HttpCompletionOption)"); client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url), HttpCompletionOption.ResponseContentRead, CancellationToken.None).Wait(); Console.WriteLine("Received response for client.SendAsync(HttpRequestMessage, HttpCompletionOption, CancellationToken)"); } using (Tracer.Instance.StartActive("ErrorSpanBelow")) { client.GetAsync($"{url}HttpErrorCode").Wait(); Console.WriteLine("Received response for client.GetAsync Error Span"); } } } // Wait for the tasks in a single threaded synchronization context, to detect potential deadlocks var synchronizationContext = new SingleThreadedSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(synchronizationContext); synchronizationContext.Send(_ => Invoke(), null); SynchronizationContext.SetSynchronizationContext(null); }