1
- use std:: future:: pending;
1
+ use std:: { future:: pending, sync :: Arc , time :: Duration } ;
2
2
3
- use kvsd :: client :: Api ;
3
+ use futures_util :: TryFutureExt ;
4
4
use ratatui:: backend:: TestBackend ;
5
- use synd_api:: { client:: github:: GithubClient , serve:: auth:: Authenticator } ;
5
+ use synd_api:: {
6
+ client:: github:: GithubClient ,
7
+ dependency:: Dependency ,
8
+ repository:: kvsd:: KvsdClient ,
9
+ serve:: auth:: Authenticator ,
10
+ usecase:: { authorize:: Authorizer , MakeUsecase , Runtime } ,
11
+ } ;
12
+ use synd_feed:: feed:: { cache:: CacheLayer , parser:: FeedService } ;
6
13
use synd_term:: terminal:: Terminal ;
7
14
use tokio:: net:: { TcpListener , TcpStream } ;
8
15
@@ -12,25 +19,33 @@ pub fn new_test_terminal() -> Terminal {
12
19
Terminal :: with ( terminal)
13
20
}
14
21
15
- // Dependency
16
- // * serve
17
- // * tcp listener
18
- // * dependency
19
- // * authenticator
20
- // * github client
21
- // * github endpoint
22
- // * usecase runtime
23
- // * make usecase
24
- // * datastore
25
- // * fetch cached feed
26
- // * authorizer
27
- #[ allow( unused) ]
28
- pub fn serve_api ( mock_port : u16 ) -> anyhow:: Result < ( ) > {
22
+ pub async fn serve_api ( mock_port : u16 , api_port : u16 ) -> anyhow:: Result < ( ) > {
29
23
let github_endpoint: & ' static str =
30
24
format ! ( "http://localhost:{mock_port}/github/graphql" ) . leak ( ) ;
31
25
let github_client = GithubClient :: new ( ) ?. with_endpoint ( github_endpoint) ;
32
26
let authenticator = Authenticator :: new ( ) ?. with_client ( github_client) ;
33
27
28
+ let kvsd_client = run_kvsd ( ) . await . map ( KvsdClient :: new) ?;
29
+ let feed_service = FeedService :: new ( "synd_term_test" , 1024 * 1024 ) ;
30
+ let feed_service = CacheLayer :: new ( feed_service) ;
31
+ let make_usecase = MakeUsecase {
32
+ subscription_repo : Arc :: new ( kvsd_client) ,
33
+ fetch_feed : Arc :: new ( feed_service) ,
34
+ } ;
35
+ let authorizer = Authorizer :: new ( ) ;
36
+ let runtime = Runtime :: new ( make_usecase, authorizer) ;
37
+ let dep = Dependency {
38
+ authenticator,
39
+ runtime,
40
+ } ;
41
+ let listener = TcpListener :: bind ( ( "localhost" , api_port) ) . await ?;
42
+
43
+ tokio:: spawn ( synd_api:: serve:: serve (
44
+ listener,
45
+ dep,
46
+ std:: future:: pending :: < ( ) > ( ) ,
47
+ ) ) ;
48
+
34
49
Ok ( ( ) )
35
50
}
36
51
@@ -57,17 +72,19 @@ pub async fn run_kvsd() -> anyhow::Result<kvsd::client::tcp::Client<TcpStream>>
57
72
58
73
let _server_handler = tokio:: spawn ( initializer. run_kvsd ( pending :: < ( ) > ( ) ) ) ;
59
74
60
- // TODO: retry
61
- let mut client = kvsd:: client:: tcp:: UnauthenticatedClient :: insecure_from_addr ( addr. 0 , addr. 1 )
62
- . await
63
- . unwrap ( )
64
- . authenticate ( "test" , "test" )
65
- . await
66
- . unwrap ( ) ;
67
-
68
- // Ping
69
- let ping_duration = client. ping ( ) . await . unwrap ( ) ;
70
- assert ! ( ping_duration. num_nanoseconds( ) . unwrap( ) > 0 ) ;
75
+ let handshake = async {
76
+ loop {
77
+ match kvsd:: client:: tcp:: UnauthenticatedClient :: insecure_from_addr ( addr. 0 , addr. 1 )
78
+ . and_then ( |client| client. authenticate ( "test" , "test" ) )
79
+ . await
80
+ {
81
+ Ok ( client) => break client,
82
+ Err ( _) => tokio:: time:: sleep ( Duration :: from_millis ( 500 ) ) . await ,
83
+ }
84
+ }
85
+ } ;
86
+
87
+ let client = tokio:: time:: timeout ( Duration :: from_secs ( 5 ) , handshake) . await ?;
71
88
72
89
Ok ( client)
73
90
}
0 commit comments