mirror of https://github.com/fafhrd91/actix-net
				
				
				
			add run_with_code tests
This commit is contained in:
		
							parent
							
								
									a9eccaa26f
								
							
						
					
					
						commit
						359d59ab39
					
				|  | @ -1,8 +1,7 @@ | ||||||
| # Changes | # Changes | ||||||
| 
 | 
 | ||||||
| ## Unreleased - 2021-xx-xx | ## Unreleased - 2021-xx-xx | ||||||
| 
 | * Add `System::run_with_code` to allow retrieving the exit code on stop. [#411] | ||||||
| * Add `System::run_until_stop` to allow retrieving the exit code on stop. [#411] |  | ||||||
| 
 | 
 | ||||||
| [#411]: https://github.com/actix/actix-net/pull/411 | [#411]: https://github.com/actix/actix-net/pull/411 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -175,8 +175,8 @@ impl System { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[cfg(not(feature = "io-uring"))] |  | ||||||
| /// Runner that keeps a [System]'s event loop alive until stop message is received.
 | /// Runner that keeps a [System]'s event loop alive until stop message is received.
 | ||||||
|  | #[cfg(not(feature = "io-uring"))] | ||||||
| #[must_use = "A SystemRunner does nothing unless `run` is called."] | #[must_use = "A SystemRunner does nothing unless `run` is called."] | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct SystemRunner { | pub struct SystemRunner { | ||||||
|  | @ -190,9 +190,9 @@ pub struct SystemRunner { | ||||||
| impl SystemRunner { | impl SystemRunner { | ||||||
|     /// Starts event loop and will return once [System] is [stopped](System::stop).
 |     /// Starts event loop and will return once [System] is [stopped](System::stop).
 | ||||||
|     pub fn run(self) -> io::Result<()> { |     pub fn run(self) -> io::Result<()> { | ||||||
|         // run loop
 |         let exit_code = self.run_with_code()?; | ||||||
|         let code = self.run_until_stop()?; | 
 | ||||||
|         match code { |         match exit_code { | ||||||
|             0 => Ok(()), |             0 => Ok(()), | ||||||
|             nonzero => Err(io::Error::new( |             nonzero => Err(io::Error::new( | ||||||
|                 io::ErrorKind::Other, |                 io::ErrorKind::Other, | ||||||
|  | @ -202,12 +202,12 @@ impl SystemRunner { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Runs the event loop until [stopped](System::stop_with_code), returning the exit code.
 |     /// Runs the event loop until [stopped](System::stop_with_code), returning the exit code.
 | ||||||
|     pub fn run_until_stop(self) -> io::Result<i32> { |     pub fn run_with_code(self) -> io::Result<i32> { | ||||||
|         let SystemRunner { rt, stop_rx, .. } = self; |         let SystemRunner { rt, stop_rx, .. } = self; | ||||||
| 
 | 
 | ||||||
|         // run loop
 |         // run loop
 | ||||||
|         rt.block_on(stop_rx) |         rt.block_on(stop_rx) | ||||||
|             .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) |             .map_err(|err| io::Error::new(io::ErrorKind::Other, err)) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Runs the provided future, blocking the current thread until the future completes.
 |     /// Runs the provided future, blocking the current thread until the future completes.
 | ||||||
|  | @ -217,8 +217,8 @@ impl SystemRunner { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #[cfg(feature = "io-uring")] |  | ||||||
| /// Runner that keeps a [System]'s event loop alive until stop message is received.
 | /// Runner that keeps a [System]'s event loop alive until stop message is received.
 | ||||||
|  | #[cfg(feature = "io-uring")] | ||||||
| #[must_use = "A SystemRunner does nothing unless `run` is called."] | #[must_use = "A SystemRunner does nothing unless `run` is called."] | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct SystemRunner; | pub struct SystemRunner; | ||||||
|  | @ -227,7 +227,14 @@ pub struct SystemRunner; | ||||||
| impl SystemRunner { | impl SystemRunner { | ||||||
|     /// Starts event loop and will return once [System] is [stopped](System::stop).
 |     /// Starts event loop and will return once [System] is [stopped](System::stop).
 | ||||||
|     pub fn run(self) -> io::Result<()> { |     pub fn run(self) -> io::Result<()> { | ||||||
|         unimplemented!("SystemRunner::run is not implemented yet") |         unimplemented!("SystemRunner::run is not implemented for io-uring feature yet"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /// Runs the event loop until [stopped](System::stop_with_code), returning the exit code.
 | ||||||
|  |     pub fn run_with_code(self) -> io::Result<i32> { | ||||||
|  |         unimplemented!( | ||||||
|  |             "SystemRunner::run_with_code is not implemented for io-uring feature yet" | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /// Runs the provided future, blocking the current thread until the future completes.
 |     /// Runs the provided future, blocking the current thread until the future completes.
 | ||||||
|  |  | ||||||
|  | @ -24,6 +24,14 @@ fn await_for_timer() { | ||||||
|     ); |     ); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | #[test] | ||||||
|  | fn run_with_code() { | ||||||
|  |     let sys = System::new(); | ||||||
|  |     System::current().stop_with_code(42); | ||||||
|  |     let exit_code = sys.run_with_code().expect("system stop should not error"); | ||||||
|  |     assert_eq!(exit_code, 42); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[test] | #[test] | ||||||
| fn join_another_arbiter() { | fn join_another_arbiter() { | ||||||
|     let time = Duration::from_secs(1); |     let time = Duration::from_secs(1); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue