mod.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //! # All commands handlers
  2. use std::time::{SystemTime, UNIX_EPOCH};
  3. use tokio::time::{Duration, Instant};
  4. pub mod client;
  5. pub mod hash;
  6. pub mod help;
  7. pub mod key;
  8. pub mod list;
  9. pub mod metrics;
  10. pub mod pubsub;
  11. pub mod server;
  12. pub mod set;
  13. pub mod string;
  14. pub mod transaction;
  15. /// Returns the current time
  16. pub fn now() -> Duration {
  17. let start = SystemTime::now();
  18. start
  19. .duration_since(UNIX_EPOCH)
  20. .expect("Time went backwards")
  21. }
  22. #[cfg(test)]
  23. mod test {
  24. use crate::{
  25. connection::{connections::Connections, Connection},
  26. db::pool::Databases,
  27. dispatcher::Dispatcher,
  28. error::Error,
  29. value::Value,
  30. };
  31. use bytes::Bytes;
  32. use std::{
  33. collections::VecDeque,
  34. net::{IpAddr, Ipv4Addr, SocketAddr},
  35. sync::Arc,
  36. };
  37. use tokio::sync::mpsc::Receiver;
  38. pub fn create_connection() -> Arc<Connection> {
  39. let (default_db, all_dbs) = Databases::new(16, 1000);
  40. let all_connections = Arc::new(Connections::new(all_dbs));
  41. let client = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
  42. all_connections.new_connection(default_db, client).1
  43. }
  44. pub fn create_connection_and_pubsub() -> (Receiver<Value>, Arc<Connection>) {
  45. let (default_db, all_dbs) = Databases::new(16, 1000);
  46. let all_connections = Arc::new(Connections::new(all_dbs));
  47. let client = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
  48. all_connections.new_connection(default_db, client)
  49. }
  50. pub async fn invalid_type(cmd: &[&str]) {
  51. let c = create_connection();
  52. let _ = run_command(&c, &["set", "key", "test"]).await;
  53. assert_eq!(Err(Error::WrongType), run_command(&c, cmd).await);
  54. }
  55. pub fn create_new_connection_from_connection(
  56. conn: &Connection,
  57. ) -> (Receiver<Value>, Arc<Connection>) {
  58. let all_connections = conn.all_connections();
  59. let client = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
  60. all_connections.new_connection(
  61. all_connections.get_databases().get(0).expect("DB(0)"),
  62. client,
  63. )
  64. }
  65. pub async fn run_command(conn: &Connection, cmd: &[&str]) -> Result<Value, Error> {
  66. let args: VecDeque<Bytes> = cmd.iter().map(|s| Bytes::from(s.to_string())).collect();
  67. let dispatcher = Dispatcher::new();
  68. dispatcher.execute(conn, args).await
  69. }
  70. #[tokio::test]
  71. async fn total_connections() {
  72. let c = create_connection();
  73. let all_connections = c.all_connections();
  74. assert_eq!(1, all_connections.total_connections());
  75. let client = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
  76. let c2 = c.all_connections().new_connection(c.db(), client).1;
  77. c2.block();
  78. assert_eq!(2, all_connections.total_connections());
  79. assert_eq!(1, all_connections.total_blocked_connections());
  80. c2.destroy();
  81. assert_eq!(1, all_connections.total_connections());
  82. assert_eq!(0, all_connections.total_blocked_connections());
  83. }
  84. }