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
+ }
0 commit comments