public static void WebSocket_Http_Duplex_Buffered(NetHttpMessageEncoding messageEncoding) { EndpointAddress endpointAddress; NetHttpBinding binding = null; ClientReceiver clientReceiver = null; InstanceContext context = null; DuplexChannelFactory <IWSDuplexService> channelFactory = null; IWSDuplexService client = null; try { // *** SETUP *** \\ binding = new NetHttpBinding() { MaxReceivedMessageSize = ScenarioTestHelpers.SixtyFourMB, MaxBufferSize = ScenarioTestHelpers.SixtyFourMB, }; binding.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always; binding.MessageEncoding = messageEncoding; clientReceiver = new ClientReceiver(); context = new InstanceContext(clientReceiver); endpointAddress = new EndpointAddress(Endpoints.WebSocketHttpDuplexBuffered_Address + Enum.GetName(typeof(NetHttpMessageEncoding), messageEncoding)); channelFactory = new DuplexChannelFactory <IWSDuplexService>(context, binding, endpointAddress); client = channelFactory.CreateChannel(); // *** EXECUTE *** \\ // Invoking UploadData client.UploadData(ScenarioTestHelpers.CreateInterestingString(123)); // Invoking StartPushingData client.StartPushingData(); Assert.True(clientReceiver.ReceiveDataInvoked.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the buffered response from the Service. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); clientReceiver.ReceiveDataInvoked.Reset(); // Invoking StopPushingData client.StopPushingData(); Assert.True(clientReceiver.ReceiveDataCompleted.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the buffered response from the Service to be completed. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); clientReceiver.ReceiveDataCompleted.Reset(); // Getting results from server via callback. client.GetLog(); Assert.True(clientReceiver.LogReceived.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the Logging from the Service to be received. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); // *** VALIDATE *** \\ Assert.True(clientReceiver.ServerLog.Count > 0, "The logging done by the Server was not returned via the Callback."); // *** CLEANUP *** \\ ((ICommunicationObject)client).Close(); channelFactory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)client, channelFactory); } }
public static void WebSocket_Http_Duplex_BinaryStreamed() { NetHttpBinding binding = null; ClientReceiver clientReceiver = null; InstanceContext context = null; DuplexChannelFactory <IWSDuplexService> channelFactory = null; IWSDuplexService client = null; FlowControlledStream uploadStream = null; try { // *** SETUP *** \\ binding = new NetHttpBinding() { MaxReceivedMessageSize = ScenarioTestHelpers.SixtyFourMB, MaxBufferSize = ScenarioTestHelpers.SixtyFourMB, }; binding.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always; binding.TransferMode = TransferMode.Streamed; binding.MessageEncoding = NetHttpMessageEncoding.Binary; clientReceiver = new ClientReceiver(); context = new InstanceContext(clientReceiver); channelFactory = new DuplexChannelFactory <IWSDuplexService>(context, binding, Endpoints.WebSocketHttpDuplexBinaryStreamed_Address); client = channelFactory.CreateChannel(); // *** EXECUTE *** \\ using (Stream stream = client.DownloadStream()) { int readResult; // Read from the stream, 1000 bytes at a time. byte[] buffer = new byte[1000]; do { readResult = stream.Read(buffer, 0, buffer.Length); }while (readResult != 0); } uploadStream = new FlowControlledStream(); uploadStream.ReadThrottle = TimeSpan.FromMilliseconds(500); uploadStream.StreamDuration = TimeSpan.FromSeconds(1); client.UploadStream(uploadStream); client.StartPushingStream(); // Wait for the callback to get invoked before telling the service to stop streaming. // This ensures we can read from the stream on the callback while the NCL layer at the service // is still writing the bytes from the stream to the wire. // This will deadlock if the transfer mode is buffered because the callback will wait for the // stream, and the NCL layer will continue to buffer the stream until it reaches the end. Assert.True(clientReceiver.ReceiveStreamInvoked.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the stream response from the Service. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); clientReceiver.ReceiveStreamInvoked.Reset(); // Upload the stream while we are downloading a different stream uploadStream = new FlowControlledStream(); uploadStream.ReadThrottle = TimeSpan.FromMilliseconds(500); uploadStream.StreamDuration = TimeSpan.FromSeconds(1); client.UploadStream(uploadStream); client.StopPushingStream(); // Waiting on ReceiveStreamCompleted from the ClientReceiver. Assert.True(clientReceiver.ReceiveStreamCompleted.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the stream response from the Service to be completed. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); clientReceiver.ReceiveStreamCompleted.Reset(); // Getting results from server via callback. client.GetLog(); Assert.True(clientReceiver.LogReceived.WaitOne(ScenarioTestHelpers.TestTimeout), String.Format("Test case timeout was reached while waiting for the Logging from the Service to be received. Timeout was: {0}", ScenarioTestHelpers.TestTimeout)); // *** VALIDATE *** \\ Assert.True(clientReceiver.ServerLog.Count > 0, "The logging done by the Server was not returned via the Callback."); // *** CLEANUP *** \\ ((ICommunicationObject)client).Close(); channelFactory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)client, channelFactory); clientReceiver.Dispose(); } }
public static void WebSocket_Https_Duplex_BinaryStreamed() { BinaryMessageEncodingBindingElement binaryMessageEncodingBindingElement = null; HttpsTransportBindingElement httpsTransportBindingElement = null; CustomBinding binding = null; ClientReceiver clientReceiver = null; InstanceContext context = null; DuplexChannelFactory <IWSDuplexService> channelFactory = null; IWSDuplexService client = null; FlowControlledStream uploadStream = null; try { // *** SETUP *** \\ binaryMessageEncodingBindingElement = new BinaryMessageEncodingBindingElement(); httpsTransportBindingElement = new HttpsTransportBindingElement(); httpsTransportBindingElement.WebSocketSettings.TransportUsage = WebSocketTransportUsage.Always; httpsTransportBindingElement.MaxReceivedMessageSize = ScenarioTestHelpers.DefaultMaxReceivedMessageSize; httpsTransportBindingElement.MaxBufferSize = ScenarioTestHelpers.DefaultMaxReceivedMessageSize; httpsTransportBindingElement.TransferMode = TransferMode.Streamed; binding = new CustomBinding(binaryMessageEncodingBindingElement, httpsTransportBindingElement); clientReceiver = new ClientReceiver(); context = new InstanceContext(clientReceiver); channelFactory = new DuplexChannelFactory <IWSDuplexService>(context, binding, Endpoints.WebSocketHttpsDuplexBinaryStreamed_Address); client = channelFactory.CreateChannel(); // *** EXECUTE *** \\ using (Stream stream = client.DownloadStream()) { int readResult; // Read from the stream, 1000 bytes at a time. byte[] buffer = new byte[1000]; do { readResult = stream.Read(buffer, 0, buffer.Length); }while (readResult != 0); } uploadStream = new FlowControlledStream(); uploadStream.ReadThrottle = TimeSpan.FromMilliseconds(500); uploadStream.StreamDuration = TimeSpan.FromSeconds(1); client.UploadStream(uploadStream); client.StartPushingStream(); // Wait for the callback to get invoked before telling the service to stop streaming. // This ensures we can read from the stream on the callback while the NCL layer at the service // is still writing the bytes from the stream to the wire. // This will deadlock if the transfer mode is buffered because the callback will wait for the // stream, and the NCL layer will continue to buffer the stream until it reaches the end. Assert.True(clientReceiver.ReceiveStreamInvoked.WaitOne(ScenarioTestHelpers.TestTimeout), "Test case timeout was reached while waiting for the stream response from the Service."); clientReceiver.ReceiveStreamInvoked.Reset(); // Upload the stream while we are downloading a different stream uploadStream = new FlowControlledStream(); uploadStream.ReadThrottle = TimeSpan.FromMilliseconds(500); uploadStream.StreamDuration = TimeSpan.FromSeconds(1); client.UploadStream(uploadStream); client.StopPushingStream(); // Waiting on ReceiveStreamCompleted from the ClientReceiver. clientReceiver.ReceiveStreamCompleted.WaitOne(); clientReceiver.ReceiveStreamCompleted.Reset(); // *** VALIDATE *** \\ // Validation is based on no exceptions being thrown. // *** CLEANUP *** \\ ((ICommunicationObject)client).Close(); channelFactory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)client, channelFactory); clientReceiver.Dispose(); } }