Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing real behavior of node by running sample http server application. #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/test/java/com/eclipsesource/v8/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*
* Contributors:
* EclipseSource - initial API and implementation
******************************************************************************/
* Dukehoff GmbH - node.js server test
******************************************************************************/
package com.eclipsesource.v8;

import org.junit.runner.RunWith;
Expand All @@ -32,7 +33,8 @@
@SuiteClasses({ V8RuntimeNotLoadedTest.class, LibraryLoaderTest.class, V8ObjectTest.class, V8Test.class, V8ArrayTest.class, V8JSFunctionCallTest.class,
V8CallbackTest.class, V8ScriptCompilationExceptionTest.class, V8ScriptExecutionExceptionTest.class, V8ObjectUtilsTest.class, V8TypedArraysTest.class,
NullScriptExecuteTest.class, V8MultiThreadTest.class, V8LockerTest.class, V8ExecutorTest.class, V8MapTest.class, V8PropertyMapTest.class,
DebugHandlerTest.class, ExecutionStateTest.class, FrameTest.class, ScopeTest.class, ScriptBreakPointTest.class, MirrorTest.class, BreakEventTest.class, NodeJSTest.class })
DebugHandlerTest.class, ExecutionStateTest.class, FrameTest.class, ScopeTest.class, ScriptBreakPointTest.class, MirrorTest.class, BreakEventTest.class,
NodeJSTest.class, NodeJSRunServerTest.class })
public class AllTests {

}
112 changes: 112 additions & 0 deletions src/test/java/com/eclipsesource/v8/NodeJSRunServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*******************************************************************************
* Copyright (c) 2016 EclipseSource 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:
* EclipseSource - initial API and implementation
* Dukehoff GmbH - node.js server test
******************************************************************************/

package com.eclipsesource.v8;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;

public class NodeJSRunServerTest {
private NodeJS node;

@Before
public void createNode() {
node = NodeJS.createNodeJS();
}

@After
public void destroyNode() {
node.release();
}

@Test
public void runServerAndConnetToIt() throws IOException, InterruptedException, ExecutionException {
final int port = findEmptyPort();
String code =
"var http = require('http');\n" +
"function handleRequest(request, response){\n" +
" response.end('Connected: ' + request.url);\n" +
"}\n" +
"var p = " + port + ";\n" +
"var server = http.createServer(handleRequest);\n" +
"server.listen(p, function(){\n" +
" console.log(\"Server listening on: http://localhost:%s\", p);\n" +
"});\n" +
"return server";

File serverScript = createScriptFile(code);
V8Object server = node.require(serverScript);
assertNotNull(server);

Future<String> done = Executors.newSingleThreadExecutor().submit(new Callable<String>() {
@Override
public String call() throws Exception {
URL u = new URL("http://127.0.0.1:" + port + "/hello");
final URLConnection conn = u.openConnection();

InputStreamReader r = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(r);
String line = br.readLine();
return line;
}
});

while (!done.isDone()) {
process(node, done);
}

assertEquals("Connected: /hello", done.get());

server.release();
serverScript.delete();
}

private File createScriptFile(String code) throws IOException {
File serverScript = File.createTempFile("temp", ".js");
FileWriter w = new FileWriter(serverScript);
w.write(code);
w.close();
serverScript.deleteOnExit();
return serverScript;
}

private int findEmptyPort() throws IOException {
final ServerSocket ss = new ServerSocket(0);
final int port = ss.getLocalPort();
ss.close();
return port;
}

private void process(NodeJS node, Future<?> await) {
while (node.isRunning()) {
if (await != null && await.isDone()) {
break;
}
node.handleMessage();
}
}
}