diff mbox

[libvirt,2/3] tests: eventtest: Open code virtTestResult

Message ID 36363a3385c25563f2aee28e2e134698593e64b5.1443570876.git.crobinso@redhat.com
State New
Headers show

Commit Message

Cole Robinson Sept. 29, 2015, 11:56 p.m. UTC
These event tests aren't run synchronously, so there isn't an obvious
function to pass to virtTestRun. Instead, open code roughly what
virtTestResult did before: printing an error message if a test failed.
---
 tests/eventtest.c | 57 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 47 insertions(+), 10 deletions(-)

Comments

Martin Kletzander Sept. 30, 2015, 5:38 a.m. UTC | #1
On Tue, Sep 29, 2015 at 07:56:46PM -0400, Cole Robinson wrote:
>These event tests aren't run synchronously, so there isn't an obvious
>function to pass to virtTestRun. Instead, open code roughly what
>virtTestResult did before: printing an error message if a test failed.
>---
> tests/eventtest.c | 57 +++++++++++++++++++++++++++++++++++++++++++++----------
> 1 file changed, 47 insertions(+), 10 deletions(-)
>
>diff --git a/tests/eventtest.c b/tests/eventtest.c
>index 13adbf6..ab08181 100644
>--- a/tests/eventtest.c
>+++ b/tests/eventtest.c
>@@ -63,6 +63,43 @@ enum {
>     EV_ERROR_DATA,
> };
>
>+struct testEventResultData {
>+    bool failed;
>+    const char *msg;
>+};
>+
>+static int
>+testEventResultCallback(const void *opaque)
>+{
>+    const struct testEventResultData *data = opaque;
>+
>+    if (data->failed && data->msg) {
>+        fprintf(stderr, "%s", data->msg);
>+    }
>+    return data->failed;
>+}
>+
>+static void
>+ATTRIBUTE_FMT_PRINTF(3,4)
>+testEventReport(const char *name, bool failed, const char *msg, ...)
>+{
>+    va_list vargs;
>+    va_start(vargs, msg);
>+    char *str = NULL;
>+    struct testEventResultData data;
>+
>+    if (msg && virVasprintfQuiet(&str, msg, vargs) != 0) {
>+        failed = true;
>+    }
>+
>+    data.failed = failed;
>+    data.msg = msg;

I think you meant data.msg = str; here.

ACK with that changed and your amendment squashed in.

P.S.: If you'd really like, I think this test could be made to use
virTestRun as well ;)
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Cole Robinson Sept. 30, 2015, 5:43 p.m. UTC | #2
On 09/30/2015 01:38 AM, Martin Kletzander wrote:
> On Tue, Sep 29, 2015 at 07:56:46PM -0400, Cole Robinson wrote:
>> These event tests aren't run synchronously, so there isn't an obvious
>> function to pass to virtTestRun. Instead, open code roughly what
>> virtTestResult did before: printing an error message if a test failed.
>> ---
>> tests/eventtest.c | 57 +++++++++++++++++++++++++++++++++++++++++++++----------
>> 1 file changed, 47 insertions(+), 10 deletions(-)
>>
>> diff --git a/tests/eventtest.c b/tests/eventtest.c
>> index 13adbf6..ab08181 100644
>> --- a/tests/eventtest.c
>> +++ b/tests/eventtest.c
>> @@ -63,6 +63,43 @@ enum {
>>     EV_ERROR_DATA,
>> };
>>
>> +struct testEventResultData {
>> +    bool failed;
>> +    const char *msg;
>> +};
>> +
>> +static int
>> +testEventResultCallback(const void *opaque)
>> +{
>> +    const struct testEventResultData *data = opaque;
>> +
>> +    if (data->failed && data->msg) {
>> +        fprintf(stderr, "%s", data->msg);
>> +    }
>> +    return data->failed;
>> +}
>> +
>> +static void
>> +ATTRIBUTE_FMT_PRINTF(3,4)
>> +testEventReport(const char *name, bool failed, const char *msg, ...)
>> +{
>> +    va_list vargs;
>> +    va_start(vargs, msg);
>> +    char *str = NULL;
>> +    struct testEventResultData data;
>> +
>> +    if (msg && virVasprintfQuiet(&str, msg, vargs) != 0) {
>> +        failed = true;
>> +    }
>> +
>> +    data.failed = failed;
>> +    data.msg = msg;
> 
> I think you meant data.msg = str; here.
> 
> ACK with that changed and your amendment squashed in.
> 

Changed locally, thanks for the review. I'll push after the release

> P.S.: If you'd really like, I think this test could be made to use
> virTestRun as well ;)

Maybe for another patch :)

- Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
John Ferlan Oct. 7, 2015, 1:24 p.m. UTC | #3
On 09/29/2015 07:56 PM, Cole Robinson wrote:
> These event tests aren't run synchronously, so there isn't an obvious
> function to pass to virtTestRun. Instead, open code roughly what
> virtTestResult did before: printing an error message if a test failed.
> ---
>  tests/eventtest.c | 57 +++++++++++++++++++++++++++++++++++++++++++++----------
>  1 file changed, 47 insertions(+), 10 deletions(-)
> 
> diff --git a/tests/eventtest.c b/tests/eventtest.c
> index 13adbf6..ab08181 100644
> --- a/tests/eventtest.c
> +++ b/tests/eventtest.c
> @@ -63,6 +63,43 @@ enum {
>      EV_ERROR_DATA,
>  };
>  
> +struct testEventResultData {
> +    bool failed;
> +    const char *msg;
> +};
> +
> +static int
> +testEventResultCallback(const void *opaque)
> +{
> +    const struct testEventResultData *data = opaque;
> +
> +    if (data->failed && data->msg) {
> +        fprintf(stderr, "%s", data->msg);
> +    }
> +    return data->failed;
> +}
> +
> +static void
> +ATTRIBUTE_FMT_PRINTF(3,4)
> +testEventReport(const char *name, bool failed, const char *msg, ...)
> +{
> +    va_list vargs;
> +    va_start(vargs, msg);
> +    char *str = NULL;
> +    struct testEventResultData data;
> +
> +    if (msg && virVasprintfQuiet(&str, msg, vargs) != 0) {
> +        failed = true;
> +    }
> +
> +    data.failed = failed;
> +    data.msg = msg;
> +    virtTestRun(name, testEventResultCallback, &data);

Now that these are pushed Coverity is complaining:

(3) Event check_return: 	Calling "virtTestRun" without checking return
value (as is done elsewhere 2547 out of 2548 times).
Also see events:

John
> +
> +    va_end(vargs);
> +    VIR_FREE(str);
> +}
> +
>  static void
>  testPipeReader(int watch, int fd, int events, void *data)
>  {
> @@ -148,13 +185,13 @@ verifyFired(const char *name, int handle, int timer)
>      for (i = 0; i < NUM_FDS; i++) {
>          if (handles[i].fired) {
>              if (i != handle) {
> -                virtTestResult(name, 1,
> +                testEventReport(name, 1,
>                                 "Handle %zu fired, but expected %d\n", i,
>                                 handle);
>                  return EXIT_FAILURE;
>              } else {
>                  if (handles[i].error != EV_ERROR_NONE) {
> -                    virtTestResult(name, 1,
> +                    testEventReport(name, 1,
>                                     "Handle %zu fired, but had error %d\n", i,
>                                     handles[i].error);
>                      return EXIT_FAILURE;
> @@ -163,7 +200,7 @@ verifyFired(const char *name, int handle, int timer)
>              }
>          } else {
>              if (i == handle) {
> -                virtTestResult(name, 1,
> +                testEventReport(name, 1,
>                                 "Handle %d should have fired, but didn't\n",
>                                 handle);
>                  return EXIT_FAILURE;
> @@ -171,7 +208,7 @@ verifyFired(const char *name, int handle, int timer)
>          }
>      }
>      if (handleFired != 1 && handle != -1) {
> -        virtTestResult(name, 1,
> +        testEventReport(name, 1,
>                         "Something weird happened, expecting handle %d\n",
>                         handle);
>          return EXIT_FAILURE;
> @@ -181,12 +218,12 @@ verifyFired(const char *name, int handle, int timer)
>      for (i = 0; i < NUM_TIME; i++) {
>          if (timers[i].fired) {
>              if (i != timer) {
> -                virtTestResult(name, 1,
> +                testEventReport(name, 1,
>                                 "Timer %zu fired, but expected %d\n", i, timer);
>                  return EXIT_FAILURE;
>              } else {
>                  if (timers[i].error != EV_ERROR_NONE) {
> -                    virtTestResult(name, 1,
> +                    testEventReport(name, 1,
>                                     "Timer %zu fired, but had error %d\n", i,
>                                     timers[i].error);
>                      return EXIT_FAILURE;
> @@ -195,7 +232,7 @@ verifyFired(const char *name, int handle, int timer)
>              }
>          } else {
>              if (i == timer) {
> -                virtTestResult(name, 1,
> +                testEventReport(name, 1,
>                                 "Timer %d should have fired, but didn't\n",
>                                 timer);
>                  return EXIT_FAILURE;
> @@ -203,7 +240,7 @@ verifyFired(const char *name, int handle, int timer)
>          }
>      }
>      if (timerFired != 1 && timer != -1) {
> -        virtTestResult(name, 1,
> +        testEventReport(name, 1,
>                         "Something weird happened, expecting timer %d\n",
>                         timer);
>          return EXIT_FAILURE;
> @@ -234,14 +271,14 @@ finishJob(const char *name, int handle, int timer)
>          rc = pthread_cond_timedwait(&eventThreadJobCond, &eventThreadMutex,
>                                      &waitTime);
>      if (rc != 0) {
> -        virtTestResult(name, 1, "Timed out waiting for pipe event\n");
> +        testEventReport(name, 1, "Timed out waiting for pipe event\n");
>          return EXIT_FAILURE;
>      }
>  
>      if (verifyFired(name, handle, timer) != EXIT_SUCCESS)
>          return EXIT_FAILURE;
>  
> -    virtTestResult(name, 0, NULL);
> +    testEventReport(name, 0, NULL);
>      return EXIT_SUCCESS;
>  }
>  
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
diff mbox

Patch

diff --git a/tests/eventtest.c b/tests/eventtest.c
index 13adbf6..ab08181 100644
--- a/tests/eventtest.c
+++ b/tests/eventtest.c
@@ -63,6 +63,43 @@  enum {
     EV_ERROR_DATA,
 };
 
+struct testEventResultData {
+    bool failed;
+    const char *msg;
+};
+
+static int
+testEventResultCallback(const void *opaque)
+{
+    const struct testEventResultData *data = opaque;
+
+    if (data->failed && data->msg) {
+        fprintf(stderr, "%s", data->msg);
+    }
+    return data->failed;
+}
+
+static void
+ATTRIBUTE_FMT_PRINTF(3,4)
+testEventReport(const char *name, bool failed, const char *msg, ...)
+{
+    va_list vargs;
+    va_start(vargs, msg);
+    char *str = NULL;
+    struct testEventResultData data;
+
+    if (msg && virVasprintfQuiet(&str, msg, vargs) != 0) {
+        failed = true;
+    }
+
+    data.failed = failed;
+    data.msg = msg;
+    virtTestRun(name, testEventResultCallback, &data);
+
+    va_end(vargs);
+    VIR_FREE(str);
+}
+
 static void
 testPipeReader(int watch, int fd, int events, void *data)
 {
@@ -148,13 +185,13 @@  verifyFired(const char *name, int handle, int timer)
     for (i = 0; i < NUM_FDS; i++) {
         if (handles[i].fired) {
             if (i != handle) {
-                virtTestResult(name, 1,
+                testEventReport(name, 1,
                                "Handle %zu fired, but expected %d\n", i,
                                handle);
                 return EXIT_FAILURE;
             } else {
                 if (handles[i].error != EV_ERROR_NONE) {
-                    virtTestResult(name, 1,
+                    testEventReport(name, 1,
                                    "Handle %zu fired, but had error %d\n", i,
                                    handles[i].error);
                     return EXIT_FAILURE;
@@ -163,7 +200,7 @@  verifyFired(const char *name, int handle, int timer)
             }
         } else {
             if (i == handle) {
-                virtTestResult(name, 1,
+                testEventReport(name, 1,
                                "Handle %d should have fired, but didn't\n",
                                handle);
                 return EXIT_FAILURE;
@@ -171,7 +208,7 @@  verifyFired(const char *name, int handle, int timer)
         }
     }
     if (handleFired != 1 && handle != -1) {
-        virtTestResult(name, 1,
+        testEventReport(name, 1,
                        "Something weird happened, expecting handle %d\n",
                        handle);
         return EXIT_FAILURE;
@@ -181,12 +218,12 @@  verifyFired(const char *name, int handle, int timer)
     for (i = 0; i < NUM_TIME; i++) {
         if (timers[i].fired) {
             if (i != timer) {
-                virtTestResult(name, 1,
+                testEventReport(name, 1,
                                "Timer %zu fired, but expected %d\n", i, timer);
                 return EXIT_FAILURE;
             } else {
                 if (timers[i].error != EV_ERROR_NONE) {
-                    virtTestResult(name, 1,
+                    testEventReport(name, 1,
                                    "Timer %zu fired, but had error %d\n", i,
                                    timers[i].error);
                     return EXIT_FAILURE;
@@ -195,7 +232,7 @@  verifyFired(const char *name, int handle, int timer)
             }
         } else {
             if (i == timer) {
-                virtTestResult(name, 1,
+                testEventReport(name, 1,
                                "Timer %d should have fired, but didn't\n",
                                timer);
                 return EXIT_FAILURE;
@@ -203,7 +240,7 @@  verifyFired(const char *name, int handle, int timer)
         }
     }
     if (timerFired != 1 && timer != -1) {
-        virtTestResult(name, 1,
+        testEventReport(name, 1,
                        "Something weird happened, expecting timer %d\n",
                        timer);
         return EXIT_FAILURE;
@@ -234,14 +271,14 @@  finishJob(const char *name, int handle, int timer)
         rc = pthread_cond_timedwait(&eventThreadJobCond, &eventThreadMutex,
                                     &waitTime);
     if (rc != 0) {
-        virtTestResult(name, 1, "Timed out waiting for pipe event\n");
+        testEventReport(name, 1, "Timed out waiting for pipe event\n");
         return EXIT_FAILURE;
     }
 
     if (verifyFired(name, handle, timer) != EXIT_SUCCESS)
         return EXIT_FAILURE;
 
-    virtTestResult(name, 0, NULL);
+    testEventReport(name, 0, NULL);
     return EXIT_SUCCESS;
 }