Adding HubConnection Tests for the Java Client(#2550)
This commit is contained in:
parent
f4bf222099
commit
13a3dfe8dc
|
|
@ -5,18 +5,18 @@ import java.net.URISyntaxException;
|
|||
import java.util.HashMap;
|
||||
|
||||
public class HubConnection {
|
||||
private String _url;
|
||||
private Transport _transport;
|
||||
private String url;
|
||||
private Transport transport;
|
||||
private OnReceiveCallBack callback;
|
||||
private HashMap<String, Action> handlers = new HashMap<>();
|
||||
private HubProtocol protocol;
|
||||
|
||||
public Boolean connected = false;
|
||||
|
||||
public HubConnection(String url) {
|
||||
_url = url;
|
||||
protocol = new JsonHubProtocol();
|
||||
callback = (payload) -> {
|
||||
public HubConnection(String url, Transport transport){
|
||||
this.url = url;
|
||||
this.protocol = new JsonHubProtocol();
|
||||
this.callback = (payload) -> {
|
||||
|
||||
InvocationMessage[] messages = protocol.parseMessages(payload);
|
||||
|
||||
|
|
@ -29,28 +29,36 @@ public class HubConnection {
|
|||
}
|
||||
};
|
||||
|
||||
try {
|
||||
_transport = new WebSocketTransport(_url);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
if (transport == null){
|
||||
try {
|
||||
this.transport = new WebSocketTransport(this.url);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
this.transport = transport;
|
||||
}
|
||||
}
|
||||
|
||||
public HubConnection(String url) {
|
||||
this(url, null);
|
||||
}
|
||||
|
||||
public void start() throws InterruptedException {
|
||||
_transport.setOnReceive(this.callback);
|
||||
_transport.start();
|
||||
transport.setOnReceive(this.callback);
|
||||
transport.start();
|
||||
connected = true;
|
||||
}
|
||||
|
||||
public void stop(){
|
||||
_transport.stop();
|
||||
transport.stop();
|
||||
connected = false;
|
||||
}
|
||||
|
||||
public void send(String method, Object... args) {
|
||||
InvocationMessage invocationMessage = new InvocationMessage(method, args);
|
||||
String message = protocol.writeMessage(invocationMessage);
|
||||
_transport.send(message);
|
||||
transport.send(message);
|
||||
}
|
||||
|
||||
public void On(String target, Action callback) {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,86 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class HubConnectionTest {
|
||||
// Coming soon!
|
||||
@Test
|
||||
public void testEmptyCollection() {
|
||||
public void checkHubConnectionState() throws InterruptedException {
|
||||
Transport mockTransport = new MockEchoTransport();
|
||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||
hubConnection.start();
|
||||
assertTrue(hubConnection.connected);
|
||||
|
||||
hubConnection.stop();
|
||||
assertFalse(hubConnection.connected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SendWithNoParamsTriggersOnHandler() throws InterruptedException {
|
||||
AtomicInteger value = new AtomicInteger(0);
|
||||
Transport mockTransport = new MockEchoTransport();
|
||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||
|
||||
Action callback = (param) -> {
|
||||
assertEquals(0, value.get());
|
||||
value.incrementAndGet();
|
||||
};
|
||||
hubConnection.On("inc", callback);
|
||||
|
||||
hubConnection.start();
|
||||
hubConnection.send("inc");
|
||||
|
||||
// Confirming that our handler was called and that the counter property was incremented.
|
||||
assertEquals(1, value.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SendWithParamTriggersOnHandler() throws InterruptedException {
|
||||
AtomicReference<String> value = new AtomicReference<>();
|
||||
Transport mockTransport = new MockEchoTransport();
|
||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
||||
|
||||
Action callback = (param) -> {
|
||||
assertNull(value.get());
|
||||
value.set(((JsonArray) param).get(0).getAsString());
|
||||
};
|
||||
hubConnection.On("inc", callback);
|
||||
|
||||
hubConnection.start();
|
||||
hubConnection.send("inc", "Hello World");
|
||||
|
||||
// Confirming that our handler was called and the correct message was passed in.
|
||||
assertEquals("Hello World", value.get());
|
||||
}
|
||||
|
||||
private class MockEchoTransport implements Transport {
|
||||
private OnReceiveCallBack onReceiveCallBack;
|
||||
|
||||
@Override
|
||||
public void start() {}
|
||||
|
||||
@Override
|
||||
public void send(String message) {
|
||||
this.onReceive(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOnReceive(OnReceiveCallBack callback) {
|
||||
this.onReceiveCallBack = callback;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(String message) {
|
||||
this.onReceiveCallBack.invoke(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {return;}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue