Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.pusher.client.connection.impl;

import com.pusher.client.channel.PusherEvent;

/**
* Receives events from the connection.
* <p>
* Used instead of java.util.function.Consumer, which isn't available before Android 7 (API 24).
*/
public interface PusherEventHandler {
void handle(PusherEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;
import com.pusher.client.connection.impl.InternalConnection;
import com.pusher.client.connection.impl.PusherEventHandler;
import com.pusher.client.util.Factory;

import org.java_websocket.handshake.ServerHandshake;
Expand All @@ -20,7 +21,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Logger;

import javax.net.ssl.SSLException;
Expand All @@ -42,7 +42,7 @@ public class WebSocketConnection implements InternalConnection, WebSocketListene

private volatile ConnectionState state = ConnectionState.DISCONNECTED;
private WebSocketClientWrapper underlyingConnection;
private final Consumer<PusherEvent> eventHandler;
private final PusherEventHandler eventHandler;
private String socketId;
private int reconnectAttempts = 0;
private volatile Future<?> reconnectTimer;
Expand All @@ -54,7 +54,7 @@ public WebSocketConnection(
int maxReconnectionAttempts,
int maxReconnectionGap,
final Proxy proxy,
final Consumer<PusherEvent> eventHandler,
final PusherEventHandler eventHandler,
final Factory factory
) throws URISyntaxException {
webSocketUri = new URI(url);
Expand Down Expand Up @@ -167,7 +167,7 @@ private void handleEvent(final PusherEvent event) {
} else if (event.getEventName().equals("pusher:error")) {
handleError(event);
}
eventHandler.accept(event);
eventHandler.handle(event);
}

@SuppressWarnings("rawtypes")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/pusher/client/util/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.pusher.client.channel.impl.PrivateChannelImpl;
import com.pusher.client.channel.impl.PrivateEncryptedChannelImpl;
import com.pusher.client.connection.impl.InternalConnection;
import com.pusher.client.connection.impl.PusherEventHandler;
import com.pusher.client.connection.websocket.WebSocketClientWrapper;
import com.pusher.client.connection.websocket.WebSocketConnection;
import com.pusher.client.connection.websocket.WebSocketListener;
Expand All @@ -23,7 +24,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer;

import javax.net.ssl.SSLException;

Expand Down Expand Up @@ -55,7 +55,7 @@ public class Factory {
public synchronized InternalConnection getConnection(
final String apiKey,
final PusherOptions options,
final Consumer<PusherEvent> eventHandler
final PusherEventHandler eventHandler
) {
if (connection == null) {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/pusher/client/EndToEndTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.pusher.client.connection.impl.PusherEventHandler;
import com.pusher.client.channel.PusherEvent;
import com.pusher.client.channel.impl.ChannelManager;
import com.pusher.client.connection.ConnectionEventListener;
Expand All @@ -33,7 +34,6 @@

import java.net.Proxy;
import java.net.URI;
import java.util.function.Consumer;

@RunWith(MockitoJUnitRunner.class)
public class EndToEndTest {
Expand All @@ -57,7 +57,7 @@ public class EndToEndTest {
private ChannelAuthorizer mockChannelAuthorizer;

@Mock
private Consumer<PusherEvent> mockEventHandler;
private PusherEventHandler mockEventHandler;

@Mock
private ConnectionEventListener mockConnectionEventListener;
Expand Down Expand Up @@ -117,7 +117,7 @@ public WebSocketClientWrapper answer(final InvocationOnMock invocation) throws T
}
);

when(factory.getConnection(eq(API_KEY), eq(pusherOptions), any(Consumer.class))).thenReturn(connection);
when(factory.getConnection(eq(API_KEY), eq(pusherOptions), any(PusherEventHandler.class))).thenReturn(connection);

when(factory.getChannelManager())
.thenAnswer(
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/pusher/client/PusherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.pusher.client.connection.impl.PusherEventHandler;
import com.pusher.client.channel.ChannelEventListener;
import com.pusher.client.channel.PresenceChannelEventListener;
import com.pusher.client.channel.PrivateChannelEventListener;
Expand All @@ -33,7 +34,6 @@
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.function.Consumer;

@RunWith(MockitoJUnitRunner.class)
public class PusherTest {
Expand Down Expand Up @@ -88,7 +88,7 @@ public void setUp() {
.setChannelAuthorizer(channelAuthorizer)
.setUserAuthenticator(new HttpUserAuthenticator("http://user-auth.com"));

when(factory.getConnection(eq(API_KEY), any(PusherOptions.class), any(Consumer.class)))
when(factory.getConnection(eq(API_KEY), any(PusherOptions.class), any(PusherEventHandler.class)))
.thenReturn(mockConnection);
when(factory.getChannelManager()).thenReturn(mockChannelManager);
when(factory.newPublicChannel(PUBLIC_CHANNEL_NAME)).thenReturn(mockPublicChannel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.pusher.client.connection.impl.PusherEventHandler;
import com.pusher.client.channel.PusherEvent;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
Expand All @@ -39,7 +40,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import javax.net.ssl.SSLException;

Expand All @@ -62,7 +62,7 @@ public class WebSocketConnectionTest {
private WebSocketClientWrapper mockUnderlyingConnection;

@Mock
private Consumer<PusherEvent> mockEventHandler;
private PusherEventHandler mockEventHandler;

@Mock
private ConnectionEventListener mockEventListener;
Expand Down Expand Up @@ -271,7 +271,7 @@ public void testReceiveUserMessagePassesMessageToEventHandler() {
connect();

connection.onMessage(INCOMING_MESSAGE);
// verify(mockEventHandler).accept(EVENT_NAME, INCOMING_MESSAGE);
// verify(mockEventHandler).handle(EVENT_NAME, INCOMING_MESSAGE);
}

@Test
Expand Down
Loading