Skip to content

Commit

Permalink
Events for simple conn
Browse files Browse the repository at this point in the history
listen_event method added to simple connection type

issue #149
  • Loading branch information
fernandobatels committed Nov 2, 2023
1 parent 682f4f4 commit 38bf9c9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/connection/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl SimpleConnection {
}

/// Wait for an event to be posted on database
pub(crate) fn wait_for_event(&mut self, name: String) -> Result<(), FbError> {
pub fn wait_for_event(&mut self, name: String) -> Result<(), FbError> {

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v3, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v3, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v3, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v2, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v2, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v2, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (ubuntu-20.04, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (windows-latest, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (windows-latest, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (windows-latest, v4, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (macos-latest, v3, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (macos-latest, v3, pure_rust)

unused variable: `name`

Check warning on line 181 in src/connection/simple.rs

View workflow job for this annotation

GitHub Actions / testing (macos-latest, v3, pure_rust)

unused variable: `name`
match &mut self.inner {
#[cfg(feature = "linking")]
TypeConnectionContainer::NativeDynLink(c) => c.wait_for_event(name),
Expand Down
59 changes: 53 additions & 6 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,32 @@ where
fn listen_event(
self,
name: String,
mut handler: F,
handler: F,
) -> Result<JoinHandle<Result<(), FbError>>, FbError> {
let mut conn: SimpleConnection = self.into();
let conn: SimpleConnection = self.into();

return conn.listen_event(name, handler);

Check warning on line 40 in src/events.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/events.rs:40:9 | 40 | return conn.listen_event(name, handler); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 40 - return conn.listen_event(name, handler); 40 + conn.listen_event(name, handler) |
}
}

impl<F, C> RemoteEventsManager<F, C> for SimpleConnection
where
C: FirebirdClient + FirebirdClientDbEvents + 'static,
F: FnMut(&mut SimpleConnection) -> Result<bool, FbError> + Send + Sync + 'static,
SimpleConnection: From<Connection<C>>,
{
fn listen_event(
mut self,
name: String,
mut handler: F,
) -> Result<JoinHandle<Result<(), FbError>>, FbError> {
return Ok(thread::spawn(move || {
let mut hold = true;

while hold {
conn.wait_for_event(name.clone())?;
self.wait_for_event(name.clone())?;

hold = handler(&mut conn)?;
hold = handler(&mut self)?;
}

Ok(())
Expand Down Expand Up @@ -98,6 +113,38 @@ mk_tests_default! {
Ok(())
}

#[test]
#[ignore]
#[cfg(all(not(feature = "pure_rust"), not(feature = "embedded_tests")))]
fn remote_events_simple_conn() -> Result<(), FbError> {
let conn1: SimpleConnection = cbuilder().connect()?.into();
let mut conn2: SimpleConnection = cbuilder().connect()?.into();

let counter = Arc::new(Mutex::new(0)).clone();

let acounter = Arc::clone(&counter);
let _ = conn1.listen_event("evento2".to_string(), move |c| {

let (_,): (i32,) = c.query_first(
"select 1 from rdb$database",
(),
)?.unwrap();

let mut num = acounter.lock().unwrap();
*num += 1;

Ok(*num < 2)
})?;

thread::sleep(Duration::from_secs(2));

conn2.execute("execute block as begin POST_EVENT 'evento2'; end", ())?;
thread::sleep(Duration::from_secs(2));
assert_eq!(1, *counter.lock().unwrap());

Ok(())
}

#[test]
#[ignore]
#[cfg(all(not(feature = "pure_rust"), not(feature = "embedded_tests")))]
Expand All @@ -109,11 +156,11 @@ mk_tests_default! {
thread::spawn(move || {
thread::sleep(Duration::from_secs(2));

conn1.execute("execute block as begin POST_EVENT 'evento'; end", ())
conn1.execute("execute block as begin POST_EVENT 'evento3'; end", ())
});

let wait = thread::spawn(move || {
conn2.wait_for_event("evento".to_string())
conn2.wait_for_event("evento3".to_string())
});

thread::sleep(Duration::from_secs(10));
Expand Down

0 comments on commit 38bf9c9

Please sign in to comment.