public void SimulateTimeout() { var gate = new AutoResetEvent(false); var testWeb = "Http://localhost:21266"; var client = new ClientBase(new Serializer()); client.BeginRequest(RequestMethod.GET, testWeb, "/DelayHandler.ashx", null, null, ContentType.TEXT, ContentType.TEXT, TimeSpan.FromSeconds(1), 3000, 0, ar => { Console.WriteLine("we are in the callback about to end the request"); // any time your code utilizes network resources you need to be ready to handle exceptions. try { var response = client.EndRequest(ar); Console.WriteLine("you will not see this line output, exception had already been triggered by .EndRequest()"); } catch (TimeoutException ex) { Console.Write("our code properly caught a timeout exception:\r\n" + ex.ToString()); } gate.Set(); }, null); gate.WaitOne(20000); client.Dispose(); }
public void TestRecorder() { var client = new ClientBase(new Serializer()); var recorder = new Recorder(client); recorder.Start(); var gate = new AutoResetEvent(false); Exception exception = null; FooClass result = null; Guid id = client.BeginRequest(RequestMethod.GET, "http://api.geonames.org", "/citiesJSON?north={north}&south={south}&east{east}&west={west}&lang={lang}&username={username}", new Dictionary<string, string>(), new Dictionary<string, object> { {"north",44.1}, {"south",-9.9}, {"east",-22.4}, {"west",55.2}, {"lang","de"}, {"username","demo"} }, ContentType.TEXT, ContentType.JSON, TimeSpan.FromSeconds(1), 3000, 0, ar => { try { result = client.EndRequest<FooClass>(ar); var responsetext = ar.ResponseText; } catch (Exception ex) { exception = ex; } gate.Set(); }, null); if (!gate.WaitOne(10000)) { throw new Exception("timed out"); } // verify cache has purged gate.WaitOne(3000); if (exception != null) { Assert.Fail(exception.Message); } recorder.Stop(); List<RequestInfoBase> recorded = recorder.GetRequests(); recorder.Dispose(); Assert.IsTrue(recorded.Count == 1); var recordedJson = client.Serializer.SerializeObject(recorded); List<RequestInfoBase> deserializedRecording = client.Serializer.DeserializeObject<List<RequestInfoBase>>(recordedJson); Assert.IsTrue(deserializedRecording.Count == 1); }
public void Test() { var client = new ClientBase(new Serializer()); var gate = new AutoResetEvent(false); Exception exception = null; FooClass result = null; Guid id = client.BeginRequest(RequestMethod.GET, "http://api.geonames.org", "/citiesJSON?north={north}&south={south}&east{east}&west={west}&lang={lang}&username={username}", new Dictionary<string, object> { {"north",44.1}, {"south",-9.9}, {"east",-22.4}, {"west",55.2}, {"lang","de"}, {"username","demo"} }, ContentType.TEXT, ContentType.JSON, TimeSpan.FromSeconds(1), 3000, 0, ar => { try { result = client.EndRequest<FooClass>(ar); var responsetext = ar.ResponseText; } catch (Exception ex) { exception = ex; } gate.Set(); }, null); if (!gate.WaitOne(10000)) { throw new Exception("timed out"); } // verify cache has purged gate.WaitOne(3000); if (exception != null) { Assert.Fail(exception.Message); } var output = GetLogOutput(); }