99namespace WebApiClient . Test
1010{
1111 public class HttpApiFactoryTest
12- {
13-
12+ {
1413 [ Fact ]
1514 public void CreateHttpApiTest ( )
1615 {
17- var factory = new HttpApiFactory < IMyApi > ( null , null ) { Lifetime = TimeSpan . FromMilliseconds ( 100 ) } ;
18-
16+ var factory = new HttpApiFactory < IMyApi > ( )
17+ . SetLifetime ( TimeSpan . FromMilliseconds ( 100d ) ) ;
18+
1919 var api1 = factory . CreateHttpApi ( ) ;
2020 var api2 = factory . CreateHttpApi ( ) ;
2121 Assert . True ( IsHttpApiConfigEquals ( api1 , api2 ) ) ;
@@ -25,18 +25,44 @@ public void CreateHttpApiTest()
2525
2626 var api3 = factory . CreateHttpApi ( ) ;
2727 Assert . False ( IsHttpApiConfigEquals ( api1 , api3 ) ) ;
28+
29+ api3 . Dispose ( ) ;
30+ Assert . True ( GetHttpApiConfig ( api3 ) . IsDisposed == false ) ;
2831 }
2932
30- private bool IsHttpApiConfigEquals ( IHttpApiClient x , IHttpApiClient y )
33+ [ Fact ]
34+ public void AddCreateTest ( )
3135 {
32- var xInterceptor = x . ApiInterceptor as ApiInterceptor ;
33- var yInterceptor = y . ApiInterceptor as ApiInterceptor ;
36+ HttpApiFactory . Add < IMyApi > ( )
37+ . SetLifetime ( TimeSpan . FromMilliseconds ( 100d ) ) ;
38+
39+ var api1 = HttpApiFactory . Create < IMyApi > ( ) ;
40+ var api2 = HttpApiFactory . Create < IMyApi > ( ) ;
41+ Assert . True ( IsHttpApiConfigEquals ( api1 , api2 ) ) ;
42+ Assert . False ( api1 == api2 ) ;
43+
44+ Thread . Sleep ( TimeSpan . FromMilliseconds ( 150 ) ) ;
3445
35- return xInterceptor . HttpApiConfig == yInterceptor . HttpApiConfig &&
36- xInterceptor . HttpApiConfig . HttpClient == yInterceptor . HttpApiConfig . HttpClient ;
46+ var api3 = HttpApiFactory . Create < IMyApi > ( ) ;
47+ Assert . False ( IsHttpApiConfigEquals ( api1 , api3 ) ) ;
48+
49+ api3 . Dispose ( ) ;
50+ Assert . True ( GetHttpApiConfig ( api3 ) . IsDisposed == false ) ;
51+ }
52+
53+ private bool IsHttpApiConfigEquals ( IHttpApi x , IHttpApi y )
54+ {
55+ return GetHttpApiConfig ( x ) == GetHttpApiConfig ( y ) ;
56+ }
57+
58+ private HttpApiConfig GetHttpApiConfig ( IHttpApi httpApi )
59+ {
60+ var httpApiClient = httpApi as HttpApiClient ;
61+ var interceptor = httpApiClient . ApiInterceptor as ApiInterceptor ;
62+ return interceptor . HttpApiConfig ;
3763 }
3864
39- public interface IMyApi : IHttpApiClient
65+ public interface IMyApi : IHttpApi
4066 {
4167 }
4268 }
0 commit comments