| 1 | import java.util.Arrays;
|
|---|
| 2 | import java.util.List;
|
|---|
| 3 | import org.virtualbox_5_0.CleanupMode;
|
|---|
| 4 | import org.virtualbox_5_0.IEvent;
|
|---|
| 5 | import org.virtualbox_5_0.IEventListener;
|
|---|
| 6 | import org.virtualbox_5_0.IMachine;
|
|---|
| 7 | import org.virtualbox_5_0.IMedium;
|
|---|
| 8 | import org.virtualbox_5_0.IProgress;
|
|---|
| 9 | import org.virtualbox_5_0.ISession;
|
|---|
| 10 | import org.virtualbox_5_0.IVirtualBox;
|
|---|
| 11 | import org.virtualbox_5_0.SessionState;
|
|---|
| 12 | import org.virtualbox_5_0.VBoxEventType;
|
|---|
| 13 | import org.virtualbox_5_0.VBoxException;
|
|---|
| 14 | import org.virtualbox_5_0.VirtualBoxManager;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Test class for <a href="https://www.virtualbox.org/ticket/13647">Ticket #13647</a>
|
|---|
| 18 | * <p>
|
|---|
| 19 | * Updated to take into account <code>VirtualBoxManager::waitForEvent()</code>
|
|---|
| 20 | * </p>
|
|---|
| 21 | *
|
|---|
| 22 | * @author Maxime Dor
|
|---|
| 23 | */
|
|---|
| 24 | public class OutOfMemory_5_0_RC2 extends Thread {
|
|---|
| 25 |
|
|---|
| 26 | private static long machineId = 0L;
|
|---|
| 27 | private static final int waitingCoef = 500;
|
|---|
| 28 | private static Thread worker;
|
|---|
| 29 | private static VirtualBoxManager vboxManager;
|
|---|
| 30 |
|
|---|
| 31 | public synchronized static String getNextName() {
|
|---|
| 32 | return Long.toString(machineId++);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | public static void main(String[] args) {
|
|---|
| 36 | int nbThreads = Integer.parseInt(System.getProperty("oom.thread",Integer.toString(Runtime.getRuntime().availableProcessors())));
|
|---|
| 37 |
|
|---|
| 38 | Runtime.getRuntime().addShutdownHook(new Thread() {
|
|---|
| 39 |
|
|---|
| 40 | @Override
|
|---|
| 41 | public void run() {
|
|---|
| 42 | System.out.println("Disconnecting");
|
|---|
| 43 | worker.interrupt();
|
|---|
| 44 | try {
|
|---|
| 45 | worker.join();
|
|---|
| 46 |
|
|---|
| 47 | } catch (InterruptedException e) {
|
|---|
| 48 | e.printStackTrace();
|
|---|
| 49 | }
|
|---|
| 50 | vboxManager.cleanup();
|
|---|
| 51 | System.out.println("Disconnected");
|
|---|
| 52 | }
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | System.out.println("Starting");
|
|---|
| 56 | vboxManager = VirtualBoxManager.createInstance(null);
|
|---|
| 57 | if (System.getProperty("vbox.ws") != null) {
|
|---|
| 58 | vboxManager.connect("http://" + System.getProperty("vbox.ws.host", "localhost") + ":" + System.getProperty("vbox.ws.port", "18083"), "", "");
|
|---|
| 59 | }
|
|---|
| 60 | System.out.println("Connected");
|
|---|
| 61 | IVirtualBox vbox = vboxManager.getVBox();
|
|---|
| 62 |
|
|---|
| 63 | worker = new EventWorker(vbox);
|
|---|
| 64 | worker.start();
|
|---|
| 65 |
|
|---|
| 66 | for (int i = 0; i < nbThreads; i++) {
|
|---|
| 67 | (new OutOfMemory_5_0_RC2()).start();
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override
|
|---|
| 73 | public void run() {
|
|---|
| 74 | while (worker.isAlive()) {
|
|---|
| 75 | try {
|
|---|
| 76 |
|
|---|
| 77 | IMachine machine = vboxManager.getVBox().createMachine(null, getNextName(), null, "Other", null);
|
|---|
| 78 | vboxManager.getVBox().registerMachine(machine);
|
|---|
| 79 | System.out.println("Machine registered");
|
|---|
| 80 |
|
|---|
| 81 | ISession session = vboxManager.getSessionObject();
|
|---|
| 82 | IProgress pOn = machine.launchVMProcess(session, "headless", null);
|
|---|
| 83 | while (!pOn.getCompleted() || pOn.getCanceled()) {
|
|---|
| 84 | try {
|
|---|
| 85 | Thread.sleep(Math.abs(pOn.getTimeRemaining()) * waitingCoef);
|
|---|
| 86 | } catch (InterruptedException e) {
|
|---|
| 87 | e.printStackTrace();
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 | if (pOn.getResultCode() != 0) {
|
|---|
| 91 | System.out.println("Error when starting machine: " + pOn.getErrorInfo().getText());
|
|---|
| 92 | } else {
|
|---|
| 93 | System.out.println("Maching started");
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | IProgress pOff = session.getConsole().powerDown();
|
|---|
| 97 | while (!pOff.getCompleted() || pOff.getCanceled()) {
|
|---|
| 98 | try {
|
|---|
| 99 | Thread.sleep(Math.abs(pOn.getTimeRemaining()) * waitingCoef);
|
|---|
| 100 | } catch (InterruptedException e) {
|
|---|
| 101 | e.printStackTrace();
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | if (pOff.getResultCode() != 0) {
|
|---|
| 105 | System.out.println("Error when stopping machine: " + pOff.getErrorInfo().getText());
|
|---|
| 106 | } else {
|
|---|
| 107 | System.out.println("Machine stopped");
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | if (session.getState().equals(SessionState.Locked)) {
|
|---|
| 111 | try {
|
|---|
| 112 | session.unlockMachine();
|
|---|
| 113 | } catch (VBoxException e) {
|
|---|
| 114 | if (!e.getMessage().toUpperCase().contains("8000FFFF")) {
|
|---|
| 115 | e.printStackTrace();
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | machine = vboxManager.getVBox().findMachine(machine.getId());
|
|---|
| 122 | while (!machine.getSessionState().equals(SessionState.Unlocked)) {
|
|---|
| 123 | Thread.sleep(10);
|
|---|
| 124 | }
|
|---|
| 125 | try {
|
|---|
| 126 | List<IMedium> hdds = machine.unregister(CleanupMode.DetachAllReturnHardDisksOnly);
|
|---|
| 127 | IProgress pDel = machine.deleteConfig(hdds);
|
|---|
| 128 | while (!pDel.getCompleted() || pDel.getCanceled()) {
|
|---|
| 129 | try {
|
|---|
| 130 | Thread.sleep(Math.abs(pDel.getTimeRemaining()) * waitingCoef);
|
|---|
| 131 | } catch (InterruptedException e) {
|
|---|
| 132 | e.printStackTrace();
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | if (pDel.getResultCode() != 0) {
|
|---|
| 136 | System.out.println("Error when deleting machine: " + pDel.getErrorInfo().getText());
|
|---|
| 137 | } else {
|
|---|
| 138 | System.out.println("Machine deleted");
|
|---|
| 139 | }
|
|---|
| 140 | } catch (Throwable t) {
|
|---|
| 141 | t.printStackTrace();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | System.out.println("------------------------------");
|
|---|
| 145 | } catch (Throwable t) {
|
|---|
| 146 | t.printStackTrace();
|
|---|
| 147 | worker.interrupt();
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | System.out.println("Stopping");
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | private static class EventWorker extends Thread {
|
|---|
| 156 |
|
|---|
| 157 | private IVirtualBox vbox;
|
|---|
| 158 |
|
|---|
| 159 | public EventWorker(IVirtualBox vbox) {
|
|---|
| 160 | this.vbox = vbox;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | @Override
|
|---|
| 164 | public void run() {
|
|---|
| 165 | System.out.println("Starting worker");
|
|---|
| 166 | IEventListener el = vbox.getEventSource().createListener();
|
|---|
| 167 | vbox.getEventSource().registerListener(el, Arrays.asList(VBoxEventType.Any), false);
|
|---|
| 168 |
|
|---|
| 169 | while (!Thread.currentThread().isInterrupted()) {
|
|---|
| 170 | try {
|
|---|
| 171 | IEvent rawEvent = vbox.getEventSource().getEvent(el, 1000);
|
|---|
| 172 | if (rawEvent != null) {
|
|---|
| 173 | System.out.println("VBox Event: " + rawEvent.getClass().getName() + " - " + rawEvent.getType() + " - " + rawEvent);
|
|---|
| 174 | vbox.getEventSource().eventProcessed(el, rawEvent);
|
|---|
| 175 | }
|
|---|
| 176 | vboxManager.waitForEvents(0);
|
|---|
| 177 | } catch (Throwable t) {
|
|---|
| 178 | t.printStackTrace();
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | vbox.getEventSource().unregisterListener(el);
|
|---|
| 184 | System.out.println("Stopping worker");
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | }
|
|---|