Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1413,14 +1413,14 @@ public void handleHandshake(ByteBuffer payload) {
handshakeResponse = HandshakeProtocol.parseHandshakeResponse(handshakeResponseString);
} catch (RuntimeException ex) {
RuntimeException exception = new RuntimeException("An invalid handshake response was received from the server.", ex);
handshakeResponseSubject.onError(exception);
errorHandshake(exception);
throw exception;
}
if (handshakeResponse.getHandshakeError() != null) {
String errorMessage = "Error in handshake " + handshakeResponse.getHandshakeError();
logger.error(errorMessage);
RuntimeException exception = new RuntimeException(errorMessage);
handshakeResponseSubject.onError(exception);
errorHandshake(exception);
throw exception;
}
handshakeReceived = true;
Expand All @@ -1431,12 +1431,7 @@ public void handleHandshake(ByteBuffer payload) {
public void timeoutHandshakeResponse(long timeout, TimeUnit unit) {
handshakeTimeout = Executors.newSingleThreadScheduledExecutor();
handshakeTimeout.schedule(() -> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why this uses an executor to implement the timeout instead of using RxJava's timeout operator?
connectionState.transport.send(handshake).timeout(..., ...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably implemented before RxJava was used.

Keeping it as-is so it's easier to backport to 5.0

// If onError is called on a completed subject the global error handler is called
if (!(handshakeResponseSubject.hasComplete() || handshakeResponseSubject.hasThrowable()))
{
handshakeResponseSubject.onError(
new TimeoutException("Timed out waiting for the server to respond to the handshake message."));
}
errorHandshake(new TimeoutException("Timed out waiting for the server to respond to the handshake message."));
}, timeout, unit);
}

Expand Down Expand Up @@ -1476,6 +1471,18 @@ public List<Type> getParameterTypes(String methodName) {

return handlers.get(0).getTypes();
}

private void errorHandshake(Exception error) {
lock.lock();
try {
// If onError is called on a completed subject the global error handler is called
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we could hook the global error handler with our tests and fail if it ever gets called? Would that have been able to catch this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer, adapted something similar for junit 5

if (!(handshakeResponseSubject.hasComplete() || handshakeResponseSubject.hasThrowable())) {
handshakeResponseSubject.onError(error);
}
} finally {
lock.unlock();
}
}
}

// We don't have reconnect yet, but this helps align the Java client with the .NET client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import ch.qos.logback.classic.spi.ILoggingEvent;
import io.reactivex.rxjava3.core.Completable;
Expand All @@ -29,6 +30,7 @@
import io.reactivex.rxjava3.subjects.ReplaySubject;
import io.reactivex.rxjava3.subjects.SingleSubject;

@ExtendWith({RxJavaUnhandledExceptionsExtensions.class})
class HubConnectionTest {
private static final String RECORD_SEPARATOR = "\u001e";
private static final Type booleanType = (new TypeReference<Boolean>() { }).getType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

package com.microsoft.signalr;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.reactivex.rxjava3.plugins.RxJavaPlugins;

// Use by adding "@ExtendWith({RxJavaUnhandledExceptionsExtensions.class})" to a test class
class RxJavaUnhandledExceptionsExtensions implements BeforeAllCallback, AfterAllCallback {
private final BlockingQueue<Throwable> errors = new LinkedBlockingQueue<Throwable>();

@Override
public void beforeAll(final ExtensionContext context) {
RxJavaPlugins.setErrorHandler(error -> {
errors.put(error);
});
}

@Override
public void afterAll(final ExtensionContext context) {
if (errors.size() != 0) {
String RxErrors = "";
for (final Throwable throwable : errors) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
throwable.printStackTrace(printWriter);
RxErrors += String.format("%s\n", stringWriter.toString());
}
throw new RuntimeException(RxErrors);
}
}
}