Skip to content

Commit

Permalink
Test: add unit tests for notification service
Browse files Browse the repository at this point in the history
  • Loading branch information
zejiran committed Oct 12, 2023
1 parent 1644109 commit b300cc2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/notification_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,55 @@ impl NotificationService {
Ok(())
}
}

// Unit tests
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_send_notification_within_rate_limit() {
let mut service = NotificationService::new();

// Set a rate limit of 2 per minute for "test_type"
service.rate_limits.insert(
"test_type".to_string(),
RateLimit {
max_requests: 2,
per_duration: Duration::from_secs(60),
recipient_counters: HashMap::new(),
},
);

// Send two notifications within the rate limit
for _ in 0..2 {
let result = service.send("test_type", "test_recipient", "Test message");
assert!(result.is_ok());
}
}

#[test]
fn test_send_notification_exceeds_rate_limit() {
let mut service = NotificationService::new();

// Set a rate limit of 2 per minute for "test_type"
service.rate_limits.insert(
"test_type".to_string(),
RateLimit {
max_requests: 2,
per_duration: Duration::from_secs(60),
recipient_counters: HashMap::new(),
},
);

// Send two notifications within the rate limit
for _ in 0..2 {
let success_result = service.send("test_type", "test_recipient", "Test message");
assert_eq!(success_result, Ok(()));
}

// Attempt to send a third notification, which should exceed the rate limit
let exceed_limit_result = service.send("test_type", "test_recipient", "Test message");
assert!(exceed_limit_result.is_err());
}
}

0 comments on commit b300cc2

Please sign in to comment.