Compare commits

..
4 Commits
4 changed files with 149 additions and 16 deletions

No files matched your search

+6
View File
@@ -5,7 +5,13 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com/) and this project uses [semantic versioning](http://semver.org/).
## [Unreleased]
## [2.1.3] - 2021-3-16
### Fixed
- Dispatch error callback when socket connect fails.
## [2.1.2] - 2021-3-16
### Fixed
- Properly enable protobuf-javalite for Android builds.
## [2.1.1] - 2021-02-17
@@ -126,14 +126,14 @@ public class WebSocketClient implements SocketClient {
}
private ListenableFuture<Session> createWebsocket(@NonNull final Session session, @NonNull final SocketListener listener, @NonNull final Request request) {
final SettableFuture<Session> future = SettableFuture.create();
final SettableFuture<Session> connectFuture = SettableFuture.create();
final Object lock = this;
socket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(final WebSocket webSocket, final Response response) {
super.onOpen(webSocket, response);
// Notify the deferred caller that the client has connected and is ready to use.
future.set(session);
connectFuture.set(session);
}
@Override
@@ -234,14 +234,14 @@ public class WebSocketClient implements SocketClient {
}
});
} else {
final SettableFuture future = collationIds.remove(collationId);
if (future == null) {
final SettableFuture cidFuture = collationIds.remove(collationId);
if (cidFuture == null) {
log.warn("No matching future for incoming collation ID: " + collationId);
return;
}
if (env.getError() != null) {
future.setException(new DefaultError(collationId, env.getError()));
cidFuture.setException(new DefaultError(collationId, env.getError()));
} else if (env.getRpc() != null) {
final com.heroiclabs.nakama.api.Rpc.Builder apiRpcBuilder = com.heroiclabs.nakama.api.Rpc.newBuilder();
if (env.getRpc().getId() != null) {
@@ -250,19 +250,19 @@ public class WebSocketClient implements SocketClient {
if (env.getRpc().getPayload() != null) {
apiRpcBuilder.setPayload(env.getRpc().getPayload());
}
future.set(apiRpcBuilder.build());
cidFuture.set(apiRpcBuilder.build());
} else if (env.getChannel() != null) {
future.set(env.getChannel());
cidFuture.set(env.getChannel());
} else if (env.getChannelMessageAck() != null) {
future.set(env.getChannelMessageAck());
cidFuture.set(env.getChannelMessageAck());
} else if (env.getMatch() != null) {
future.set(env.getMatch());
cidFuture.set(env.getMatch());
} else if (env.getMatchmakerTicket() != null) {
future.set(env.getMatchmakerTicket());
cidFuture.set(env.getMatchmakerTicket());
} else if (env.getStatus() != null) {
future.set(env.getStatus());
cidFuture.set(env.getStatus());
} else {
future.set(null);
cidFuture.set(null);
}
}
}
@@ -279,8 +279,11 @@ public class WebSocketClient implements SocketClient {
// Graceful socket disconnect is complete, clean up.
synchronized (lock) {
socket = null;
// TODO callback any leftover deferred items with a disconnect error message?
collationIds.clear();
if (!connectFuture.isCancelled() && !connectFuture.isDone()) {
connectFuture.setException(new Throwable("Socket closed."));
}
}
listener.onDisconnect(null);
}
@@ -291,14 +294,17 @@ public class WebSocketClient implements SocketClient {
// Socket has failed and is no longer connected, clean up.
synchronized (lock) {
socket = null;
// TODO callback any leftover deferred items with a disconnect error message?
collationIds.clear();
if (!connectFuture.isCancelled() && !connectFuture.isDone()) {
connectFuture.setException(t);
}
}
listener.onDisconnect(t);
}
});
return future;
return connectFuture;
}
@Override
@@ -0,0 +1,69 @@
/*
* Copyright 2020 The Nakama Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.heroiclabs.nakama;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
class InvalidSession implements Session {
@Override
public String getAuthToken() {
return "badtoken";
}
@Override
public boolean isCreated() {
return true;
}
@Override
public long getCreateTime() {
return new Date().getTime();
}
@Override
public long getExpireTime() {
return new Date().getTime() + 5000L;
}
@Override
public boolean IsExpired() {
return false;
}
@Override
public String getUsername() {
return "invalid_username";
}
@Override
public String getUserId() {
return "invalid_user";
}
@Override
public boolean isExpired(Date dateTime) {
return false;
}
@Override
public Map<String, String> getVars() {
return new Hashtable<String, String>();
}
}
@@ -22,6 +22,12 @@ import org.junit.Before;
import org.junit.Test;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
public class SocketTest {
private Session session;
@@ -41,7 +47,6 @@ public class SocketTest {
@Test(expected = Error.class)
public void sendNotConnectedUnhandled() throws Throwable {
// socket.connect(session, new AbstractSocketListener() {}).get();
try {
socket.createMatch().get();
} catch (Exception e) {
@@ -60,4 +65,51 @@ public class SocketTest {
e.printStackTrace();
}
}
class TestSocketConnectCallback implements FutureCallback<Session> {
public boolean failureDispatched = false;
public boolean successDispatched = false;
@Override
public void onSuccess(@org.jetbrains.annotations.Nullable Session result) {
this.successDispatched = true;
}
@Override
public void onFailure(Throwable t) {
this.failureDispatched = true;
}
}
@Test
public void sendConnectedFailureHandled() {
final ExecutorService executor = Executors.newSingleThreadExecutor();
final TestSocketConnectCallback connectCallback = new TestSocketConnectCallback();
Futures.addCallback(socket.connect(new InvalidSession(), new AbstractSocketListener(){}), connectCallback, executor);
try {
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Assert.fail("Current thread interrupted.");
}
Assert.assertTrue("Failure callback was not dispatched.", connectCallback.failureDispatched);
}
@Test
public void sendConnectedSuccessHandled() {
final ExecutorService executor = Executors.newSingleThreadExecutor();
final TestSocketConnectCallback connectCallback = new TestSocketConnectCallback();
Futures.addCallback(socket.connect(session, new AbstractSocketListener(){}), connectCallback, executor);
try {
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Assert.fail("Current thread interrupted.");
}
Assert.assertTrue("Success callback was not dispatched.", connectCallback.successDispatched);
}
}