[Libguestfs] [PATCH] Rust bindings: Implement Event features

Pino Toscano ptoscano at redhat.com
Wed Jul 31 15:00:49 UTC 2019


Hi Hiroyuki,

On Tuesday, 30 July 2019 07:51:37 CEST Hiroyuki Katsura wrote:
> This patch includes:
> 
> - Event callback handlers
> - Tests related to events(410-430)
> ---

Would it be possible to split the Handle -> Handle<'a> change in an own
small patch? This way it can be documented why it was changed.

> +pub fn event_to_string(events: &[guestfs::Event]) -> Result<String, error::Error> {
> +    let bitmask = events_to_bitmask(events);
> +
> +    let r = unsafe { guestfs_event_to_string(bitmask) };
> +    if r.is_null() {
> +        Err(error::unix_error("event_to_string"))
> +    } else {
> +        let s = unsafe { ffi::CStr::from_ptr(r) };
> +        let s = s.to_str()?.to_string();

These two look like utils::char_ptr_to_string().

> diff --git a/rust/tests/410_close_event.rs b/rust/tests/410_close_event.rs
> new file mode 100644
> index 000000000..308471098
> --- /dev/null
> +++ b/rust/tests/410_close_event.rs
> @@ -0,0 +1,38 @@
> +/* libguestfs Rust bindings
> + * Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513 at gmail.com>
> + *
> + * This program 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 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program 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 this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +extern crate guestfs;
> +
> +use std::sync::{Arc, Mutex};
> +
> +#[test]
> +fn close_event() {
> +    let close_invoked = Arc::new(Mutex::new(0));

Maybe a thread-safe Arc is not needed for this test -- it's just a
single-threaded test with just one handle.

> +    {
> +        let mut g = guestfs::Handle::create().expect("create");
> +        g.set_event_callback(
> +            |_, _, _, _| {
> +                let mut data = (&close_invoked).lock().unwrap();
> +                *data += 1;
> +            },
> +            &[guestfs::Event::Close],
> +        )
> +        .unwrap();

Check that the close_invoked count is still 0 before the block ending.

> diff --git a/rust/tests/420_log_messages.rs b/rust/tests/420_log_messages.rs
> new file mode 100644
> index 000000000..1e9627ca7
> --- /dev/null
> +++ b/rust/tests/420_log_messages.rs
> @@ -0,0 +1,60 @@
> +/* libguestfs Rust bindings
> + * Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513 at gmail.com>
> + *
> + * This program 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 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program 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 this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +extern crate guestfs;
> +
> +use std::str;
> +use std::sync::{Arc, Mutex};
> +
> +#[test]
> +fn log_messages() {
> +    let close_invoked = Arc::new(Mutex::new(0));

Ditto as in 410_close_event.rs.

> diff --git a/rust/tests/430_progress_messages.rs b/rust/tests/430_progress_messages.rs
> new file mode 100644
> index 000000000..a1d33aff7
> --- /dev/null
> +++ b/rust/tests/430_progress_messages.rs
> @@ -0,0 +1,61 @@
> +/* libguestfs Rust bindings
> + * Copyright (C) 2019 Hiroyuki Katsura <hiroyuki.katsura.0513 at gmail.com>
> + *
> + * This program 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 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program 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 this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + */
> +
> +extern crate guestfs;
> +
> +use std::default::Default;
> +use std::sync::{Arc, Mutex};
> +
> +#[test]
> +fn progress_messages() {
> +    let callback_invoked = Arc::new(Mutex::new(0));

Ditto as in 410_close_event.rs.

> +    {
> +        let mut g = guestfs::Handle::create().expect("create");
> +        g.add_drive("/dev/null", Default::default()).unwrap();
> +        g.launch().unwrap();
> +
> +        let eh = g
> +            .set_event_callback(
> +                |_, _, _, _| {
> +                    let mut data = (&callback_invoked).lock().unwrap();
> +                    *data += 1;
> +                },
> +                &[guestfs::Event::Progress],
> +            )
> +            .unwrap();
> +        assert_eq!("ok", g.debug("progress", &["5"]).unwrap());
> +        assert!(*(&callback_invoked).lock().unwrap() > 0);
> +
> +        *(&callback_invoked).lock().unwrap() = 0;
> +        g.delete_event_callback(eh).unwrap();
> +        assert_eq!("ok", g.debug("progress", &["5"]).unwrap());
> +        assert_eq!(*(&callback_invoked).lock().unwrap(), 0);
> +
> +        g.set_event_callback(
> +            |_, _, _, _| {
> +                let mut data = (&callback_invoked).lock().unwrap();
> +                *data += 1;
> +            },
> +            &[guestfs::Event::Progress],
> +        )
> +        .unwrap();
> +        assert_eq!("ok", g.debug("progress", &["5"]).unwrap());
> +        assert!(*(&callback_invoked).lock().unwrap() > 0);
> +    }
> +    assert!(*(&callback_invoked).lock().unwrap() > 0);

This assert is not needed, and most probably the whole scope here can
be removed.

-- 
Pino Toscano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://listman.redhat.com/archives/libguestfs/attachments/20190731/942ff5bb/attachment.sig>


More information about the Libguestfs mailing list