| /******************************************************************************* |
| * Copyright (c) 2009 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.e4.languages.javascript.debug.connect.test; |
| |
| import java.io.IOException; |
| |
| import junit.framework.TestCase; |
| |
| import org.eclipse.e4.languages.javascript.debug.connect.Connection; |
| import org.eclipse.e4.languages.javascript.debug.connect.DebugRuntime; |
| import org.eclipse.e4.languages.javascript.debug.connect.DebugSession; |
| import org.eclipse.e4.languages.javascript.debug.connect.DisconnectException; |
| import org.eclipse.e4.languages.javascript.debug.connect.EventPacket; |
| import org.eclipse.e4.languages.javascript.debug.connect.PipedTransportService; |
| import org.eclipse.e4.languages.javascript.debug.connect.Request; |
| import org.eclipse.e4.languages.javascript.debug.connect.Response; |
| import org.eclipse.e4.languages.javascript.debug.connect.TimeoutException; |
| import org.eclipse.e4.languages.javascript.debug.connect.TransportService; |
| import org.eclipse.e4.languages.javascript.debug.connect.TransportService.ListenerKey; |
| |
| public class DebugSessionTest extends TestCase { |
| |
| public void testReceiveEvent() throws IOException, InterruptedException, TimeoutException, DisconnectException { |
| final TransportService service = new PipedTransportService(); |
| final ListenerKey key = service.startListening("9000"); |
| |
| Thread t = new Thread() { |
| public void run() { |
| Connection c = null; |
| DebugRuntime runtime = null; |
| try { |
| c = service.accept(key, 5000, 5000); |
| assertNotNull(c); |
| |
| runtime = new DebugRuntime(c); |
| runtime.sendEvent(new EventPacket("test")); |
| synchronized (Thread.currentThread()) { |
| Thread.currentThread().wait(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| fail(); |
| } catch (DisconnectException e) { |
| fail(); |
| } catch (InterruptedException e) { |
| fail(); |
| } finally { |
| if (runtime != null) |
| runtime.dispose(); |
| } |
| } |
| }; |
| t.start(); |
| |
| Connection c = service.attach("9000", 5000, 5000); |
| assertNotNull(c); |
| DebugSession session = new DebugSession(c); |
| try { |
| EventPacket event = session.receiveEvent(5000); |
| assertTrue(event.getEvent().equals("test")); |
| } finally { |
| session.dispose(); |
| synchronized (t) { |
| t.notify(); |
| } |
| } |
| t.join(5000); |
| service.stopListening(key); |
| } |
| |
| public void testSendRequestReceiveResponse() throws IOException, InterruptedException, DisconnectException, TimeoutException { |
| final TransportService service = new PipedTransportService(); |
| final ListenerKey key = service.startListening("9000"); |
| |
| Thread t = new Thread() { |
| public void run() { |
| Connection c = null; |
| DebugRuntime runtime = null; |
| try { |
| c = service.accept(key, 5000, 5000); |
| assertNotNull(c); |
| |
| runtime = new DebugRuntime(c); |
| Request request = runtime.receiveRequest(5000); |
| runtime.sendResponse(new Response(request.getSequence(), request.getCommand())); |
| synchronized (Thread.currentThread()) { |
| Thread.currentThread().wait(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| fail(); |
| } catch (DisconnectException e) { |
| fail(); |
| } catch (InterruptedException e) { |
| fail(); |
| } catch (TimeoutException e) { |
| fail(); |
| } finally { |
| if (runtime != null) |
| runtime.dispose(); |
| } |
| } |
| }; |
| t.start(); |
| |
| Connection c = service.attach("9000", 5000, 5000); |
| assertNotNull(c); |
| DebugSession session = new DebugSession(c); |
| try { |
| Request request = new Request("test"); |
| session.sendRequest(request); |
| Response response = session.receiveResponse(request.getSequence(), 5000); |
| assertTrue(response.getCommand().equals("test")); |
| assertTrue(response.getRequestSequence() == request.getSequence()); |
| } finally { |
| session.dispose(); |
| synchronized (t) { |
| t.notify(); |
| } |
| } |
| t.join(5000); |
| service.stopListening(key); |
| } |
| } |