11using System ;
2+ using System . Collections . Concurrent ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
25using System . Text ;
6+ using System . Threading . Tasks ;
37using Xunit ;
48using Xunit . Abstractions ;
59
@@ -14,7 +18,7 @@ public DBTester(ITestOutputHelper output)
1418 this . Console = output ;
1519 //DB.Host.AddWriteHost("192.168.2.19", 6378, true);
1620 DB . Host . AddWriteHost ( "192.168.2.19" , 6379 ) ;
17-
21+
1822 }
1923
2024 private RedisDB DB = new RedisDB ( 0 ) ;
@@ -37,9 +41,9 @@ private void Write(object result)
3741 [ Fact ]
3842 public async void DBDisposed ( )
3943 {
40- using ( RedisDB db = new RedisDB ( ) )
44+ using ( RedisDB db = new RedisDB ( ) )
4145 {
42- db . Host . AddWriteHost ( "127.0.0.1 " ) ;
46+ db . Host . AddWriteHost ( "192.168.2.19 " ) ;
4347 await db . Ping ( ) ;
4448 }
4549 }
@@ -73,7 +77,7 @@ public async void GetSetBytes()
7377 var data = Encoding . UTF8 . GetBytes ( "henryfan@msn.com" ) ;
7478 await DB . Set ( "bytes" , new ArraySegment < byte > ( data ) ) ;
7579 var result = await DB . Get < ArraySegment < byte > > ( "bytes" ) ;
76- Assert . Equal < string > ( Encoding . UTF8 . GetString ( result . Array , 0 , result . Count ) , "henryfan@msn.com" ) ;
80+ Assert . Equal < string > ( Encoding . UTF8 . GetString ( result . Array , 0 , result . Count ) , "henryfan@msn.com" ) ;
7781 }
7882
7983 [ Fact ]
@@ -85,6 +89,22 @@ public async void Set()
8589 var value = await DB . Get < string > ( "test" ) ;
8690 Write ( value ) ;
8791 Assert . Equal < string > ( value , "henryfan1" ) ;
92+
93+ var emptyResult = await DB . Set ( "test" , "" ) ;
94+ Write ( emptyResult ) ;
95+
96+ var emptyValue = await DB . Get < string > ( "test" ) ;
97+ Write ( emptyValue ) ;
98+ Assert . Equal < string > ( emptyValue , "" ) ;
99+
100+
101+ var nullResult = await DB . Set ( "test" , null ) ;
102+ Write ( nullResult ) ;
103+
104+ var nullValue = await DB . Get < string > ( "test" ) ;
105+ Write ( nullValue ) ;
106+ Assert . Equal < string > ( nullValue , "" ) ;
107+
88108 }
89109
90110 [ Fact ]
@@ -161,7 +181,7 @@ public async void MSet()
161181 public async void Keys ( )
162182 {
163183 await DB . Flushall ( ) ;
164- var mset = await DB . MSet ( ( "one" , 1 ) , ( "tow" , 2 ) , ( "three" , 2 ) , ( "four" , 4 ) ) ;
184+ var mset = await DB . MSet ( ( "one" , 1 ) , ( "tow" , 2 ) , ( "three" , 2 ) , ( "four" , 4 ) ) ;
165185 Write ( mset ) ;
166186 var keys = await DB . Keys ( "*o*" ) ;
167187 Write ( keys ) ;
@@ -482,9 +502,9 @@ public async void MGet5()
482502 public async void MSetNX ( )
483503 {
484504 await DB . Flushall ( ) ;
485- var msetnx = await DB . MSetNX ( ( "key1" , "hello" ) , ( "key2" , "there" ) ) ;
505+ var msetnx = await DB . MSetNX ( ( "key1" , "hello" ) , ( "key2" , "there" ) ) ;
486506 Write ( msetnx ) ;
487- msetnx = await DB . MSetNX ( ( "key3" , "world" ) , ( "key2" , "there" ) ) ;
507+ msetnx = await DB . MSetNX ( ( "key3" , "world" ) , ( "key2" , "there" ) ) ;
488508 Write ( msetnx ) ;
489509 var mget = await DB . MGet < string , string , string > ( "key1" , "key2" , "key3" ) ;
490510 Write ( mget . Item1 ) ;
@@ -569,5 +589,57 @@ public async void Publish()
569589 {
570590 Write ( await DB . Publish ( "channel" , DateTime . Now ) ) ;
571591 }
592+
593+ private RedisDB CreateDB ( int db , string host , int port )
594+ {
595+ var redisDb = new RedisDB ( db ) ;
596+ redisDb . Host . AddWriteHost ( host , port ) ;
597+ return redisDb ;
598+ }
599+
600+ private IEnumerable < RedisDB > CreateMultiDB ( string host , int port )
601+ {
602+ for ( int db = 0 ; db < 12 ; db ++ )
603+ {
604+ yield return CreateDB ( db , host , port ) ;
605+ }
606+ }
607+
608+
609+ [ Fact ]
610+ public async void MultiDBInit ( )
611+ {
612+ var redisDbs = CreateMultiDB ( "192.168.2.19" , 6379 ) . ToList ( ) ;
613+
614+ var db = redisDbs [ 0 ] ; //any one
615+
616+ ConcurrentBag < long ? > results = new ConcurrentBag < long ? > ( ) ;
617+ ConcurrentBag < Exception > exs = new ConcurrentBag < Exception > ( ) ;
618+ var parallelResult = Parallel . For ( 1 , 100 , async i =>
619+ {
620+ long ? result = null ;
621+ try
622+ {
623+ result = await db . Exists ( "anykey" ) ;
624+ }
625+ //throw
626+ //connect xxxxxx@6379 timeout! task status:WaitingToRun
627+ catch ( Exception ex )
628+ {
629+ exs . Add ( ex ) ;
630+ }
631+ finally
632+ {
633+ results . Add ( result ) ;
634+ }
635+ } ) ;
636+
637+ while ( results . Count != 99 )
638+ {
639+ await Task . Delay ( 1000 ) ;
640+ }
641+ Assert . True ( exs . Count == 0 , exs . ToArray ( ) [ 0 ] . Message ) ;
642+
643+ }
572644 }
573645}
0 commit comments