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

Commit 156bb53

Browse files
committed
Add BlockingPop example
1 parent c8f4d40 commit 156bb53

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

tests/Console.Tests/BlockingPop.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using ServiceStack;
3+
using ServiceStack.Logging;
4+
using ServiceStack.Redis;
5+
6+
namespace ConsoleTests
7+
{
8+
public class BlockingPop
9+
{
10+
public void Execute()
11+
{
12+
LogManager.LogFactory = new ConsoleLogFactory();
13+
var log = LogManager.LogFactory.GetLogger("redistest");
14+
15+
// ********************
16+
// set REDIS CONFIGS
17+
// ********************
18+
RedisConfig.DefaultConnectTimeout = 1 * 1000;
19+
RedisConfig.DefaultSendTimeout = 1 * 1000;
20+
RedisConfig.DefaultReceiveTimeout = 1 * 1000;
21+
//RedisConfig.DefaultRetryTimeout = 15 * 1000;
22+
RedisConfig.DefaultIdleTimeOutSecs = 240;
23+
RedisConfig.BackOffMultiplier = 10;
24+
RedisConfig.BufferLength = 1450;
25+
RedisConfig.BufferPoolMaxSize = 500000;
26+
RedisConfig.VerifyMasterConnections = true;
27+
RedisConfig.HostLookupTimeoutMs = 1000;
28+
RedisConfig.DeactivatedClientsExpiry = TimeSpan.FromSeconds(15);
29+
RedisConfig.DisableVerboseLogging = false;
30+
31+
var redisManager = new RedisManagerPool("localhost?connectTimeout=1000");
32+
33+
// how many test items to create
34+
var items = 5;
35+
// how long to try popping
36+
var waitForSeconds = 30;
37+
// name of list
38+
var listId = "testlist";
39+
40+
var startedAt = DateTime.Now;
41+
42+
log.Info("--------------------------");
43+
log.Info("push {0} items to a list, then try pop for {1} seconds. repeat.".Fmt(items, waitForSeconds));
44+
log.Info("--------------------------");
45+
46+
using (var redis = redisManager.GetClient())
47+
{
48+
do
49+
{
50+
// add items to list
51+
for (int i = 1; i <= items; i++)
52+
{
53+
redis.PushItemToList(listId, "item {0}".Fmt(i));
54+
}
55+
56+
do
57+
{
58+
var item = redis.BlockingPopItemFromList(listId, null);
59+
60+
// log the popped item. if BRPOP timeout is null and list empty, I do not expect to print anything
61+
log.InfoFormat("{0}", item.IsNullOrEmpty() ? " list empty " : item);
62+
63+
System.Threading.Thread.Sleep(1000);
64+
65+
} while (DateTime.Now - startedAt < TimeSpan.FromSeconds(waitForSeconds));
66+
67+
log.Info("--------------------------");
68+
log.Info("completed first loop");
69+
log.Info("--------------------------");
70+
71+
} while (DateTime.Now - startedAt < TimeSpan.FromSeconds(2 * waitForSeconds));
72+
73+
log.Info("--------------------------");
74+
log.Info("completed outer loop");
75+
}
76+
}
77+
}
78+
}

tests/Console.Tests/Console.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Reference Include="System.Xml" />
6161
</ItemGroup>
6262
<ItemGroup>
63+
<Compile Include="BlockingPop.cs" />
6364
<Compile Include="ForceFailover.cs" />
6465
<Compile Include="NetworkRedisSentinelFailoverTests.cs" />
6566
<Compile Include="GoogleRedisSentinelFailoverTests.cs" />

tests/Console.Tests/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ static void Main(string[] args)
3232

3333
//new GoogleRedisSentinelFailoverTests().Execute();
3434

35-
new ForceFailover().Execute();
35+
//new ForceFailover().Execute();
36+
37+
new BlockingPop().Execute();
3638
}
3739
}
3840
}

0 commit comments

Comments
 (0)