Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(AsyncEventPlugin): helper to create async events #32

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions falco_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ pub mod parse {
/// use falco_plugin::base::Plugin;
/// use falco_plugin::{async_event_plugin, plugin};
/// use falco_plugin::async_event::{
/// AsyncEvent,
/// AsyncEventPlugin,
/// AsyncHandler,
/// BackgroundTask};
Expand Down Expand Up @@ -249,22 +248,8 @@ pub mod parse {
/// // start a new thread
/// // waiting up to 100ms between events for the stop request
/// self.thread = Some(self.task.spawn(std::time::Duration::from_millis(100), move || {
/// // build an event
/// let event = AsyncEvent {
/// plugin_id: Some(0),
/// name: Some(c"sample_async"),
/// data: Some(b"hello"),
/// };
///
/// let metadata = EventMetadata::default();
///
/// let event = Event {
/// metadata,
/// params: event,
/// };
///
/// // submit it to the main event loop
/// handler.emit(event)?;
/// // submit an async event to the main event loop
/// handler.emit(Self::async_event(c"sample_async", b"hello"))?;
/// Ok(())
/// })?);
/// Ok(())
Expand Down
19 changes: 19 additions & 0 deletions falco_plugin/src/plugin/async_event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::base::Plugin;
use crate::plugin::async_event::async_handler::AsyncHandler;
use crate::plugin::async_event::wrappers::AsyncPluginExported;

use falco_event::events::types::PPME_ASYNCEVENT_E as AsyncEvent;
use falco_event::events::Event;

pub mod async_handler;
pub mod background_task;
#[doc(hidden)]
Expand Down Expand Up @@ -48,4 +51,20 @@ pub trait AsyncEventPlugin: Plugin + AsyncPluginExported {
///
/// **Note**: [`AsyncEventPlugin::start_async`] can be called again, with a different [`AsyncHandler`].
fn stop_async(&mut self) -> Result<(), anyhow::Error>;

/// # A helper method to create an asynchronous event
fn async_event<'a>(name: &'a std::ffi::CStr, data: &'a [u8]) -> Event<AsyncEvent<'a>> {
let event = AsyncEvent {
plugin_id: None, // gets populated by the framework, shall be None
name: Some(name),
data: Some(data),
};

let metadata = falco_event::events::EventMetadata::default();

Event {
metadata,
params: event,
}
}
}
35 changes: 5 additions & 30 deletions falco_plugin_tests/tests/async.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use falco_plugin::anyhow::{self, Error};
use falco_plugin::async_event::{AsyncEvent, AsyncEventPlugin, AsyncHandler, BackgroundTask};
use falco_plugin::async_event::{AsyncEventPlugin, AsyncHandler, BackgroundTask};
use falco_plugin::base::Plugin;
use falco_plugin::event::events::{Event, EventMetadata};
use falco_plugin::extract::EventInput;
use falco_plugin::source::{EventBatch, SourcePlugin, SourcePluginInstance};
use falco_plugin::tables::TablesInput;
Expand Down Expand Up @@ -70,34 +69,10 @@ impl AsyncEventPlugin for DummyPlugin {

self.thread = Some(self.task.spawn(Duration::from_millis(100), move || {
dbg!("emitting event");
let event = AsyncEvent {
plugin_id: Some(0),
name: Some(c"dummy_async"),
data: Some(b"hello"),
};

let metadata = EventMetadata::default();

let event = Event {
metadata,
params: event,
};
handler.emit(event)?;

let event = AsyncEvent {
plugin_id: Some(0),
name: Some(c"invalid_event_name"),
data: Some(b"hello"),
};

let metadata = EventMetadata::default();

let event = Event {
metadata,
params: event,
};
assert!(handler.emit(event).is_err());

handler.emit(Self::async_event(c"dummy_async", b"hello"))?;
assert!(handler
.emit(Self::async_event(c"invalid_event_name", b"hello"))
.is_err());
Ok(())
})?);

Expand Down
Loading