Skip to content

Commit 8ed7a93

Browse files
Merge pull request #84 from rabbitmq/rabbitmq-dotnet-client-72
Make WinRT compile with recent heartbeat implementation changes
2 parents b401b3d + c22b55a commit 8ed7a93

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

projects/client/RabbitMQ.Client.WinRT/src/client/impl/SocketFrameHandler.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,21 @@
3838
// Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.
3939
//---------------------------------------------------------------------------
4040

41+
using RabbitMQ.Client.Exceptions;
42+
using RabbitMQ.Util;
4143
using System;
4244
using System.IO;
4345
using System.Text;
44-
4546
using System.Threading;
46-
4747
using Windows.Foundation;
4848
using Windows.Networking;
4949
using Windows.Networking.Sockets;
5050

51-
using RabbitMQ.Util;
52-
using RabbitMQ.Client.Exceptions;
53-
5451
namespace RabbitMQ.Client.Impl
5552
{
5653
/// <summary>
57-
/// Implement <see cref="IFrameHandler"/> for WinRT. The significant
58-
/// difference is that TcpClient is not available and the new
54+
/// Implement <see cref="IFrameHandler"/> for WinRT. The significant
55+
/// difference is that TcpClient is not available and the new
5956
/// <see cref="StreamSocket"/> needs to be used.
6057
/// </summary>
6158
public class SocketFrameHandler : IFrameHandler
@@ -64,7 +61,6 @@ public class SocketFrameHandler : IFrameHandler
6461
public const int SOCKET_CLOSING_TIMEOUT = 1;
6562

6663
public StreamSocket m_socket;
67-
private CancellationTokenSource cts;
6864
public NetworkBinaryReader m_reader;
6965
public NetworkBinaryWriter m_writer;
7066
private bool _closed = false;
@@ -86,10 +82,10 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
8682
IAsyncAction ar = null;
8783
try
8884
{
89-
cts = new CancellationTokenSource();
85+
var cts = new CancellationTokenSource();
9086
if (this.defaultTimeout.HasValue)
9187
cts.CancelAfter(this.defaultTimeout.Value);
92-
88+
9389
ar = this.m_socket.UpgradeToSslAsync(
9490
SocketProtectionLevel.Ssl, new HostName(endpoint.Ssl.ServerName));
9591
ar.AsTask(cts.Token).Wait();
@@ -122,6 +118,7 @@ public int LocalPort
122118
return port;
123119
}
124120
}
121+
125122
public int RemotePort
126123
{
127124
get
@@ -137,11 +134,6 @@ public int Timeout
137134
{
138135
set
139136
{
140-
defaultTimeout = value;
141-
if (cts != null && cts.Token.CanBeCanceled)
142-
{
143-
cts.CancelAfter(value);
144-
}
145137
}
146138
}
147139

@@ -204,7 +196,7 @@ private void Connect(StreamSocket socket, AmqpTcpEndpoint endpoint, int timeout)
204196
IAsyncAction ar = null;
205197
try
206198
{
207-
cts = new CancellationTokenSource();
199+
var cts = new CancellationTokenSource();
208200
if (this.defaultTimeout.HasValue)
209201
cts.CancelAfter(this.defaultTimeout.Value);
210202

@@ -239,4 +231,4 @@ private void Connect(StreamSocket socket, AmqpTcpEndpoint endpoint, int timeout)
239231
}
240232
}
241233
}
242-
}
234+
}

projects/client/RabbitMQ.Client/src/client/impl/Connection.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@
4747
using System.IO;
4848

4949
#if NETFX_CORE
50+
5051
using System.Threading.Tasks;
5152
using Windows.Networking.Sockets;
5253
using Windows.ApplicationModel;
54+
5355
#else
5456
using System.Net;
5557
using System.Net.Sockets;
@@ -121,14 +123,14 @@ public Connection(IConnectionFactory factory, bool insist, IFrameHandler frameHa
121123
StartMainLoop(factory.UseBackgroundThreadsForIO);
122124
Open(insist);
123125
StartHeartbeatTimer();
124-
126+
125127
#if NETFX_CORE
126128
#pragma warning disable 0168
127129
try
128130
{
129131
Windows.UI.Xaml.Application.Current.Suspending += this.HandleApplicationSuspend;
130132
}
131-
catch(Exception ex)
133+
catch (Exception ex)
132134
{
133135
// If called from a desktop app (i.e. unit tests), then there is no current application
134136
}
@@ -316,7 +318,7 @@ public static IDictionary<string, object> DefaultClientProperties()
316318
{
317319
Assembly assembly =
318320
#if NETFX_CORE
319-
System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(Connection)).Assembly;
321+
System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(Connection)).Assembly;
320322
#else
321323
System.Reflection.Assembly.GetAssembly(typeof(Connection));
322324
#endif
@@ -511,13 +513,15 @@ public void FinishClose()
511513
}
512514

513515
#if NETFX_CORE
516+
514517
/// <remarks>
515518
/// We need to close the socket, otherwise suspending the application will take the maximum time allowed
516519
/// </remarks>
517520
public void HandleApplicationSuspend(object sender, SuspendingEventArgs suspendingEventArgs)
518521
{
519522
Abort(Constants.InternalError, "Application Suspend");
520523
}
524+
521525
#else
522526
/// <remarks>
523527
/// We need to close the socket, otherwise attempting to unload the domain
@@ -588,7 +592,6 @@ public void HeartbeatWriteTimerCallback(object state)
588592
e));
589593
shouldTerminate = true;
590594
}
591-
592595
if (m_closed || shouldTerminate)
593596
{
594597
TerminateMainloop();
@@ -668,7 +671,7 @@ public void MainLoop()
668671
try
669672
{
670673
ClosingLoop();
671-
}
674+
}
672675
#if NETFX_CORE
673676
catch (Exception ex)
674677
{
@@ -680,7 +683,7 @@ public void MainLoop()
680683
}
681684
else
682685
{
683-
throw;
686+
throw ex;
684687
}
685688
}
686689
#else
@@ -756,10 +759,12 @@ public void MainLoopIteration()
756759
}
757760
}
758761
}
762+
#if !NETFX_CORE
759763
catch (SocketException ioe)
760764
{
761765
HandleIOException(ioe);
762766
}
767+
#endif
763768
catch (IOException ioe)
764769
{
765770
HandleIOException(ioe);
@@ -943,7 +948,7 @@ public void PrettyPrintShutdownReport()
943948
#else
944949
Console.Error.WriteLine(
945950
#endif
946-
"No errors reported when closing connection {0}", this);
951+
"No errors reported when closing connection {0}", this);
947952
}
948953
else
949954
{
@@ -952,15 +957,15 @@ public void PrettyPrintShutdownReport()
952957
#else
953958
Console.Error.WriteLine(
954959
#endif
955-
"Log of errors while closing connection {0}:", this);
960+
"Log of errors while closing connection {0}:", this);
956961
foreach (ShutdownReportEntry entry in ShutdownReport)
957962
{
958963
#if NETFX_CORE
959-
System.Diagnostics.Debug.WriteLine(
964+
System.Diagnostics.Debug.WriteLine(
960965
#else
961966
Console.Error.WriteLine(
962967
#endif
963-
entry.ToString());
968+
entry.ToString());
964969
}
965970
}
966971
}
@@ -1302,4 +1307,4 @@ private static uint NegotiatedMaxValue(uint clientValue, uint serverValue)
13021307
Math.Min(clientValue, serverValue);
13031308
}
13041309
}
1305-
}
1310+
}

projects/client/RabbitMQ.Client/src/client/impl/Frame.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
#if NETFX_CORE
4848
using Windows.Networking.Sockets;
4949
#else
50+
5051
using System.Net.Sockets;
52+
5153
#endif
5254

5355
namespace RabbitMQ.Client.Impl
@@ -145,7 +147,6 @@ public static Frame ReadFrom(NetworkBinaryReader reader)
145147
throw ioe;
146148
}
147149
throw ioe.InnerException;
148-
149150
#endif
150151
}
151152

projects/client/Unit/src/unit/TestConsumerOperationDispatch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,16 @@ internal class TestConsumerOperationDispatch : IntegrationFixture
5757
private List<CollectingConsumer> consumers = new List<CollectingConsumer>();
5858

5959
// number of channels (and consumers)
60-
private const int y = 200;
60+
private const int y = 100;
6161

6262
// number of messages to be published
63-
private const int n = 250;
63+
private const int n = 100;
6464

6565
public static CountdownEvent counter = new CountdownEvent(y);
6666

6767
[TearDown]
6868
protected override void ReleaseResources()
6969
{
70-
base.ReleaseResources();
7170
foreach (var ch in channels)
7271
{
7372
if (ch.IsOpen)
@@ -78,6 +77,7 @@ protected override void ReleaseResources()
7877
queues.Clear();
7978
consumers.Clear();
8079
counter.Reset();
80+
base.ReleaseResources();
8181
}
8282

8383
private class CollectingConsumer : DefaultBasicConsumer

projects/client/Unit/src/unit/TestHeartbeats.cs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,17 @@
4444
using System.Collections.Generic;
4545
using System.Threading;
4646

47+
#if !NETFX_CORE
4748
namespace RabbitMQ.Client.Unit
4849
{
4950
[TestFixture]
5051
internal class TestHeartbeats : IntegrationFixture
5152
{
5253
private const UInt16 heartbeatTimeout = 2;
5354

54-
[Test]
55-
[Category("LongRunning")]
55+
[Test, Category("LongRunning"), Timeout(35000)]
5656
public void TestThatHeartbeatWriterUsesConfigurableInterval()
5757
{
58-
if (!LongRunningTestsEnabled())
59-
{
60-
Console.WriteLine("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
61-
return;
62-
}
63-
6458
var cf = new ConnectionFactory()
6559
{
6660
RequestedHeartbeat = heartbeatTimeout,
@@ -89,16 +83,9 @@ public void TestThatHeartbeatWriterUsesConfigurableInterval()
8983
conn.Close();
9084
}
9185

92-
[Test]
93-
[Category("LongRunning")]
86+
[Test, Category("LongRunning"), Timeout(65000)]
9487
public void TestHundredsOfConnectionsWithRandomHeartbeatInterval()
9588
{
96-
if (!LongRunningTestsEnabled())
97-
{
98-
Console.WriteLine("RABBITMQ_LONG_RUNNING_TESTS is not set, skipping test");
99-
return;
100-
}
101-
10289
var rnd = new Random();
10390
List<IConnection> xs = new List<IConnection>();
10491
for (var i = 0; i < 200; i++)
@@ -135,20 +122,11 @@ private void CheckInitiator(ShutdownEventArgs evt)
135122
}
136123
}
137124

138-
private bool LongRunningTestsEnabled()
139-
{
140-
var s = Environment.GetEnvironmentVariable("RABBITMQ_LONG_RUNNING_TESTS");
141-
if (s == null || s.Equals(""))
142-
{
143-
return false;
144-
}
145-
return true;
146-
}
147-
148125
private void SleepFor(int t)
149126
{
150127
Console.WriteLine("Testing heartbeats, sleeping for {0} seconds", t);
151128
Thread.Sleep(t * 1000);
152129
}
153130
}
154-
}
131+
}
132+
#endif

0 commit comments

Comments
 (0)