Skip to contentSkip to conversation

tests/qtest/virtio-net-failover: Use g_random_int() instead of g_test_rand_int()

PATCH In review Thomas Huth 4 participants 8 messages Dec 20, 2021 – Dec 22, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/qtest/virtio-net-failover: Use g_random_int() instead of g_test_rand_int()

g_test_rand_int() must not be called before g_test_init(), otherwise the glib will show a "g_rand_int: assertion 'rand != NULL' failed" message in the log. So we could change the order here, but actually, g_test_rand_int() seems also the wrong choice here, since we're using it to create a temporary file name, which certainly should not depend on the "--seed" CLI option of the g_test suite. Thus let's use the g_random_int() function instead.

---
 tests/qtest/virtio-net-failover.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/virtio-net-failover.c +1 -1
index 4b2ba8a106..70b94bf18f 100644
@@ -1308,7 +1308,7 @@ int main(int argc, char **argv)
 {
     const gchar *tmpdir = g_get_tmp_dir();
     gchar *tmpfile = g_strdup_printf("%s/failover_test_migrate-%u-%u",
-                                     tmpdir, getpid(), g_test_rand_int());
+                                     tmpdir, getpid(), g_random_int());
     int ret;
 
     g_test_init(&argc, &argv, NULL);
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Thomas Huth

g_test_rand_int() must not be called before g_test_init(), otherwise the glib will show a "g_rand_int: assertion 'rand != NULL' failed" message in the log. So we could change the order here, but actually, g_test_rand_int() seems also the wrong choice here, since we're using it to create a temporary file name, which certainly should not depend on the "--seed" CLI option of the g_test suite. Thus let's use the g_random_int() function instead.

---
  tests/qtest/virtio-net-failover.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/virtio-net-failover.c +0 -0
index 4b2ba8a106..70b94bf18f 100644

tests/qtest/virtio-net-failover.c int main(int argc, char **argv) · line 1314

Quoted 8 lines
tests/qtest/virtio-net-failover.c +1 -1
@@ -1308,7 +1308,7 @@ int main(int argc, char **argv)
  {
      const gchar *tmpdir = g_get_tmp_dir();
      gchar *tmpfile = g_strdup_printf("%s/failover_test_migrate-%u-%u",
-                                     tmpdir, getpid(), g_test_rand_int());
+                                     tmpdir, getpid(), g_random_int());
      int ret;
 
      g_test_init(&argc, &argv, NULL);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Thomas Huth
Quoted 8 lines · Thomas Huth

g_test_rand_int() must not be called before g_test_init(), otherwise the glib will show a "g_rand_int: assertion 'rand != NULL' failed" message in the log. So we could change the order here, but actually, g_test_rand_int() seems also the wrong choice here, since we're using it to create a temporary file name, which certainly should not depend on the "--seed" CLI option of the g_test suite. Thus let's use the g_random_int() function instead.

Quoted 5 lines
---
 tests/qtest/virtio-net-failover.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw
In reply to Thomas Huth

tests/qtest/virtio-net-failover.c int main(int argc, char **argv) · line 1311

Quoted 4 lines · Thomas Huth

const gchar *tmpdir = g_get_tmp_dir();
gchar *tmpfile = g_strdup_printf("%s/failover_test_migrate-%u-%u",

tests/qtest/virtio-net-failover.c +1 -1
-                                     tmpdir, getpid(), g_test_rand_int());
+                                     tmpdir, getpid(), g_random_int());

Random numbers plus pid are irrelevant, because you still don't have guaranteed uniqueness -- think stale files in /tmp.

Use g_file_open_tmp.

r~

#4 lore raw
Quoted 11 lines · Richard Henderson
Quoted 5 lines · Thomas Huth

      const gchar *tmpdir = g_get_tmp_dir();
      gchar *tmpfile = g_strdup_printf("%s/failover_test_migrate-%u-%u", -                                     tmpdir, getpid(), g_test_rand_int());
+                                     tmpdir, getpid(), g_random_int());

Random numbers plus pid are irrelevant, because you still don't have guaranteed uniqueness -- think stale files in /tmp.

Use g_file_open_tmp.

Another use in test_socket_unix_abstract(),
tests/unit/test-util-sockets.c.

#5 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Richard Henderson
Quoted 5 lines · Thomas Huth

      const gchar *tmpdir = g_get_tmp_dir();
      gchar *tmpfile = g_strdup_printf("%s/failover_test_migrate-%u-%u",
-                                     tmpdir, getpid(), g_test_rand_int());
+                                     tmpdir, getpid(), g_random_int());

Quoted 7 lines
Quoted 4 lines

Random numbers plus pid are irrelevant, because you still don't have guaranteed uniqueness -- think stale files in /tmp.

Use g_file_open_tmp.

Another use in test_socket_unix_abstract(),
tests/unit/test-util-sockets.c.

Using g_file_open_tmp is certainly better ... but the tests are currently written in a way where they require the file name of the temporary file - so switching to g_file_open_tmp() (which only provides a file handle) certainly would need some rewrite here... Thus I'd suggest to go first with this patch to silence the Assert messages, and then to clean this up properly later.

Thomas

#6 lore raw
Quoted 1 line · Thomas Huth

Using g_file_open_tmp is certainly better ... but the tests are currently written in a way where they require the file name of the temporary file - so switching to g_file_open_tmp() (which only provides a file handle) certainly would need some rewrite here...

Incorrect. g_file_open_tmp returns the open file handle, but also returns the filename in *name_used.

I should think you can close the file handle straight away and use the filename.

r~

#7 lore raw
Quoted 4 lines · Richard Henderson
Quoted 1 line · Thomas Huth

Using g_file_open_tmp is certainly better ... but the tests are currently written in a way where they require the file name of the temporary file - so switching to g_file_open_tmp() (which only provides a file handle) certainly would need some rewrite here...

Incorrect.  g_file_open_tmp returns the open file handle, but also returns the filename in *name_used.

D'oh - not sure how I missed that yesterday :-/

Quoted 1 line

I should think you can close the file handle straight away and use the filename.

Ok, then let's continue with that approach.

Thomas