Skip to content

Commit

Permalink
Fix of flaky SdchTest#testSdchEnabled test.
Browse files Browse the repository at this point in the history
BUG=657413
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.android:android_cronet_tester

Review-Url: https://codereview.chromium.org/2709003002
Cr-Commit-Position: refs/heads/master@{#451872}
  • Loading branch information
kapishnikov authored and Commit bot committed Feb 22, 2017
1 parent 5d8ee17 commit 5ccaaea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package org.chromium.net;

import android.os.ConditionVariable;
import android.support.test.filters.SmallTest;

import org.json.JSONException;
Expand Down Expand Up @@ -71,7 +70,7 @@ public void testSdchEnabled() throws Exception {
String targetUrl = NativeTestServer.getSdchURL() + "/sdch/test";
long contextAdapter =
getContextAdapter((CronetUrlRequestContext) mTestFramework.mCronetEngine);
DictionaryAddedObserver observer = new DictionaryAddedObserver(targetUrl, contextAdapter);
SdchObserver observer = new SdchObserver(targetUrl, contextAdapter);

// Make a request to /sdch which advertises the dictionary.
TestUrlRequestCallback callback1 = startAndWaitForComplete(mTestFramework.mCronetEngine,
Expand Down Expand Up @@ -101,8 +100,7 @@ public void testSdchEnabled() throws Exception {
null, mTestFramework.getCronetEngineBuilder());
CronetUrlRequestContext newContext = (CronetUrlRequestContext) mTestFramework.mCronetEngine;
long newContextAdapter = getContextAdapter(newContext);
DictionaryAddedObserver newObserver =
new DictionaryAddedObserver(targetUrl, newContextAdapter);
SdchObserver newObserver = new SdchObserver(targetUrl, newContextAdapter);
newObserver.waitForDictionaryAdded();

// Make a request to fetch encoded response at /sdch/test.
Expand Down Expand Up @@ -146,27 +144,6 @@ public void testDictionaryNotFound() throws Exception {
assertEquals("Sdch is not used.\n", callback2.mResponseAsString);
}

private static class DictionaryAddedObserver extends SdchObserver {
private final ConditionVariable mBlock;

public DictionaryAddedObserver(String targetUrl, long contextAdapter) {
super(targetUrl, contextAdapter);
mBlock = new ConditionVariable();
}

@Override
public void onDictionaryAdded() {
mBlock.open();
}

public void waitForDictionaryAdded() {
if (!mDictionaryAlreadyPresent) {
mBlock.block();
mBlock.close();
}
}
}

private long getContextAdapter(CronetUrlRequestContext requestContext) {
return requestContext.getUrlRequestContextAdapter();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

import android.os.ConditionVariable;

import junit.framework.Assert;

import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;

/**
* Class to watch for Sdch dictionary events. The native implementation
* unregisters itself when an event happens. Therefore, an instance of this
* class is only able to receive a notification of the earliest event.
* Currently, implemented events include {@link #onDictionaryAdded}.
*/
@JNINamespace("cronet")
public class SdchObserver {
protected boolean mDictionaryAlreadyPresent = false;
private static final int BLOCK_WAIT_TIMEOUT_SEC = 20;
private final ConditionVariable mAddBlock = new ConditionVariable();

/**
Expand All @@ -27,29 +28,39 @@ public class SdchObserver {
*/
public SdchObserver(String targetUrl, long contextAdapter) {
nativeAddSdchObserver(targetUrl, contextAdapter);
mAddBlock.block();
mAddBlock.close();
}

/**
* Called when a dictionary is added to the SdchManager for the target url.
* Override this method if caller would like to get notified.
*/
@CalledByNative
public void onDictionaryAdded() {
// Left blank;
protected void onDictionaryAdded() {
mAddBlock.open();
}

/**
* Called after the observer has been registered.
*/
@CalledByNative
private void onAddSdchObserverCompleted() {
mAddBlock.open();
protected void onAddSdchObserverCompleted() {
// Left blank;
}

/**
* Called if the dictionary was added before the observer registration.
*/
@CalledByNative
private void onDictionarySetAlreadyPresent() {
mDictionaryAlreadyPresent = true;
protected void onDictionarySetAlreadyPresent() {
mAddBlock.open();
}

public void waitForDictionaryAdded() {
boolean success = mAddBlock.block(BLOCK_WAIT_TIMEOUT_SEC * 1000);
if (!success) {
Assert.fail("Timeout: the dictionary hasn't been added after waiting for "
+ BLOCK_WAIT_TIMEOUT_SEC + " seconds");
}
}

private native void nativeAddSdchObserver(String targetUrl, long contextAdapter);
}

0 comments on commit 5ccaaea

Please sign in to comment.