From: Kent Gibson <warthog618@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
linux-gpio@vger.kernel.org,
Bartosz Golaszewski <bgolaszewski@baylibre.com>
Subject: Re: [libgpiod][PATCH 3/7] tests: event: extend test coverage for reading multiple line events at once
Date: Thu, 19 Dec 2019 13:35:02 +0000 [thread overview]
Message-ID: <20191219133502.GA12028@firefly> (raw)
In-Reply-To: <20191218142449.10957-4-brgl@bgdev.pl>
On Wed, Dec 18, 2019 at 03:24:45PM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> Add test cases for new helpers allowing users to read multiple events
> at once.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---
> tests/tests-event.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 83 insertions(+)
>
> diff --git a/tests/tests-event.c b/tests/tests-event.c
> index d425d1a..1f4a2eb 100644
> --- a/tests/tests-event.c
> +++ b/tests/tests-event.c
> @@ -552,3 +552,86 @@ GPIOD_TEST_CASE(invalid_fd, 0, { 8 })
> g_assert_cmpint(ret, ==, -1);
> g_assert_cmpint(errno, ==, EINVAL);
> }
> +
> +GPIOD_TEST_CASE(read_multiple_events, 0, { 8 })
> +{
> + g_autoptr(gpiod_chip_struct) chip = NULL;
> + struct gpiod_line_event events[3];
> + struct timespec ts = { 1, 0 };
> + struct gpiod_line *line;
> + gint ret;
> +
> + chip = gpiod_chip_open(gpiod_test_chip_path(0));
> + g_assert_nonnull(chip);
> + gpiod_test_return_if_failed();
> +
> + line = gpiod_chip_get_line(chip, 4);
> + g_assert_nonnull(line);
> + gpiod_test_return_if_failed();
> +
> + ret = gpiod_line_request_both_edges_events(line, GPIOD_TEST_CONSUMER);
> + g_assert_cmpint(ret, ==, 0);
> +
> + gpiod_test_chip_set_pull(0, 4, 1);
> + usleep(10000);
> + gpiod_test_chip_set_pull(0, 4, 0);
> + usleep(10000);
> + gpiod_test_chip_set_pull(0, 4, 1);
> + usleep(10000);
> +
I assume the sleep is to wait for the event to be generated from the
call gpiod_test_chip_set_pull, which is not guaranteed to occur before
the call returns, otherwise you can toggle the line too fast and may
miss events.
Arbitrary sleeps in code, including tests, should be avoided as they
are brittle and obsure what you are actually waiting for.
An alternative in this case is to add a second event fd and wait for
the event to arrive there before continuing.
Kent.
> + ret = gpiod_line_event_wait(line, &ts);
> + g_assert_cmpint(ret, ==, 1);
> +
> + ret = gpiod_line_event_read_multiple(line, events, 3);
> + g_assert_cmpint(ret, ==, 3);
> +
> + g_assert_cmpint(events[0].event_type, ==,
> + GPIOD_LINE_EVENT_RISING_EDGE);
> + g_assert_cmpint(events[1].event_type, ==,
> + GPIOD_LINE_EVENT_FALLING_EDGE);
> + g_assert_cmpint(events[2].event_type, ==,
> + GPIOD_LINE_EVENT_RISING_EDGE);
> +}
> +
> +GPIOD_TEST_CASE(read_multiple_events_fd, 0, { 8 })
> +{
> + g_autoptr(gpiod_chip_struct) chip = NULL;
> + struct gpiod_line_event events[3];
> + struct timespec ts = { 1, 0 };
> + struct gpiod_line *line;
> + gint ret, fd;
> +
> + chip = gpiod_chip_open(gpiod_test_chip_path(0));
> + g_assert_nonnull(chip);
> + gpiod_test_return_if_failed();
> +
> + line = gpiod_chip_get_line(chip, 4);
> + g_assert_nonnull(line);
> + gpiod_test_return_if_failed();
> +
> + ret = gpiod_line_request_both_edges_events(line, GPIOD_TEST_CONSUMER);
> + g_assert_cmpint(ret, ==, 0);
> +
> + gpiod_test_chip_set_pull(0, 4, 1);
> + usleep(10000);
> + gpiod_test_chip_set_pull(0, 4, 0);
> + usleep(10000);
> + gpiod_test_chip_set_pull(0, 4, 1);
> + usleep(10000);
> +
> + ret = gpiod_line_event_wait(line, &ts);
> + g_assert_cmpint(ret, ==, 1);
> +
> + fd = gpiod_line_event_get_fd(line);
> + g_assert_cmpint(fd, >=, 0);
> +
> + ret = gpiod_line_event_read_fd_multiple(fd, events, 3);
> + g_assert_cmpint(ret, ==, 3);
> +
> + g_assert_cmpint(events[0].event_type, ==,
> + GPIOD_LINE_EVENT_RISING_EDGE);
> + g_assert_cmpint(events[1].event_type, ==,
> + GPIOD_LINE_EVENT_FALLING_EDGE);
> + g_assert_cmpint(events[2].event_type, ==,
> + GPIOD_LINE_EVENT_RISING_EDGE);
> +}
> --
> 2.23.0
>
next prev parent reply other threads:[~2019-12-19 13:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-18 14:24 [libgpiod][PATCH 0/7] teach libgpiod to read multiple line events at once Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 1/7] core: use gpiod_line_event_get_fd() in gpiod_line_event_read() Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 2/7] core: provide functions for reading multiple line events at once Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 3/7] tests: event: extend test coverage " Bartosz Golaszewski
2019-12-19 13:35 ` Kent Gibson [this message]
2019-12-19 13:48 ` Bartosz Golaszewski
2019-12-19 14:05 ` Kent Gibson
2019-12-19 14:07 ` Bartosz Golaszewski
2019-12-19 14:36 ` Kent Gibson
2019-12-19 16:19 ` Bartosz Golaszewski
2019-12-24 12:11 ` Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 4/7] bindings: cxx: provide a method for reading multiple line events Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 5/7] bindings: cxx: tests: add a test-case " Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 6/7] bindings: python: add a method " Bartosz Golaszewski
2019-12-18 14:24 ` [libgpiod][PATCH 7/7] bindings: python: tests: add a test-case " Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20191219133502.GA12028@firefly \
--to=warthog618@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=bgolaszewski@baylibre.com \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox