@@ -4,9 +4,53 @@ use redis::Connection;
44use std:: fs;
55use std:: path:: PathBuf ;
66use std:: process:: Command ;
7+ use std:: sync:: atomic:: AtomicU16 ;
78use std:: time:: Duration ;
89
10+ /// Starts a redis instance with the module provided as a module name
11+ /// and a port, returns the connection guards (`ChildGuard`) through
12+ /// which the redis instance can be interacted with.
13+ pub fn start_redis ( module_name : & str , port : u16 ) -> Result < Vec < ChildGuard > , & ' static str > {
14+ Ok ( vec ! [ start_redis_server_with_module( module_name, port)
15+ . map_err( |_| "failed to start redis server" ) ?] )
16+ }
17+
18+ pub struct TestConnection {
19+ _guards : Vec < ChildGuard > ,
20+ connection : Connection ,
21+ }
22+
23+ static TEST_PORT : AtomicU16 = AtomicU16 :: new ( 6479 ) ;
24+
25+ impl TestConnection {
26+ /// Creates a new connection to a Redis server with the module
27+ /// provided as a module name.
28+ pub fn new ( module_name : & str ) -> Self {
29+ let port = TEST_PORT . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: SeqCst ) ;
30+
31+ Self {
32+ _guards : start_redis ( module_name, port) . expect ( "Redis instance started." ) ,
33+ connection : get_redis_connection ( port) . expect ( "Established connection to server." ) ,
34+ }
35+ }
36+ }
37+
38+ impl std:: ops:: Deref for TestConnection {
39+ type Target = Connection ;
40+
41+ fn deref ( & self ) -> & Self :: Target {
42+ & self . connection
43+ }
44+ }
45+
46+ impl std:: ops:: DerefMut for TestConnection {
47+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
48+ & mut self . connection
49+ }
50+ }
51+
952/// Ensure child process is killed both on normal exit and when panicking due to a failed test.
53+ #[ derive( Debug ) ]
1054pub struct ChildGuard {
1155 name : & ' static str ,
1256 child : std:: process:: Child ,
0 commit comments