2018-08-07 12:01:15 +00:00
|
|
|
/*
|
2020-02-04 13:52:12 +00:00
|
|
|
* meli
|
2018-08-07 12:01:15 +00:00
|
|
|
*
|
|
|
|
* Copyright 2017-2018 Manos Pitsidianakis
|
|
|
|
*
|
|
|
|
* This file is part of meli.
|
|
|
|
*
|
|
|
|
* meli is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* meli is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-02-04 13:52:12 +00:00
|
|
|
|
|
|
|
/*! UI types used throughout meli.
|
|
|
|
*
|
2023-04-30 16:39:41 +00:00
|
|
|
* The `segment_tree` module performs maximum range queries. This is used in
|
|
|
|
* getting the maximum element of a column within a specific range in e-mail
|
|
|
|
* lists. That way a very large value that is not the in the currently
|
|
|
|
* displayed page does not cause the column to be rendered bigger
|
2020-02-04 13:52:12 +00:00
|
|
|
* than it has to.
|
|
|
|
*
|
2023-04-30 16:39:41 +00:00
|
|
|
* `UIMode` describes the application's... mode. Same as in the modal editor
|
|
|
|
* `vi`.
|
2020-02-04 13:52:12 +00:00
|
|
|
*
|
|
|
|
* `UIEvent` is the type passed around `Component`s when something happens.
|
|
|
|
*/
|
2019-03-03 12:24:15 +00:00
|
|
|
extern crate serde;
|
2018-08-08 08:50:51 +00:00
|
|
|
#[macro_use]
|
2018-08-06 13:53:23 +00:00
|
|
|
mod helpers;
|
2023-04-30 16:39:41 +00:00
|
|
|
use std::{borrow::Cow, fmt, sync::Arc};
|
2018-08-06 13:53:23 +00:00
|
|
|
|
2023-04-30 16:39:41 +00:00
|
|
|
use melib::{
|
|
|
|
backends::{AccountHash, BackendEvent, MailboxHash},
|
|
|
|
uuid::Uuid,
|
|
|
|
EnvelopeHash, RefreshEvent, ThreadHash,
|
|
|
|
};
|
2019-11-19 20:47:34 +00:00
|
|
|
use nix::unistd::Pid;
|
2023-04-30 16:39:41 +00:00
|
|
|
|
|
|
|
pub use self::helpers::*;
|
|
|
|
use super::{
|
|
|
|
command::Action,
|
|
|
|
jobs::{JobExecutor, JobId},
|
|
|
|
terminal::*,
|
|
|
|
};
|
|
|
|
use crate::components::{Component, ComponentId, ScrollUpdate};
|
2018-08-06 13:53:23 +00:00
|
|
|
|
2018-08-26 16:29:12 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum StatusEvent {
|
|
|
|
DisplayMessage(String),
|
|
|
|
BufClear,
|
|
|
|
BufSet(String),
|
2019-09-15 06:41:52 +00:00
|
|
|
UpdateStatus(String),
|
2021-09-12 14:39:51 +00:00
|
|
|
UpdateSubStatus(String),
|
2020-06-30 08:40:26 +00:00
|
|
|
NewJob(JobId),
|
|
|
|
JobFinished(JobId),
|
2020-07-24 19:06:19 +00:00
|
|
|
JobCanceled(JobId),
|
2020-10-14 09:50:38 +00:00
|
|
|
SetMouse(bool),
|
2021-01-07 18:26:17 +00:00
|
|
|
ScrollUpdate(ScrollUpdate),
|
2018-08-26 16:29:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-30 16:39:41 +00:00
|
|
|
/// `ThreadEvent` encapsulates all of the possible values we need to transfer
|
|
|
|
/// between our threads to the main process.
|
2018-08-06 13:53:23 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ThreadEvent {
|
|
|
|
/// User input.
|
2020-05-29 12:35:29 +00:00
|
|
|
Input((Key, Vec<u8>)),
|
2019-11-05 06:32:27 +00:00
|
|
|
/// User input and input as raw bytes.
|
2020-02-26 08:54:10 +00:00
|
|
|
/// A watched Mailbox has been refreshed.
|
2018-09-12 12:10:19 +00:00
|
|
|
RefreshMailbox(Box<RefreshEvent>),
|
2019-04-10 20:37:20 +00:00
|
|
|
UIEvent(UIEvent),
|
2019-09-11 14:57:55 +00:00
|
|
|
/// A thread has updated some of its information
|
|
|
|
Pulse,
|
2018-08-06 13:53:23 +00:00
|
|
|
//Decode { _ }, // For gpg2 signature check
|
2020-06-26 15:31:37 +00:00
|
|
|
JobFinished(JobId),
|
2018-08-06 13:53:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RefreshEvent> for ThreadEvent {
|
|
|
|
fn from(event: RefreshEvent) -> Self {
|
2018-09-12 12:10:19 +00:00
|
|
|
ThreadEvent::RefreshMailbox(Box::new(event))
|
2018-08-06 13:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ForkType {
|
2019-11-05 06:32:27 +00:00
|
|
|
/// Already finished fork, we only want to restore input/output
|
|
|
|
Finished,
|
|
|
|
/// Embed pty
|
2019-11-19 20:47:34 +00:00
|
|
|
Embed(Pid),
|
2018-08-06 13:53:23 +00:00
|
|
|
Generic(std::process::Child),
|
|
|
|
NewDraft(File, std::process::Child),
|
|
|
|
}
|
|
|
|
|
2022-11-24 17:58:23 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2019-09-15 20:35:30 +00:00
|
|
|
pub enum NotificationType {
|
2020-09-13 12:23:14 +00:00
|
|
|
Info,
|
|
|
|
Error(melib::error::ErrorKind),
|
|
|
|
NewMail,
|
|
|
|
SentMail,
|
|
|
|
Saved,
|
2020-09-10 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl core::fmt::Display for NotificationType {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-09-13 12:23:14 +00:00
|
|
|
match *self {
|
|
|
|
NotificationType::Info => write!(f, "info"),
|
|
|
|
NotificationType::Error(melib::error::ErrorKind::None) => write!(f, "error"),
|
|
|
|
NotificationType::Error(kind) => write!(f, "error: {}", kind),
|
|
|
|
NotificationType::NewMail => write!(f, "new mail"),
|
|
|
|
NotificationType::SentMail => write!(f, "sent mail"),
|
|
|
|
NotificationType::Saved => write!(f, "saved"),
|
|
|
|
}
|
2020-09-10 17:31:12 +00:00
|
|
|
}
|
2019-09-15 20:35:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 13:53:23 +00:00
|
|
|
#[derive(Debug)]
|
2019-04-10 20:37:20 +00:00
|
|
|
pub enum UIEvent {
|
2018-08-06 13:53:23 +00:00
|
|
|
Input(Key),
|
2020-07-25 10:08:36 +00:00
|
|
|
CmdInput(Key),
|
2019-02-25 09:11:56 +00:00
|
|
|
InsertInput(Key),
|
2019-11-05 06:32:27 +00:00
|
|
|
EmbedInput((Key, Vec<u8>)),
|
2018-08-06 13:53:23 +00:00
|
|
|
//Quit?
|
|
|
|
Resize,
|
|
|
|
/// Force redraw.
|
|
|
|
Fork(ForkType),
|
|
|
|
ChangeMailbox(usize),
|
|
|
|
ChangeMode(UIMode),
|
|
|
|
Command(String),
|
2019-09-15 20:35:30 +00:00
|
|
|
Notification(Option<String>, String, Option<NotificationType>),
|
2018-08-06 13:53:23 +00:00
|
|
|
Action(Action),
|
2018-08-26 16:29:12 +00:00
|
|
|
StatusEvent(StatusEvent),
|
2020-08-17 12:31:30 +00:00
|
|
|
MailboxUpdate((AccountHash, MailboxHash)), // (account_idx, mailbox_idx)
|
|
|
|
MailboxDelete((AccountHash, MailboxHash)),
|
|
|
|
MailboxCreate((AccountHash, MailboxHash)),
|
2022-09-11 12:00:30 +00:00
|
|
|
AccountStatusChange(AccountHash, Option<Cow<'static, str>>),
|
2019-04-10 19:01:02 +00:00
|
|
|
ComponentKill(Uuid),
|
2020-08-19 22:55:24 +00:00
|
|
|
BackendEvent(AccountHash, BackendEvent),
|
2020-02-26 08:54:10 +00:00
|
|
|
StartupCheck(MailboxHash),
|
2018-09-15 17:09:41 +00:00
|
|
|
RefreshEvent(Box<RefreshEvent>),
|
2019-04-01 20:53:06 +00:00
|
|
|
EnvelopeUpdate(EnvelopeHash),
|
2019-06-05 21:27:40 +00:00
|
|
|
EnvelopeRename(EnvelopeHash, EnvelopeHash), // old_hash, new_hash
|
2020-06-23 14:23:42 +00:00
|
|
|
EnvelopeRemove(EnvelopeHash, ThreadHash),
|
2020-02-19 14:57:37 +00:00
|
|
|
Contacts(ContactEvent),
|
|
|
|
Compose(ComposeEvent),
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 11:38:43 +00:00
|
|
|
FinishedUIDialog(ComponentId, UIMessage),
|
2020-08-25 12:39:43 +00:00
|
|
|
Callback(CallbackFn),
|
Add smtp client support for sending mail in UI
`mailer_command` was removed, and a new setting `send_mail` was added.
Its possible values are a string, consisting of a shell command to
execute, or settings to configure an smtp server connection. The
configuration I used for testing this is:
[composing]
send_mail = { hostname = "smtp.mail.tld", port = 587, auth = { type = "auto", username = "yoshi", password = { type = "command_eval", value = "gpg2 --no-tty -q -d ~/.passwords/msmtp/yoshi.gpg" } }, security = { type = "STARTTLS" } }
For local smtp server:
[composing]
send_mail = { hostname = "localhost", port = 25, auth = { type = "none" }, security = { type = "none" } }
2020-07-15 11:38:43 +00:00
|
|
|
GlobalUIDialog(Box<dyn Component>),
|
2020-10-29 11:09:31 +00:00
|
|
|
Timer(Uuid),
|
2020-12-02 18:47:48 +00:00
|
|
|
ConfigReload {
|
2022-08-25 12:17:18 +00:00
|
|
|
old_settings: Box<crate::conf::Settings>,
|
2020-12-02 18:47:48 +00:00
|
|
|
},
|
2021-01-07 18:26:17 +00:00
|
|
|
VisibilityChange(bool),
|
2018-09-15 17:09:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 12:17:18 +00:00
|
|
|
pub struct CallbackFn(pub Box<dyn FnOnce(&mut crate::Context) + Send + 'static>);
|
2020-08-25 12:39:43 +00:00
|
|
|
|
|
|
|
impl core::fmt::Debug for CallbackFn {
|
|
|
|
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
|
|
|
|
write!(fmt, "CallbackFn")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:09:41 +00:00
|
|
|
impl From<RefreshEvent> for UIEvent {
|
|
|
|
fn from(event: RefreshEvent) -> Self {
|
2019-04-10 20:37:20 +00:00
|
|
|
UIEvent::RefreshEvent(Box::new(event))
|
2018-09-15 17:09:41 +00:00
|
|
|
}
|
2018-08-06 13:53:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 12:19:40 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2018-08-06 13:53:23 +00:00
|
|
|
pub enum UIMode {
|
|
|
|
Normal,
|
2019-02-25 09:11:56 +00:00
|
|
|
Insert,
|
2019-11-05 06:32:27 +00:00
|
|
|
/// Forward input to an embed pseudoterminal.
|
|
|
|
Embed,
|
2020-07-25 10:08:36 +00:00
|
|
|
Command,
|
2018-08-06 13:53:23 +00:00
|
|
|
Fork,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for UIMode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
match *self {
|
|
|
|
UIMode::Normal => "NORMAL",
|
2019-02-25 09:11:56 +00:00
|
|
|
UIMode::Insert => "INSERT",
|
2020-07-25 10:08:36 +00:00
|
|
|
UIMode::Command => "COMMAND",
|
2018-08-06 13:53:23 +00:00
|
|
|
UIMode::Fork => "FORK",
|
2019-11-05 06:32:27 +00:00
|
|
|
UIMode::Embed => "EMBED",
|
2018-08-06 13:53:23 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:11:32 +00:00
|
|
|
pub mod segment_tree {
|
2023-04-30 16:39:41 +00:00
|
|
|
/*! Simple segment tree implementation for maximum in range queries. This
|
|
|
|
* is useful if given an array of numbers you want to get the
|
|
|
|
* maximum value inside an interval quickly.
|
2019-12-12 09:11:32 +00:00
|
|
|
*/
|
2023-04-30 16:39:41 +00:00
|
|
|
use std::{convert::TryFrom, iter::FromIterator};
|
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
use smallvec::SmallVec;
|
2019-12-12 09:11:32 +00:00
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone)]
|
|
|
|
pub struct SegmentTree {
|
2020-06-07 11:25:33 +00:00
|
|
|
pub array: SmallVec<[u8; 1024]>,
|
2020-01-08 15:04:44 +00:00
|
|
|
tree: SmallVec<[u8; 1024]>,
|
2019-12-12 09:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
impl From<SmallVec<[u8; 1024]>> for SegmentTree {
|
|
|
|
fn from(val: SmallVec<[u8; 1024]>) -> SegmentTree {
|
2019-12-12 09:11:32 +00:00
|
|
|
SegmentTree::new(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SegmentTree {
|
2020-01-08 15:04:44 +00:00
|
|
|
pub fn new(val: SmallVec<[u8; 1024]>) -> SegmentTree {
|
2019-12-12 09:11:32 +00:00
|
|
|
if val.is_empty() {
|
|
|
|
return SegmentTree {
|
|
|
|
array: val.clone(),
|
|
|
|
tree: val,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let height = (f64::from(u32::try_from(val.len()).unwrap_or(0)))
|
|
|
|
.log2()
|
|
|
|
.ceil() as u32;
|
2020-01-15 10:31:49 +00:00
|
|
|
let max_size = 2 * (2_usize.pow(height));
|
2019-12-12 09:11:32 +00:00
|
|
|
|
2020-01-08 15:04:44 +00:00
|
|
|
let mut segment_tree: SmallVec<[u8; 1024]> =
|
|
|
|
SmallVec::from_iter(core::iter::repeat(0).take(max_size));
|
2019-12-12 09:11:32 +00:00
|
|
|
for i in 0..val.len() {
|
|
|
|
segment_tree[val.len() + i] = val[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in (1..val.len()).rev() {
|
|
|
|
segment_tree[i] = std::cmp::max(segment_tree[2 * i], segment_tree[2 * i + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SegmentTree {
|
|
|
|
array: val,
|
|
|
|
tree: segment_tree,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// (left, right) is inclusive
|
|
|
|
pub fn get_max(&self, mut left: usize, mut right: usize) -> u8 {
|
2020-10-05 15:56:58 +00:00
|
|
|
if self.array.is_empty() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:11:32 +00:00
|
|
|
let len = self.array.len();
|
|
|
|
debug_assert!(left <= right);
|
|
|
|
if right >= len {
|
|
|
|
right = len.saturating_sub(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
left += len;
|
|
|
|
right += len + 1;
|
|
|
|
|
|
|
|
let mut max = 0;
|
|
|
|
|
|
|
|
while left < right {
|
|
|
|
if (left & 1) > 0 {
|
|
|
|
max = std::cmp::max(max, self.tree[left]);
|
|
|
|
left += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (right & 1) > 0 {
|
|
|
|
right -= 1;
|
|
|
|
max = std::cmp::max(max, self.tree[right]);
|
|
|
|
}
|
|
|
|
|
|
|
|
left /= 2;
|
|
|
|
right /= 2;
|
|
|
|
}
|
|
|
|
max
|
|
|
|
}
|
2020-05-18 17:58:55 +00:00
|
|
|
|
|
|
|
pub fn update(&mut self, pos: usize, value: u8) {
|
|
|
|
let mut ctr = pos + self.array.len();
|
|
|
|
|
|
|
|
// Update leaf node value
|
|
|
|
self.tree[ctr] = value;
|
|
|
|
while ctr > 1 {
|
|
|
|
// move up one level
|
|
|
|
ctr >>= 1;
|
|
|
|
|
|
|
|
self.tree[ctr] = std::cmp::max(self.tree[2 * ctr], self.tree[2 * ctr + 1]);
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 09:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_segment_tree() {
|
2020-01-08 15:04:44 +00:00
|
|
|
let array: SmallVec<[u8; 1024]> = [9, 1, 17, 2, 3, 23, 4, 5, 6, 37]
|
2020-02-26 10:25:57 +00:00
|
|
|
.iter()
|
2019-12-12 09:11:32 +00:00
|
|
|
.cloned()
|
2020-01-08 15:04:44 +00:00
|
|
|
.collect::<SmallVec<[u8; 1024]>>();
|
2023-04-30 16:39:41 +00:00
|
|
|
let mut segment_tree = SegmentTree::from(array);
|
2019-12-12 09:11:32 +00:00
|
|
|
|
|
|
|
assert_eq!(segment_tree.get_max(0, 5), 23);
|
|
|
|
assert_eq!(segment_tree.get_max(6, 9), 37);
|
2020-05-18 17:58:55 +00:00
|
|
|
|
|
|
|
segment_tree.update(2_usize, 24_u8);
|
|
|
|
|
|
|
|
assert_eq!(segment_tree.get_max(0, 5), 24);
|
2019-12-12 09:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-15 10:36:31 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RateLimit {
|
|
|
|
last_tick: std::time::Instant,
|
2020-10-29 11:09:31 +00:00
|
|
|
pub timer: crate::jobs::Timer,
|
2020-01-15 10:36:31 +00:00
|
|
|
rate: std::time::Duration,
|
|
|
|
pub active: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RateLimit {
|
2020-10-29 11:09:31 +00:00
|
|
|
pub fn new(reqs: u64, millis: u64, job_executor: Arc<JobExecutor>) -> Self {
|
2020-01-15 10:36:31 +00:00
|
|
|
RateLimit {
|
|
|
|
last_tick: std::time::Instant::now(),
|
2020-10-29 11:09:31 +00:00
|
|
|
timer: job_executor.create_timer(
|
2020-01-15 10:36:31 +00:00
|
|
|
std::time::Duration::from_secs(0),
|
2020-05-18 17:58:20 +00:00
|
|
|
std::time::Duration::from_millis(millis),
|
2020-10-29 11:09:31 +00:00
|
|
|
),
|
2020-01-15 10:36:31 +00:00
|
|
|
rate: std::time::Duration::from_millis(millis / reqs),
|
|
|
|
active: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.last_tick = std::time::Instant::now();
|
|
|
|
self.active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) -> bool {
|
|
|
|
let now = std::time::Instant::now();
|
2020-05-18 17:58:20 +00:00
|
|
|
if self.last_tick + self.rate > now {
|
|
|
|
self.active = false;
|
|
|
|
} else {
|
2020-01-15 10:36:31 +00:00
|
|
|
self.timer.rearm();
|
2020-05-18 17:58:20 +00:00
|
|
|
self.last_tick = now;
|
2020-01-15 10:36:31 +00:00
|
|
|
self.active = true;
|
|
|
|
}
|
2020-05-18 17:58:20 +00:00
|
|
|
self.active
|
2020-01-15 10:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2020-10-29 11:09:31 +00:00
|
|
|
pub fn id(&self) -> Uuid {
|
|
|
|
self.timer.id()
|
2020-01-15 10:36:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 19:50:08 +00:00
|
|
|
|
2020-05-18 17:58:20 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rate_limit() {
|
2020-11-15 19:50:08 +00:00
|
|
|
/*
|
|
|
|
let (sender, receiver) =
|
|
|
|
crossbeam::channel::bounded(4096 * ::std::mem::size_of::<ThreadEvent>());
|
|
|
|
use std::sync::Arc;
|
|
|
|
let job_executor = Arc::new(JobExecutor::new(sender));
|
2020-05-18 17:58:20 +00:00
|
|
|
/* Accept at most one request per 3 milliseconds */
|
2020-11-15 19:50:08 +00:00
|
|
|
let mut rt = RateLimit::new(1, 3, job_executor.clone());
|
2020-05-18 17:58:20 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(2000));
|
|
|
|
/* assert that only one request per 3 milliseconds is accepted */
|
|
|
|
for _ in 0..5 {
|
|
|
|
assert!(rt.tick());
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
|
|
|
assert!(!rt.tick());
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
|
|
|
assert!(!rt.tick());
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
|
|
|
/* How many times was the signal handler called? We've slept for at least 3
|
|
|
|
* milliseconds, so it should have been called once */
|
|
|
|
let mut ctr = 0;
|
2020-11-15 19:50:08 +00:00
|
|
|
while receiver.try_recv().is_ok() {
|
2020-05-18 17:58:20 +00:00
|
|
|
ctr += 1;
|
2020-11-15 19:50:08 +00:00
|
|
|
println!("got {}", ctr);
|
2020-05-18 17:58:20 +00:00
|
|
|
}
|
2020-11-15 19:50:08 +00:00
|
|
|
println!("ctr = {} {}", ctr, ctr == 1);
|
2020-05-18 17:58:20 +00:00
|
|
|
assert_eq!(ctr, 1);
|
|
|
|
}
|
|
|
|
/* next, test at most 100 requests per second */
|
2020-11-15 19:50:08 +00:00
|
|
|
let mut rt = RateLimit::new(100, 1000, job_executor.clone());
|
2020-05-18 17:58:20 +00:00
|
|
|
for _ in 0..5 {
|
|
|
|
let mut ctr = 0;
|
|
|
|
for _ in 0..500 {
|
|
|
|
if rt.tick() {
|
|
|
|
ctr += 1;
|
|
|
|
}
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(2));
|
|
|
|
}
|
|
|
|
/* around 100 requests should succeed. might be 99 if in first loop, since
|
|
|
|
* RateLimit::new() has a delay */
|
|
|
|
assert!(ctr > 97 && ctr < 103);
|
|
|
|
/* alarm should expire in 1 second */
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
|
|
/* How many times was the signal handler called? */
|
|
|
|
ctr = 0;
|
2020-11-15 19:50:08 +00:00
|
|
|
while receiver.try_recv().is_ok() {
|
2020-05-18 17:58:20 +00:00
|
|
|
ctr += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(ctr, 1);
|
|
|
|
}
|
|
|
|
/* next, test at most 500 requests per second */
|
2020-11-15 19:50:08 +00:00
|
|
|
let mut rt = RateLimit::new(500, 1000, job_executor.clone());
|
2020-05-18 17:58:20 +00:00
|
|
|
for _ in 0..5 {
|
|
|
|
let mut ctr = 0;
|
|
|
|
for _ in 0..500 {
|
|
|
|
if rt.tick() {
|
|
|
|
ctr += 1;
|
|
|
|
}
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(2));
|
|
|
|
}
|
|
|
|
/* all requests should succeed. */
|
|
|
|
assert!(ctr < 503 && ctr > 497);
|
|
|
|
/* alarm should expire in 1 second */
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
|
|
/* How many times was the signal handler called? */
|
|
|
|
ctr = 0;
|
2020-11-15 19:50:08 +00:00
|
|
|
while receiver.try_recv().is_ok() {
|
2020-05-18 17:58:20 +00:00
|
|
|
ctr += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(ctr, 1);
|
|
|
|
}
|
2020-11-15 19:50:08 +00:00
|
|
|
*/
|
2020-05-18 17:58:20 +00:00
|
|
|
}
|
2020-02-19 14:57:37 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ContactEvent {
|
|
|
|
CreateContacts(Vec<melib::Card>),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ComposeEvent {
|
|
|
|
SetReceipients(Vec<melib::Address>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type UIMessage = Box<dyn 'static + std::any::Any + Send + Sync>;
|