1
1
use std:: borrow:: Cow ;
2
+ use std:: fs:: File ;
3
+ use std:: io:: Write ;
4
+ use std:: path:: PathBuf ;
2
5
3
6
use crate :: {
4
7
background:: { BackgroundTask , Command } ,
@@ -12,6 +15,7 @@ use clap::Parser;
12
15
13
16
use influxdb:: WriteQuery ;
14
17
18
+ use crate :: events:: LogLine ;
15
19
use tokio:: sync:: {
16
20
mpsc:: { self , channel, Sender } ,
17
21
oneshot,
@@ -31,21 +35,39 @@ pub struct Client {
31
35
global_seq : u64 ,
32
36
/// A group-scoped sequence number assigned to this test instance by the sync service.
33
37
group_seq : u64 ,
38
+ /// A path to `run.out`.
39
+ run_out : Option < PathBuf > ,
34
40
}
35
41
36
42
impl Client {
37
43
pub async fn new_and_init ( ) -> Result < Self , Box < dyn std:: error:: Error > > {
38
- let run_parameters = RunParameters :: try_parse ( ) ?;
44
+ let run_parameters: RunParameters = RunParameters :: try_parse ( ) ?;
39
45
40
46
let ( cmd_tx, cmd_rx) = channel ( 1 ) ;
41
47
42
48
let background = BackgroundTask :: new ( cmd_rx, run_parameters. clone ( ) ) . await ?;
49
+
50
+ let run_out = run_parameters
51
+ . test_outputs_path
52
+ . to_str ( )
53
+ . map ( |path_str| {
54
+ if path_str. is_empty ( ) {
55
+ None
56
+ } else {
57
+ let mut path = PathBuf :: from ( path_str) ;
58
+ path. push ( "run.out" ) ;
59
+ Some ( path)
60
+ }
61
+ } )
62
+ . unwrap_or ( None ) ;
63
+
43
64
// `global_seq` and `group_seq` are initialized by 0 at this point since no way to signal to the sync service.
44
65
let mut client = Self {
45
66
cmd_tx,
46
67
run_parameters,
47
68
global_seq : 0 ,
48
69
group_seq : 0 ,
70
+ run_out,
49
71
} ;
50
72
51
73
tokio:: spawn ( background. run ( ) ) ;
@@ -64,7 +86,7 @@ impl Client {
64
86
// Note that the sdk-go only signals, but not waits.
65
87
. signal_and_wait (
66
88
format ! ( "initialized_group_{}" , client. run_parameters. test_group_id) ,
67
- client. run_parameters . test_group_instance_count as u64 ,
89
+ client. run_parameters . test_group_instance_count ,
68
90
)
69
91
. await ?;
70
92
@@ -252,6 +274,8 @@ impl Client {
252
274
let json_event = serde_json:: to_string ( & event) . expect ( "Event Serialization" ) ;
253
275
254
276
println ! ( "{}" , json_event) ;
277
+
278
+ self . write ( & event. event ) ;
255
279
}
256
280
257
281
pub async fn record_success ( self ) -> Result < ( ) , Error > {
@@ -263,6 +287,10 @@ impl Client {
263
287
264
288
receiver. await . expect ( BACKGROUND_SENDER ) ?;
265
289
290
+ self . write ( & EventType :: Success {
291
+ group : self . run_parameters . test_group_id . clone ( ) ,
292
+ } ) ;
293
+
266
294
Ok ( ( ) )
267
295
}
268
296
@@ -271,12 +299,20 @@ impl Client {
271
299
272
300
let ( sender, receiver) = oneshot:: channel ( ) ;
273
301
274
- let cmd = Command :: SignalFailure { error, sender } ;
302
+ let cmd = Command :: SignalFailure {
303
+ error : error. clone ( ) ,
304
+ sender,
305
+ } ;
275
306
276
307
self . cmd_tx . send ( cmd) . await . expect ( BACKGROUND_RECEIVER ) ;
277
308
278
309
receiver. await . expect ( BACKGROUND_SENDER ) ?;
279
310
311
+ self . write ( & EventType :: Failure {
312
+ group : self . run_parameters . test_group_id . clone ( ) ,
313
+ error,
314
+ } ) ;
315
+
280
316
Ok ( ( ) )
281
317
}
282
318
@@ -291,15 +327,21 @@ impl Client {
291
327
let ( sender, receiver) = oneshot:: channel ( ) ;
292
328
293
329
let cmd = Command :: SignalCrash {
294
- error,
295
- stacktrace,
330
+ error : error . clone ( ) ,
331
+ stacktrace : stacktrace . clone ( ) ,
296
332
sender,
297
333
} ;
298
334
299
335
self . cmd_tx . send ( cmd) . await . expect ( BACKGROUND_RECEIVER ) ;
300
336
301
337
receiver. await . expect ( BACKGROUND_SENDER ) ?;
302
338
339
+ self . write ( & EventType :: Crash {
340
+ groups : self . run_parameters . test_group_id . clone ( ) ,
341
+ error,
342
+ stacktrace,
343
+ } ) ;
344
+
303
345
Ok ( ( ) )
304
346
}
305
347
@@ -332,4 +374,25 @@ impl Client {
332
374
pub fn group_seq ( & self ) -> u64 {
333
375
self . group_seq
334
376
}
377
+
378
+ /// Writes an event to `run.out`.
379
+ fn write ( & self , event_type : & EventType ) {
380
+ if let Some ( path) = self . run_out . as_ref ( ) {
381
+ let mut file = match File :: options ( ) . create ( true ) . append ( true ) . open ( path) {
382
+ Ok ( file) => file,
383
+ Err ( e) => {
384
+ eprintln ! ( "Failed to open `run.out`: {}" , e) ;
385
+ return ;
386
+ }
387
+ } ;
388
+
389
+ if let Err ( e) = writeln ! (
390
+ file,
391
+ "{}" ,
392
+ serde_json:: to_string( & LogLine :: new( event_type) ) . expect( "Event Serialization" )
393
+ ) {
394
+ eprintln ! ( "Failed to write a log to `run.out`: {}" , e) ;
395
+ }
396
+ }
397
+ }
335
398
}
0 commit comments