11using System ;
22using System . Buffers ;
3+ using System . Diagnostics . CodeAnalysis ;
4+ using System . Runtime . CompilerServices ;
35using System . Text ;
46using RESPite . Messages ;
57using Xunit ;
@@ -8,39 +10,84 @@ namespace StackExchange.Redis.Tests;
810
911public class ResultProcessorUnitTests ( ITestOutputHelper log )
1012{
11- public void Log ( string message ) => log ? . WriteLine ( message ) ;
12-
13- private protected static Message DummyMessage < T > ( )
14- => Message . Create ( 0 , default , RedisCommand . UNKNOWN ) ;
15-
1613 [ Theory ]
1714 [ InlineData ( ":1\r \n " , 1 ) ]
1815 [ InlineData ( "+1\r \n " , 1 ) ]
1916 [ InlineData ( "$1\r \n 1\r \n " , 1 ) ]
20- public void Int32 ( string resp , int value )
21- {
22- var result = Execute ( resp , ResultProcessor . Int32 ) ;
23- Assert . Equal ( value , result ) ;
24- }
17+ [ InlineData ( ":-42\r \n " , - 42 ) ]
18+ [ InlineData ( "+-42\r \n " , - 42 ) ]
19+ [ InlineData ( "$3\r \n -42\r \n " , - 42 ) ]
20+ public void Int32 ( string resp , int value ) => Assert . Equal ( value , Execute ( resp , ResultProcessor . Int32 ) ) ;
21+
22+ [ Theory ]
23+ [ InlineData ( "+OK\r \n " ) ]
24+ [ InlineData ( "$4\r \n PONG\r \n " ) ]
25+ public void FailingInt32 ( string resp ) => ExecuteUnexpected ( resp , ResultProcessor . Int32 ) ;
2526
2627 [ Theory ]
2728 [ InlineData ( ":1\r \n " , 1 ) ]
2829 [ InlineData ( "+1\r \n " , 1 ) ]
2930 [ InlineData ( "$1\r \n 1\r \n " , 1 ) ]
30- public void Int64 ( string resp , int value )
31+ [ InlineData ( ":-42\r \n " , - 42 ) ]
32+ [ InlineData ( "+-42\r \n " , - 42 ) ]
33+ [ InlineData ( "$3\r \n -42\r \n " , - 42 ) ]
34+ public void Int64 ( string resp , long value ) => Assert . Equal ( value , Execute ( resp , ResultProcessor . Int64 ) ) ;
35+
36+ [ Theory ]
37+ [ InlineData ( "+OK\r \n " ) ]
38+ [ InlineData ( "$4\r \n PONG\r \n " ) ]
39+ public void FailingInt64 ( string resp ) => ExecuteUnexpected ( resp , ResultProcessor . Int64 ) ;
40+
41+ [ Theory ]
42+ [ InlineData ( "*-1\r \n " , null ) ]
43+ [ InlineData ( "*0\r \n " , "" ) ]
44+ [ InlineData ( "*1\r \n +42\r \n " , "42" ) ]
45+ [ InlineData ( "*2\r \n +42\r \n :78\r \n " , "42,78" ) ]
46+ public void Int64Array ( string resp , string ? value ) => Assert . Equal ( value , Join ( Execute ( resp , ResultProcessor . Int64Array ) ) ) ;
47+
48+ [ return : NotNullIfNotNull ( nameof ( array ) ) ]
49+ protected static string ? Join < T > ( T [ ] ? array , string separator = "," )
3150 {
32- var result = Execute ( resp , ResultProcessor . Int32 ) ;
33- Assert . Equal ( value , result ) ;
51+ if ( array is null ) return null ;
52+ return string . Join ( separator , array ) ;
3453 }
3554
36- private protected static T ? Execute < T > ( string resp , ResultProcessor < T > processor )
55+ public void Log ( string message ) => log ? . WriteLine ( message ) ;
56+
57+ private protected static Message DummyMessage < T > ( )
58+ => Message . Create ( 0 , default , RedisCommand . UNKNOWN ) ;
59+
60+ private protected void ExecuteUnexpected < T > (
61+ string resp ,
62+ ResultProcessor < T > processor ,
63+ ConnectionType connectionType = ConnectionType . Interactive ,
64+ RedisProtocol protocol = RedisProtocol . Resp2 ,
65+ [ CallerMemberName ] string caller = "" )
66+ {
67+ Assert . False ( TryExecute ( resp , processor , out _ , out var ex ) ) ;
68+ if ( ex is not null ) Log ( ex . Message ) ;
69+ Assert . StartsWith ( "Unexpected response to UNKNOWN:" , Assert . IsType < RedisConnectionException > ( ex ) . Message ) ;
70+ }
71+ private protected static T ? Execute < T > (
72+ string resp ,
73+ ResultProcessor < T > processor ,
74+ ConnectionType connectionType = ConnectionType . Interactive ,
75+ RedisProtocol protocol = RedisProtocol . Resp2 ,
76+ [ CallerMemberName ] string caller = "" )
3777 {
3878 Assert . True ( TryExecute < T > ( resp , processor , out var value , out var ex ) ) ;
3979 Assert . Null ( ex ) ;
4080 return value ;
4181 }
4282
43- private protected static bool TryExecute < T > ( string resp , ResultProcessor < T > processor , out T ? value , out Exception ? exception )
83+ private protected static bool TryExecute < T > (
84+ string resp ,
85+ ResultProcessor < T > processor ,
86+ out T ? value ,
87+ out Exception ? exception ,
88+ ConnectionType connectionType = ConnectionType . Interactive ,
89+ RedisProtocol protocol = RedisProtocol . Resp2 ,
90+ [ CallerMemberName ] string caller = "" )
4491 {
4592 byte [ ] ? lease = null ;
4693 try
@@ -51,15 +98,15 @@ private protected static bool TryExecute<T>(string resp, ResultProcessor<T> proc
5198 ? stackalloc byte [ MAX_STACK ]
5299 : ( lease = ArrayPool < byte > . Shared . Rent ( maxLen ) ) ;
53100
54- var msg = DummyMessage < int > ( ) ;
101+ var msg = DummyMessage < T > ( ) ;
55102 var box = SimpleResultBox < T > . Get ( ) ;
56103 msg . SetSource ( processor , box ) ;
57104
58105 var reader = new RespReader ( oversized . Slice ( 0 , Encoding . UTF8 . GetBytes ( resp , oversized ) ) ) ;
59- bool success = processor . SetResult ( null ! , msg , ref reader ) ;
60- exception = null ;
61- value = success ? box . GetResult ( out exception , canRecycle : true ) : default ;
62- return success ;
106+ PhysicalConnection connection = new ( connectionType , protocol , caller ) ;
107+ Assert . True ( processor . SetResult ( connection , msg , ref reader ) ) ;
108+ value = box . GetResult ( out exception , canRecycle : true ) ;
109+ return exception is null ;
63110 }
64111 finally
65112 {
0 commit comments