[libvirt-java] [PATCH 46/65] test: add unit test for domain lifecycle events

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Claudio Bley <cbley@xxxxxxxxxx>
---
 src/test/java/org/libvirt/TestJavaBindings.java |   69 +++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/src/test/java/org/libvirt/TestJavaBindings.java b/src/test/java/org/libvirt/TestJavaBindings.java
index 5cc77e7..a73148f 100644
--- a/src/test/java/org/libvirt/TestJavaBindings.java
+++ b/src/test/java/org/libvirt/TestJavaBindings.java
@@ -1,5 +1,10 @@
 package org.libvirt;
 
+import org.libvirt.event.*;
+
+import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.UUID;
 
 import junit.framework.TestCase;
@@ -12,6 +17,15 @@ public final class TestJavaBindings extends TestCase {
 
     private Connect conn;
 
+    static {
+        // do this once for each JVM instance, before connecting
+        try {
+            Library.initEventLoop();
+        } catch (LibvirtException e) {
+            // XXX warn
+        }
+    }
+
     protected void setUp() throws LibvirtException {
         conn = new Connect("test:///default", false);
     }
@@ -237,4 +251,59 @@ public final class TestJavaBindings extends TestCase {
         assertTrue("pool1 should not be active", pool1.isActive() == 0);        
         assertTrue("Domain2 should be active", defaultPool.isActive() == 1);         
     }
+
+    public void testDomainEvents() throws Exception {
+        final List<DomainEventType> events = new ArrayList<DomainEventType>();
+        final Thread t = new Thread() {
+                @Override
+                public void run() {
+                    try {
+                        Library.runEventLoop();
+                    } catch (LibvirtException e) {
+                        fail("LibvirtException was thrown: " + e);
+                    } catch (InterruptedException e) {
+                    }
+                }
+            };
+        t.setDaemon(true);
+        t.start();
+
+        LifecycleListener listener = new LifecycleListener() {
+            @Override
+            public int onLifecycleChange(Domain d, DomainEvent e)
+            {
+                events.add(e.getType());
+
+                return 0;
+            }
+        };
+        try {
+            conn.addLifecycleListener(listener);
+
+            Domain dom = conn.domainDefineXML("<domain type='test' id='2'>" + "  <name>deftest</name>"
+                + "  <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e70</uuid>" + "  <memory>8388608</memory>"
+                + "  <vcpu>2</vcpu>" + "  <os><type arch='i686'>hvm</type></os>" + "  <on_reboot>restart</on_reboot>"
+                + "  <on_poweroff>destroy</on_poweroff>" + "  <on_crash>restart</on_crash>" + "</domain>");
+
+            dom.create();
+            dom.suspend();
+            dom.resume();
+            dom.destroy();
+            dom.undefine();
+
+            // wait until (presumably) all events have been processed
+            Thread.sleep(300);
+
+            assertEquals(Arrays.asList(DomainEventType.DEFINED,
+                                       DomainEventType.STARTED,
+                                       DomainEventType.SUSPENDED,
+                                       DomainEventType.RESUMED,
+                                       DomainEventType.STOPPED,
+                                       DomainEventType.UNDEFINED),
+                         events);
+        } finally {
+            conn.removeLifecycleListener(listener);
+            Library.stopEventLoop();
+        }
+    }
 }
-- 
1.7.9.5

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




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]