Skip to content

Commit

Permalink
sample to test pending callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Armenta committed Aug 15, 2013
1 parent a0331ca commit e0a561b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
57 changes: 56 additions & 1 deletion samples/Common/CommonClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client.Hubs;

Expand Down Expand Up @@ -156,7 +157,61 @@ private async Task RunHeaderAuthHub(string url)

await hubConnection.Start();
hubConnection.TraceWriter.WriteLine("transport.Name={0}", hubConnection.Transport.Name);
}
}

private async Task RunPendingCallbacks(string url)
{
var hubConnection = new HubConnection(url);
hubConnection.TraceWriter = _traceWriter;
hubConnection.TraceLevel = TraceLevels.StateChanges;

var hubProxy = hubConnection.CreateHubProxy("LongRunningHub");
ManualResetEvent event1 = new ManualResetEvent(false);
ManualResetEvent event2 = new ManualResetEvent(false);

int callbacks = 1000;
int counter = 0;
hubProxy.On<int>("serverIsWaiting", (i) =>
{
if (i % 100 == 0)
{
hubConnection.TraceWriter.WriteLine("{0} serverIsWaiting: {1}", DateTime.Now, i);
}
if (i == callbacks)
{
event1.Set();
}
});

await hubConnection.Start();
await hubProxy.Invoke("Reset");

hubConnection.TraceWriter.WriteLine("check memory size before sending longRunning");

for (int messageNumber = 1; messageNumber <= callbacks; messageNumber++)
{
hubProxy.Invoke("LongRunningMethod", messageNumber).ContinueWith(task =>
{
int i = Interlocked.Increment(ref counter);
if (i % 100 == 0)
{
hubConnection.TraceWriter.WriteLine("{0} completed: {1} task.Status={2}", DateTime.Now, i, task.Status);
}
if (i == callbacks)
{
event2.Set();
}
});
}

await Task.Factory.StartNew(() => event1.WaitOne());
hubConnection.TraceWriter.WriteLine("check memory size after sending longRunning");
await hubProxy.Invoke("Set");
await Task.Factory.StartNew(() => event2.WaitOne());
hubConnection.TraceWriter.WriteLine("check memory size after all callbacks completed");
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNet.SignalR.Samples.Hubs.Test
{
public class LongRunningHub : Hub
{
private static ManualResetEvent myEvent = new ManualResetEvent(false);

public void Set()
{
myEvent.Set();
}

public void Reset()
{
myEvent.Reset();
}

public Task LongRunningMethod(int i)
{
Clients.Caller.serverIsWaiting(i).Wait();

return Task.Run(() =>
{
myEvent.WaitOne();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
<Compile Include="Hubs\RealtimeBroadcast\HighFrequencyTimer.cs" />
<Compile Include="Hubs\RealtimeBroadcast\Realtime.cs" />
<Compile Include="Hubs\ShapeShare\ShapeShare.cs" />
<Compile Include="Hubs\Test\LongRunningHub.cs" />
<Compile Include="Hubs\Test\Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
Expand Down
20 changes: 20 additions & 0 deletions samples/Microsoft.AspNet.SignalR.Samples/Scripts/hubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,26 @@
}
};

proxies.longRunningHub = this.createHubProxy('longRunningHub');
proxies.longRunningHub.client = { };
proxies.longRunningHub.server = {
longRunningMethod: function (i) {
/// <summary>Calls the LongRunningMethod method on the server-side LongRunningHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
/// <param name=\"i\" type=\"Number\">Server side type is System.Int32</param>
return proxies.longRunningHub.invoke.apply(proxies.longRunningHub, $.merge(["LongRunningMethod"], $.makeArray(arguments)));
},

reset: function () {
/// <summary>Calls the Reset method on the server-side LongRunningHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
return proxies.longRunningHub.invoke.apply(proxies.longRunningHub, $.merge(["Reset"], $.makeArray(arguments)));
},

set: function () {
/// <summary>Calls the Set method on the server-side LongRunningHub hub.&#10;Returns a jQuery.Deferred() promise.</summary>
return proxies.longRunningHub.invoke.apply(proxies.longRunningHub, $.merge(["Set"], $.makeArray(arguments)));
}
};

proxies.messageLoops = this.createHubProxy('messageLoops');
proxies.messageLoops.client = { };
proxies.messageLoops.server = {
Expand Down

0 comments on commit e0a561b

Please sign in to comment.