Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit fa07a61

Browse files
committed
Update ConsoleTest to run long running Pub/Sub Server
1 parent ea72bd5 commit fa07a61

File tree

2 files changed

+97
-55
lines changed

2 files changed

+97
-55
lines changed

tests/Console.Tests/Console.Tests.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="ServiceStack">
36+
<HintPath>..\..\lib\tests\ServiceStack.dll</HintPath>
37+
</Reference>
38+
<Reference Include="ServiceStack.Client">
39+
<HintPath>..\..\lib\tests\ServiceStack.Client.dll</HintPath>
40+
</Reference>
3541
<Reference Include="ServiceStack.Interfaces">
3642
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
3743
</Reference>
44+
<Reference Include="ServiceStack.Server">
45+
<HintPath>..\..\lib\tests\ServiceStack.Server.dll</HintPath>
46+
</Reference>
3847
<Reference Include="ServiceStack.Text">
3948
<HintPath>..\..\lib\ServiceStack.Text.dll</HintPath>
4049
</Reference>

tests/Console.Tests/Program.cs

Lines changed: 88 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,120 @@
11

22
using System;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using System.Timers;
5-
using ServiceStack;
66
using ServiceStack.Redis;
77
using ServiceStack.Text;
8+
using Timer = System.Timers.Timer;
89

910
namespace TestRedisConnection
1011
{
12+
public class Incr
13+
{
14+
public long Id { get; set; }
15+
}
16+
17+
public class IncrResponse
18+
{
19+
public long Result { get; set; }
20+
}
21+
1122
class Program
1223
{
13-
public static BasicRedisClientManager Manager { get; set; }
24+
private const string Channel = "longrunningtest";
25+
private static DateTime StartedAt;
26+
27+
private static long MessagesSent = 0;
28+
private static long HeartbeatsSent = 0;
29+
private static long HeartbeatsReceived = 0;
30+
private static long StartCount = 0;
31+
private static long StopCount = 0;
32+
private static long DisposeCount = 0;
33+
private static long ErrorCount = 0;
34+
private static long FailoverCount = 0;
35+
private static long UnSubscribeCount = 0;
36+
37+
public static RedisManagerPool Manager { get; set; }
38+
public static RedisPubSubServer PubSubServer { get; set; }
39+
1440
static void Main(string[] args)
1541
{
16-
Manager = new BasicRedisClientManager("localhost");
42+
Manager = new RedisManagerPool("10.0.0.9");
43+
StartedAt = DateTime.UtcNow;
1744

18-
var q = new System.Timers.Timer { Interval = 2 };
19-
q.Elapsed += CheckConnection;
45+
var q = new Timer { Interval = 1000 };
46+
q.Elapsed += OnInterval;
2047
q.Enabled = true;
2148

22-
if ("q" == Console.ReadLine())
23-
return;
49+
using (PubSubServer = new RedisPubSubServer(Manager, Channel)
50+
{
51+
OnStart = () =>
52+
{
53+
Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount));
54+
},
55+
OnHeartbeatSent = () =>
56+
{
57+
Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent));
58+
},
59+
OnHeartbeatReceived = () =>
60+
{
61+
Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived));
62+
},
63+
OnMessage = (channel, msg) =>
64+
{
65+
Console.WriteLine("OnMessage: @" + channel + ": " + msg);
66+
},
67+
OnStop = () =>
68+
{
69+
Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount));
70+
},
71+
OnError = ex =>
72+
{
73+
Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex);
74+
},
75+
OnFailover = server =>
76+
{
77+
Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount));
78+
},
79+
OnDispose = () =>
80+
{
81+
Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount));
82+
},
83+
OnUnSubscribe = channel =>
84+
{
85+
Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel);
86+
},
87+
})
88+
{
89+
Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString());
90+
PubSubServer.Start();
91+
92+
"Press Enter to Quit...".Print();
93+
Console.ReadLine();
94+
Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString());
95+
Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s");
96+
}
2497
}
2598

26-
private static void CheckConnection(object sender, ElapsedEventArgs e)
99+
private static void OnInterval(object sender, ElapsedEventArgs e)
27100
{
28-
Task.Factory.StartNew(CheckThisConnection);
101+
Task.Factory.StartNew(PublishMessage);
29102
}
30103

31-
private static void CheckThisConnection()
104+
private static void PublishMessage()
32105
{
33106
try
34107
{
35-
"CheckThisConnection()...".Print();
36-
using (var redisClient = Manager.GetClient())
108+
var message = "MSG: #" + Interlocked.Increment(ref MessagesSent);
109+
Console.WriteLine("PublishMessage(): " + message);
110+
using (var redis = Manager.GetClient())
37111
{
38-
using (var trans = redisClient.CreateTransaction())
39-
{
40-
trans.QueueCommand(
41-
r => r.SetEntryInHash("Test", "Price", "123"));
42-
trans.QueueCommand(
43-
r => r.SetEntryInHash("Test2", "Price", "123"));
44-
trans.QueueCommand(
45-
r => r.SetEntryInHash("Test3", "Price", "123"));
46-
trans.QueueCommand(
47-
r => r.SetEntryInHash("Test4", "Price", "123"));
48-
trans.QueueCommand(
49-
r => r.SetEntryInHash("Test5", "Price", "123"));
50-
trans.QueueCommand(
51-
r => r.SetEntryInHash("Test6", "Price", "123"));
52-
trans.QueueCommand(
53-
r => r.SetEntryInHash("Test7", "Price", "123"));
54-
trans.QueueCommand(
55-
r => r.SetEntryInHash("Test8", "Price", "123"));
56-
trans.QueueCommand(
57-
r => r.SetEntryInHash("Test9", "Price", "123"));
58-
trans.QueueCommand(
59-
r => r.SetEntryInHash("Test10", "Price", "123"));
60-
trans.QueueCommand(
61-
r => r.SetEntryInHash("Test11", "Price", "123"));
62-
trans.QueueCommand(
63-
r => r.SetEntryInHash("Test12", "Price", "123"));
64-
trans.QueueCommand(
65-
r => r.SetEntryInHash("Test13", "Price", "123"));
66-
trans.QueueCommand(
67-
r => r.SetEntryInHash("Test14", "Price", "123"));
68-
trans.QueueCommand(
69-
r => r.SetEntryInHash("Test15", "Price", "123"));
70-
trans.QueueCommand(
71-
r => r.SetEntryInHash("Test16", "Price", "123"));
72-
trans.QueueCommand(
73-
r => r.SetEntryInHash("Test17", "Price", "123"));
74-
trans.QueueCommand(
75-
r => r.SetEntryInHash("Test18", "Price", "123"));
76-
trans.QueueCommand(
77-
r => r.SetEntryInHash("Test19", "Price", "123"));
78-
trans.Commit();
79-
}
112+
redis.PublishMessage(Channel, message);
80113
}
81114
}
82115
catch (Exception ex)
83116
{
84-
"ERROR: {0}".Print(ex.ToString());
117+
Console.WriteLine("ERROR PublishMessage: " + ex);
85118
}
86119
}
87120
}

0 commit comments

Comments
 (0)