Skip to content

Commit

Permalink
Refactoring DevTools bridge tests: moving interface adapters to Signa…
Browse files Browse the repository at this point in the history
…lingReceiverProxy.

Benefits of this refactoring are:
1. Removed a redundant adapter.
2. Removed an unnecessary interface conversion (A->B->A).
3. Removed from ClientSessionTestingHost references to unrelated class LocalSessionBridge.
4. Reduced number of public classes.

BUG=383418

Review URL: https://codereview.chromium.org/700833002

Cr-Commit-Position: refs/heads/master@{#302615}
  • Loading branch information
serya authored and Commit bot committed Nov 4, 2014
1 parent e72c9f4 commit 3508300
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
package org.chromium.components.devtools_bridge;

import java.io.IOException;
import java.util.List;

/**
* Helper class which handles a client session in tests. Having direct reference to
* the server it runs its client session on a dedicated thread and proxies all call
* between them to satisfy theading requirements.
*/
public class ClientSessionTestingHost {
private static final String SESSION_ID = "ID";

private final SignalingReceiver mTarget;
private final SessionBase.Executor mTargetExecutor;
private final LocalSessionBridge.ThreadedExecutor mClientExecutor;
private final String mSessionId;
private int mDelayMs = 10;
private final ClientSession mClientSession;

public ClientSessionTestingHost(
Expand All @@ -30,10 +30,14 @@ public ClientSessionTestingHost(
mClientExecutor = new LocalSessionBridge.ThreadedExecutor();

mSessionId = sessionId;

SignalingReceiverProxy proxy = new SignalingReceiverProxy(
mTargetExecutor, mClientExecutor, target, 0);

mClientSession = new ClientSession(
factory,
mClientExecutor,
new TargetAdaptor().createProxy(),
proxy.asServerSession(SESSION_ID),
clientSocketName);
}

Expand All @@ -54,39 +58,4 @@ public void run() {
}
});
}

/**
* Adapts ServerSessionInterface to DevToolsBridgeServer. Lives on mServerExecutor.
*/
private class TargetAdaptor implements SessionBase.ServerSessionInterface {
/**
* Creates proxy that to safely use on mClientExecutor.
*/
public LocalSessionBridge.ServerSessionProxy createProxy() {
LocalSessionBridge.ServerSessionProxy proxy =
new LocalSessionBridge.ServerSessionProxy(
mTargetExecutor, mClientExecutor, this, mDelayMs);
assert proxy.clientExecutor() == mClientExecutor;
assert proxy.serverExecutor() == mTargetExecutor;
return proxy;
}

@Override
public void startSession(RTCConfiguration config,
String offer,
SessionBase.NegotiationCallback callback) {
mTarget.startSession(mSessionId, config, offer, callback);
}

@Override
public void renegotiate(String offer, SessionBase.NegotiationCallback callback) {
mTarget.renegotiate(mSessionId, offer, callback);
}

@Override
public void iceExchange(List<String> clientCandidates,
SessionBase.IceExchangeCallback callback) {
mTarget.iceExchange(mSessionId, clientCandidates, callback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.util.Log;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -277,76 +276,12 @@ public void cancel() {
}
}

private ServerSessionProxy createServerSessionProxy(SessionBase.ServerSessionInterface proxee) {
return new ServerSessionProxy(mServerExecutor, mClientExecutor, proxee, mDelayMs);
}

/**
* Helper proxy that binds client and server sessions living on different executors.
* Exchange java objects instead of serialized messages.
*/
public static final class ServerSessionProxy implements SessionBase.ServerSessionInterface {
private static final String SESSION_ID = "";
private final SignalingReceiverProxy mProxy;

public ServerSessionProxy(
SessionBase.Executor serverExecutor, SessionBase.Executor clientExecutor,
SessionBase.ServerSessionInterface proxee, int delayMs) {
mProxy = new SignalingReceiverProxy(
serverExecutor, clientExecutor, new ServerSessionAdaptor(proxee), delayMs);
}

public SessionBase.Executor serverExecutor() {
return mProxy.serverExecutor();
}

public SessionBase.Executor clientExecutor() {
return mProxy.clientExecutor();
}

@Override
public void startSession(
RTCConfiguration config, String offer, SessionBase.NegotiationCallback callback) {
mProxy.startSession(SESSION_ID, config, offer, callback);
}

@Override
public void renegotiate(String offer, SessionBase.NegotiationCallback callback) {
mProxy.renegotiate(SESSION_ID, offer, callback);
}
private SessionBase.ServerSessionInterface createServerSessionProxy(
SessionBase.ServerSessionInterface serverSession) {
String sessionId = "";

@Override
public void iceExchange(
List<String> clientCandidates, SessionBase.IceExchangeCallback callback) {
mProxy.iceExchange(SESSION_ID, clientCandidates, callback);
}
}

private static final class ServerSessionAdaptor implements SignalingReceiver {
private final SessionBase.ServerSessionInterface mAdaptee;

public ServerSessionAdaptor(SessionBase.ServerSessionInterface adaptee) {
mAdaptee = adaptee;
}

@Override
public void startSession(
String sessionId, RTCConfiguration config, String offer,
SessionBase.NegotiationCallback callback) {
mAdaptee.startSession(config, offer, callback);
}

@Override
public void renegotiate(
String sessionId, String offer, SessionBase.NegotiationCallback callback) {
mAdaptee.renegotiate(offer, callback);
}

@Override
public void iceExchange(
String sessionId, List<String> clientCandidates,
SessionBase.IceExchangeCallback callback) {
mAdaptee.iceExchange(clientCandidates, callback);
}
return new SignalingReceiverProxy(
mServerExecutor, mClientExecutor, serverSession, sessionId, mDelayMs)
.asServerSession(sessionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.chromium.components.devtools_bridge.commands.CommandReceiver;
import org.chromium.components.devtools_bridge.commands.CommandSender;

import java.util.List;

/**
* Helper proxy that binds client and server sessions living on different executors.
*/
Expand All @@ -28,6 +30,17 @@ public SignalingReceiverProxy(
mDelayMs = delayMs;
}

public SignalingReceiverProxy(
SessionBase.Executor serverExecutor,
SessionBase.Executor clientExecutor,
SessionBase.ServerSessionInterface serverSession,
String sessionId,
int delayMs) {
this(serverExecutor, clientExecutor,
new SignalingReceiverAdaptor(serverSession, sessionId),
delayMs);
}

public SessionBase.Executor serverExecutor() {
return mServerExecutor;
}
Expand All @@ -53,4 +66,72 @@ public void run() {
}
});
}

public SessionBase.ServerSessionInterface asServerSession(String sessionId) {
return new ServerSessionAdapter(this, sessionId);
}

private static final class ServerSessionAdapter implements SessionBase.ServerSessionInterface {
private final SignalingReceiver mAdaptee;
private final String mSessionId;

public ServerSessionAdapter(SignalingReceiver adaptee, String sessionId) {
mAdaptee = adaptee;
mSessionId = sessionId;
}

@Override
public void startSession(
RTCConfiguration config, String offer, SessionBase.NegotiationCallback callback) {
mAdaptee.startSession(mSessionId, config, offer, callback);
}

@Override
public void renegotiate(String offer, SessionBase.NegotiationCallback callback) {
mAdaptee.renegotiate(mSessionId, offer, callback);
}

@Override
public void iceExchange(
List<String> clientCandidates, SessionBase.IceExchangeCallback callback) {
mAdaptee.iceExchange(mSessionId, clientCandidates, callback);
}
}

private static final class SignalingReceiverAdaptor implements SignalingReceiver {
private final SessionBase.ServerSessionInterface mAdaptee;
private final String mSessionId;

public SignalingReceiverAdaptor(
SessionBase.ServerSessionInterface adaptee, String sessionId) {
mAdaptee = adaptee;
mSessionId = sessionId;
}

@Override
public void startSession(
String sessionId, RTCConfiguration config, String offer,
SessionBase.NegotiationCallback callback) {
if (mSessionId.equals(sessionId)) {
mAdaptee.startSession(config, offer, callback);
}
}

@Override
public void renegotiate(
String sessionId, String offer, SessionBase.NegotiationCallback callback) {
if (mSessionId.equals(sessionId)) {
mAdaptee.renegotiate(offer, callback);
}
}

@Override
public void iceExchange(
String sessionId, List<String> clientCandidates,
SessionBase.IceExchangeCallback callback) {
if (mSessionId.equals(sessionId)) {
mAdaptee.iceExchange(clientCandidates, callback);
}
}
}
}

0 comments on commit 3508300

Please sign in to comment.