1+ using System ;
2+ using System . IO ;
3+ using System . Runtime . InteropServices ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+ using Autofac . Extras . FakeItEasy ;
7+ using Elasticsearch . Net . Connection ;
8+ using Elasticsearch . Net . ConnectionPool ;
9+ using Elasticsearch . Net . Exceptions ;
10+ using Elasticsearch . Net . Tests . Unit . Stubs ;
11+ using FakeItEasy ;
12+ using FluentAssertions ;
13+ using NUnit . Framework ;
14+
15+ namespace Elasticsearch . Net . Tests . Unit . Connection
16+ {
17+ [ TestFixture ]
18+ public class NoRetryOnServerExceptionTests
19+ {
20+ private MemoryStream CreateServerExceptionResponse ( int status , string exceptionType , string exceptionMessage )
21+ {
22+ var format = @"{{ ""status"": {0}, ""error"" : ""{1}[{2}]"" }}" ;
23+ var bytes = Encoding . UTF8 . GetBytes ( string . Format ( format , status , exceptionType , exceptionMessage ) ) ;
24+ var stream = new MemoryStream ( bytes ) ;
25+ return stream ;
26+ }
27+
28+ [ Test ]
29+ [ TestCase ( 505 , "SomeException" , "Some Error Message" ) ]
30+ [ TestCase ( 505 , "" , "" ) ]
31+ public void IfResponseIsKnowError_DoNotRetry_ThrowServerException ( int status , string exceptionType , string exceptionMessage )
32+ {
33+ var response = CreateServerExceptionResponse ( status , exceptionType , exceptionMessage ) ;
34+ using ( var fake = new AutoFake ( callsDoNothing : true ) )
35+ {
36+ var connectionPool = new StaticConnectionPool ( new [ ]
37+ {
38+ new Uri ( "http://localhost:9200" ) ,
39+ new Uri ( "http://localhost:9201" ) ,
40+ } ) ;
41+ var connectionConfiguration = new ConnectionConfiguration ( connectionPool )
42+ . ThrowOnElasticsearchServerExceptions ( )
43+ . ExposeRawResponse ( false ) ;
44+
45+ fake . Provide < IConnectionConfigurationValues > ( connectionConfiguration ) ;
46+ FakeCalls . ProvideDefaultTransport ( fake ) ;
47+
48+ var pingCall = FakeCalls . PingAtConnectionLevel ( fake ) ;
49+ pingCall . Returns ( FakeResponse . Ok ( connectionConfiguration ) ) ;
50+
51+ var getCall = FakeCalls . GetSyncCall ( fake ) ;
52+ getCall . Returns ( FakeResponse . Any ( connectionConfiguration , status , response : response ) ) ;
53+
54+ var client = fake . Resolve < ElasticsearchClient > ( ) ;
55+
56+ var e = Assert . Throws < ElasticsearchServerException > ( ( ) => client . Info ( ) ) ;
57+ AssertServerErrorsOnResponse ( e , status , exceptionType , exceptionMessage ) ;
58+
59+ //make sure a know ElasticsearchServerException does not cause a retry
60+ //In this case we want to fail early
61+
62+ getCall . MustHaveHappened ( Repeated . Exactly . Once ) ;
63+
64+ }
65+ }
66+
67+ [ Test ]
68+ [ TestCase ( 505 , "SomeException" , "Some Error Message" ) ]
69+ [ TestCase ( 505 , "" , "" ) ]
70+ public async void IfResponseIsKnowError_DoNotRetry_ThrowServerException_Async ( int status , string exceptionType , string exceptionMessage )
71+ {
72+ var response = CreateServerExceptionResponse ( status , exceptionType , exceptionMessage ) ;
73+ using ( var fake = new AutoFake ( callsDoNothing : true ) )
74+ {
75+ var connectionPool = new StaticConnectionPool ( new [ ]
76+ {
77+ new Uri ( "http://localhost:9200" ) ,
78+ new Uri ( "http://localhost:9201" ) ,
79+ } ) ;
80+ var connectionConfiguration = new ConnectionConfiguration ( connectionPool )
81+ . ThrowOnElasticsearchServerExceptions ( )
82+ . ExposeRawResponse ( false ) ;
83+
84+ fake . Provide < IConnectionConfigurationValues > ( connectionConfiguration ) ;
85+ FakeCalls . ProvideDefaultTransport ( fake ) ;
86+
87+ var pingCall = FakeCalls . PingAtConnectionLevelAsync ( fake ) ;
88+ pingCall . Returns ( FakeResponse . OkAsync ( connectionConfiguration ) ) ;
89+
90+ var getCall = FakeCalls . GetCall ( fake ) ;
91+ getCall . Returns ( FakeResponse . AnyAsync ( connectionConfiguration , status , response : response ) ) ;
92+
93+ var client = fake . Resolve < ElasticsearchClient > ( ) ;
94+
95+ var e = Assert . Throws < ElasticsearchServerException > ( async ( ) => await client . InfoAsync ( ) ) ;
96+ AssertServerErrorsOnResponse ( e , status , exceptionType , exceptionMessage ) ;
97+
98+ //make sure a know ElasticsearchServerException does not cause a retry
99+ //In this case we want to fail early
100+
101+ getCall . MustHaveHappened ( Repeated . Exactly . Once ) ;
102+
103+ }
104+ }
105+
106+ private static void AssertServerErrorsOnResponse (
107+ ElasticsearchServerException serverException , int status , string exceptionType , string exceptionMessage )
108+ {
109+ serverException . Should ( ) . NotBeNull ( ) ;
110+ serverException . ExceptionType . Should ( ) . Be ( exceptionType ) ;
111+ serverException . Message . Should ( ) . Be ( exceptionMessage ) ;
112+ serverException . Status . Should ( ) . Be ( status ) ;
113+ }
114+ }
115+ }
0 commit comments