@@ -4,10 +4,6 @@ use std::{
4
4
thread:: { sleep, spawn} ,
5
5
time:: Duration ,
6
6
} ;
7
- use tungstenite:: {
8
- accept_hdr, connect,
9
- handshake:: server:: { Request , Response } ,
10
- } ;
11
7
12
8
use appconfiguration:: {
13
9
AppConfigurationClient , AppConfigurationClientHttp , ConfigurationId , ServiceAddress ,
@@ -20,23 +16,18 @@ use std::{fs, path::PathBuf};
20
16
use tungstenite:: WebSocket ;
21
17
22
18
fn handle_config_request_trivial_config ( server : & TcpListener ) {
23
- let json_payload = r#"
24
- {
25
- "environments":
26
- [
27
- {
28
- "name": "Dev",
29
- "environment_id": "dev",
30
- "features": [],
31
- "properties": []
32
- }
19
+ let json_payload = serde_json:: json!( {
20
+ "environments" : [
21
+ {
22
+ "name" : "Dev" ,
23
+ "environment_id" : "dev" ,
24
+ "features" : [ ] ,
25
+ "properties" : [ ]
26
+ }
33
27
] ,
34
- "segments": []
35
- }
36
- "#
37
- . to_string ( ) ;
38
-
39
- handle_config_request ( server, json_payload) ;
28
+ "segments" : [ ]
29
+ } ) ;
30
+ handle_config_request ( server, json_payload. to_string ( ) ) ;
40
31
}
41
32
42
33
fn handle_config_request_enterprise_example ( server : & TcpListener ) {
@@ -67,34 +58,35 @@ fn handle_config_request(server: &TcpListener, json_payload: String) {
67
58
}
68
59
69
60
fn handle_websocket ( server : & TcpListener ) -> WebSocket < TcpStream > {
70
- let ( mut stream, _) = server. accept ( ) . unwrap ( ) ;
61
+ let ( stream, _) = server. accept ( ) . unwrap ( ) ;
71
62
let mut websocket = tungstenite:: accept ( stream) . unwrap ( ) ;
72
- websocket. send ( tungstenite:: Message :: text ( "test message" . to_string ( ) ) ) . unwrap ( ) ;
73
- websocket. send ( tungstenite:: Message :: text ( "test messag" . to_string ( ) ) ) . unwrap ( ) ;
63
+ websocket
64
+ . send ( tungstenite:: Message :: text ( "test message" . to_string ( ) ) )
65
+ . unwrap ( ) ;
66
+ websocket
67
+ . send ( tungstenite:: Message :: text ( "test messag" . to_string ( ) ) )
68
+ . unwrap ( ) ;
74
69
websocket
75
70
}
76
71
77
72
struct ServerHandle {
78
- terminator : std:: sync:: mpsc:: Sender < ( ) > ,
73
+ _terminator : std:: sync:: mpsc:: Sender < ( ) > ,
79
74
config_updated : std:: sync:: mpsc:: Receiver < ( ) > ,
80
- port : u16
75
+ port : u16 ,
81
76
}
82
77
fn server_thread ( ) -> ServerHandle {
83
78
let ( terminator, receiver) = channel ( ) ;
84
79
let ( config_updated_tx, config_updated_rx) = channel ( ) ;
85
80
86
- let server = TcpListener :: bind ( ( "127.0.0.1" , 0 ) )
87
- . expect ( "Failed to bind" ) ;
81
+ let server = TcpListener :: bind ( ( "127.0.0.1" , 0 ) ) . expect ( "Failed to bind" ) ;
88
82
let port = server. local_addr ( ) . unwrap ( ) . port ( ) ;
89
83
spawn ( move || {
90
-
91
84
handle_config_request_enterprise_example ( & server) ;
92
85
93
86
// notify client that config changed
94
87
let _websocket = handle_websocket ( & server) ;
95
88
96
- // client will request changed config
97
- // asynchronously
89
+ // client will request changed config asynchronously
98
90
handle_config_request_trivial_config ( & server) ;
99
91
100
92
// we now allow the test to continue
@@ -103,9 +95,9 @@ fn server_thread() -> ServerHandle {
103
95
let _ = receiver. recv ( ) ;
104
96
} ) ;
105
97
ServerHandle {
106
- terminator,
98
+ _terminator : terminator,
107
99
config_updated : config_updated_rx,
108
- port
100
+ port,
109
101
}
110
102
}
111
103
@@ -141,22 +133,19 @@ fn main() {
141
133
features. sort ( ) ;
142
134
assert_eq ! ( features, vec![ "f1" , "f2" , "f3" , "f4" , "f5" , "f6" ] ) ;
143
135
136
+ // TODO: Once we can subscribe to config updates via client APIs, we don't need to wait for signal, neither the loop below
144
137
server. config_updated . recv ( ) . unwrap ( ) ;
145
138
146
139
let start = std:: time:: Instant :: now ( ) ;
147
-
148
- loop {
140
+ loop {
149
141
// We need the loop for now to wait until client updates the config.
150
- // TODO: Once we can subscribe to config updates via client APIs, we do not need to loop here anymore.
151
142
let features = client. get_feature_ids ( ) . unwrap ( ) ;
152
- if features. is_empty ( )
153
- {
143
+ if features. is_empty ( ) {
154
144
// This is what we expect after the config update :)
155
145
break ;
156
146
}
157
- if start. elapsed ( ) . as_millis ( ) > 1000
158
- {
159
- assert ! ( false , "Did not receive updated configuration in time" )
147
+ if start. elapsed ( ) . as_millis ( ) > 1000 {
148
+ panic ! ( "Did not receive updated configuration in time" )
160
149
}
161
150
sleep ( Duration :: from_millis ( 10 ) ) ;
162
151
}
0 commit comments