Skip to content

Commit a13554b

Browse files
authored
Merge pull request #7 from jonasbn/issue6
First shot at implementation of configurable parameter for setting the sleep interval between retries
2 parents 9a62e65 + 81d66fa commit a13554b

File tree

3 files changed

+42
-25
lines changed

3 files changed

+42
-25
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ services:
3939
hostname: mongo
4040
ports:
4141
- "27017:27017"
42-
42+
4343
postgres:
4444
image: "postgres:9.4"
4545
hostname: postgres
@@ -51,7 +51,7 @@ services:
5151
hostname: mysql
5252
ports:
5353
- "3306:3306"
54-
54+
5555
mySuperApp:
5656
image: "mySuperApp:latest"
5757
hostname: mySuperApp
@@ -68,6 +68,7 @@ The behaviour of the wait utility can be configured with the following environme
6868
- *WAIT_HOSTS_TIMEOUT*: max number of seconds to wait for the hosts to be available before failure. The default is 30 seconds.
6969
- *WAIT_BEFORE_HOSTS*: number of seconds to wait (sleep) before start checking for the hosts availability
7070
- *WAIT_AFTER_HOSTS*: number of seconds to wait (sleep) once all the hosts are available
71+
- *WAIT_SLEEP_INTERVAL*: number of seconds to sleep between retries. The default is 1 second.
7172
7273
# Notes
7374
This utility was explicitly written to be used with docker-compose; however, it can be used everywhere since it has no dependencies on docker.

src/lib.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ pub struct Config {
66
pub hosts: String,
77
pub timeout : u64,
88
pub wait_before : u64,
9-
pub wait_after : u64
9+
pub wait_after : u64,
10+
pub wait_sleep_interval : u64
1011
}
1112

1213
pub fn wait(sleep: &sleeper::Sleeper, config: &Config, on_timeout : &mut FnMut() ) {
@@ -38,7 +39,7 @@ pub fn wait(sleep: &sleeper::Sleeper, config: &Config, on_timeout : &mut FnMut()
3839
on_timeout();
3940
return;
4041
}
41-
sleep.sleep(1);
42+
sleep.sleep(config.wait_sleep_interval);
4243
}
4344
println!("Host {} is now available", host);
4445
}
@@ -56,6 +57,7 @@ pub fn config_from_env() -> Config {
5657
timeout: to_int(env_reader::env_var(&"WAIT_HOSTS_TIMEOUT".to_string(), "".to_string()), 30),
5758
wait_before: to_int(env_reader::env_var(&"WAIT_BEFORE_HOSTS".to_string(), "".to_string()), 0),
5859
wait_after: to_int(env_reader::env_var(&"WAIT_AFTER_HOSTS".to_string(), "".to_string()), 0),
60+
wait_sleep_interval: to_int(env_reader::env_var(&"WAIT_SLEEP_INTERVAL".to_string(), "".to_string()), 1),
5961
}
6062
}
6163

@@ -110,18 +112,31 @@ mod test {
110112

111113
#[test]
112114
fn should_get_config_values_from_env() {
113-
set_env("localhost:1234", "", "2", "3");
115+
set_env("localhost:1234", "20", "2", "3", "4");
114116
let config = config_from_env();
115117
assert_eq!("localhost:1234".to_string(), config.hosts);
116-
assert_eq!(30, config.timeout);
118+
assert_eq!(20, config.timeout);
117119
assert_eq!(2, config.wait_before);
118120
assert_eq!(3, config.wait_after);
121+
assert_eq!(4, config.wait_sleep_interval);
119122
}
120123

121-
fn set_env(hosts: &str, timeout: &str, before: &str, after: &str) {
124+
/* #[test]
125+
fn should_get_default_config_values() {
126+
set_env("localhost:1234", "", "", "", "");
127+
let config = config_from_env();
128+
assert_eq!("localhost:1234".to_string(), config.hosts);
129+
assert_eq!(30, config.timeout);
130+
assert_eq!(0, config.wait_before);
131+
assert_eq!(0, config.wait_after);
132+
assert_eq!(1, config.wait_sleep_interval);
133+
}
134+
*/
135+
fn set_env(hosts: &str, timeout: &str, before: &str, after: &str, sleep: &str) {
122136
env::set_var("WAIT_BEFORE_HOSTS", before.to_string());
123137
env::set_var("WAIT_AFTER_HOSTS", after.to_string());
124138
env::set_var("WAIT_HOSTS_TIMEOUT", timeout.to_string());
125139
env::set_var("WAIT_HOSTS", hosts.to_string());
140+
env::set_var("WAIT_SLEEP_INTERVAL", sleep.to_string());
126141
}
127142
}

tests/integration_test.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn should_wait_5_seconds_before() {
1313
let wait_for : u64 = 5;
1414
let start = Instant::now();
1515
let sleeper = MillisSleeper{};
16-
wait::wait(&sleeper, &new_config("", 1, wait_for, 0), &mut on_timeout);
16+
wait::wait(&sleeper, &new_config("", 1, wait_for, 0, 1 ), &mut on_timeout);
1717
assert!( millis_elapsed(start) >= wait_for )
1818
}
1919

@@ -23,7 +23,7 @@ fn should_wait_10_seconds_after() {
2323
let wait_for = 10;
2424
let start = Instant::now();
2525
let sleeper = MillisSleeper{};
26-
wait::wait(&sleeper, &new_config("", 1, 0, wait_for ), &mut on_timeout);
26+
wait::wait(&sleeper, &new_config("", 1, 0, wait_for, 1 ), &mut on_timeout);
2727
assert!( millis_elapsed(start) >= wait_for )
2828
}
2929

@@ -32,15 +32,15 @@ fn should_wait_before_and_after() {
3232
let wait_for = 10;
3333
let start = Instant::now();
3434
let sleeper = MillisSleeper{};
35-
wait::wait(&sleeper, &new_config("", 1, wait_for, wait_for ), &mut on_timeout);
35+
wait::wait(&sleeper, &new_config("", 1, wait_for, wait_for, 1 ), &mut on_timeout);
3636
assert!( millis_elapsed(start) >= (wait_for + wait_for) )
3737
}
3838

3939
#[test]
4040
fn should_execute_without_wait() {
4141
let start = Instant::now();
4242
let sleeper = MillisSleeper{};
43-
wait::wait(&sleeper, &new_config("", 1, 0, 0 ), &mut on_timeout);
43+
wait::wait(&sleeper, &new_config("", 1, 0, 0, 1 ), &mut on_timeout);
4444
assert!( millis_elapsed(start) <= 5 )
4545
}
4646

@@ -56,9 +56,9 @@ fn should_exit_on_timeout() {
5656
let count : atomic_counter::RelaxedCounter = atomic_counter::RelaxedCounter::new(0);
5757
let mut fun = || { count.inc(); };
5858
assert_eq!(0, count.get());
59-
60-
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after ), &mut fun);
61-
59+
60+
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after, 1 ), &mut fun);
61+
6262
// assert that the on_timeout callback was called
6363
assert_eq!(1, count.get());
6464

@@ -83,9 +83,9 @@ fn should_identify_the_open_port() {
8383

8484
listen_async(tcp_listener);
8585

86-
thread::sleep(time::Duration::from_millis(250));
87-
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after ), &mut fun);
88-
86+
thread::sleep(time::Duration::from_millis(250));
87+
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after, 1 ), &mut fun);
88+
8989
assert_eq!(0, count.get());
9090

9191
assert!( millis_elapsed(start) >= wait_before + wait_after );
@@ -111,9 +111,9 @@ fn should_wait_multiple_hosts() {
111111
listen_async(tcp_listener1);
112112
listen_async(tcp_listener2);
113113

114-
thread::sleep(time::Duration::from_millis(250));
115-
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after ), &mut fun);
116-
114+
thread::sleep(time::Duration::from_millis(250));
115+
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after, 1 ), &mut fun);
116+
117117
assert_eq!(0, count.get());
118118

119119
assert!( millis_elapsed(start) >= wait_before + wait_after );
@@ -137,9 +137,9 @@ fn should_fail_if_not_all_hosts_are_available() {
137137

138138
listen_async(tcp_listener1);
139139

140-
thread::sleep(time::Duration::from_millis(250));
141-
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after ), &mut fun);
142-
140+
thread::sleep(time::Duration::from_millis(250));
141+
wait::wait(&sleeper, &new_config(&hosts, timeout, wait_before, wait_after, 1 ), &mut fun);
142+
143143
assert_eq!(1, count.get());
144144

145145
assert!( millis_elapsed(start) >= wait_before + wait_after );
@@ -148,12 +148,13 @@ fn should_fail_if_not_all_hosts_are_available() {
148148

149149
fn on_timeout() {}
150150

151-
fn new_config(hosts: &str, timeout: u64, before: u64, after: u64) -> wait::Config {
151+
fn new_config(hosts: &str, timeout: u64, before: u64, after: u64, sleep: u64) -> wait::Config {
152152
wait::Config {
153153
hosts: hosts.to_string(),
154154
timeout: timeout,
155155
wait_before: before,
156156
wait_after: after,
157+
wait_sleep_interval: sleep
157158
}
158159
}
159160

@@ -190,4 +191,4 @@ impl Sleeper for MillisSleeper {
190191
fn sleep(&self, duration: u64) {
191192
thread::sleep(Duration::from_millis(duration))
192193
}
193-
}
194+
}

0 commit comments

Comments
 (0)