Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some flaky tests #406

Merged
merged 25 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
966b27c
Increase timeout interval for RealtimeReauthTest
funkyboy Jun 25, 2018
6273380
Remove unneeded wait for close
funkyboy Jun 25, 2018
0611943
Randomize channel name to avoid clash in channelhistory_types test wi…
funkyboy Jun 25, 2018
d9aa7ab
Randomize channel names for channelhistory_time_f test
funkyboy Jun 25, 2018
47c5250
Temporary ignore channelhistory_time_f test
funkyboy Jun 26, 2018
d4448c7
Tweak values for Rest time tests
funkyboy Jun 26, 2018
65a535c
Increase timeout for ConnectionManager tests
funkyboy Jun 26, 2018
a8d153c
Fix typo in assert of reauth_fail test
funkyboy Jun 26, 2018
8059dce
Helper now prints the name of the state/event
funkyboy Jun 27, 2018
e6a8225
Add protocol name to channels to avoid possible clashes
funkyboy Jun 28, 2018
ecc3203
Add catch statement to trap errors in sending presence messages durin…
funkyboy Jun 28, 2018
219ffb6
Collect only enter and update actions in realtime_presence_map_test
funkyboy Jun 28, 2018
50f9777
Explicitly fail if there's an exception in publishing history messages
funkyboy Jun 29, 2018
d6fe429
Fix some formatting
funkyboy Jun 29, 2018
5d43515
Ignore channelhistory_from_attach due to issues in sandbox
funkyboy Jul 2, 2018
f100723
Increase timeout of http_ably_execute_fallback
funkyboy Jul 2, 2018
364f759
Increase timeout of RealtimeCryptoTest
funkyboy Jul 2, 2018
052bd40
Increase timeout for RealtimeConnectTest
funkyboy Jul 2, 2018
374da68
Increase timeout for ConnectionManagerTest
funkyboy Jul 2, 2018
271d2ec
Add protocol name to channels to avoid text vs binary clashes
funkyboy Jul 2, 2018
48d5888
Explicitly fail presencehistory_from_attach is publisher thread throw…
funkyboy Jul 9, 2018
6229289
Temporarily ignore presencehistory_from_attach test
funkyboy Jul 9, 2018
59f7094
Call presence methods before setting the state of a channel
funkyboy Jul 11, 2018
35fa1c7
Add test name to channels to avoid clashing
funkyboy Jul 11, 2018
5b1332e
Explicitly wait for failed state in enter/leave presence tests
funkyboy Jul 11, 2018
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
6 changes: 3 additions & 3 deletions lib/src/main/java/io/ably/lib/realtime/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,17 @@ private void setAttached(ProtocolMessage message) {
private void setDetached(ErrorInfo reason) {
clearAttachTimers();
Log.v(TAG, "setDetached(); channel = " + name);
presence.setDetached(reason);
setState(ChannelState.detached, reason);
failQueuedMessages(reason);
presence.setDetached(reason);
}

private void setFailed(ErrorInfo reason) {
clearAttachTimers();
Log.v(TAG, "setFailed(); channel = " + name);
presence.setDetached(reason);
setState(ChannelState.failed, reason);
failQueuedMessages(reason);
presence.setDetached(reason);
}

/* Timer for attach operation */
Expand Down Expand Up @@ -464,9 +464,9 @@ public synchronized void setSuspended(ErrorInfo reason, boolean notifyStateChang
clearAttachTimers();
if (state == ChannelState.attached || state == ChannelState.attaching) {
Log.v(TAG, "setSuspended(); channel = " + name);
presence.setSuspended(reason);
setState(ChannelState.suspended, reason, false, notifyStateChange);
failQueuedMessages(reason);
presence.setSuspended(reason);
}
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/test/java/io/ably/lib/test/common/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ public ConnectionWaiter(Connection connection) {
* @return error info
*/
public synchronized ErrorInfo waitFor(ConnectionState state) {
Log.d(TAG, "waitFor(state=" + state + ")");
Log.d(TAG, "waitFor(state=" + state.getConnectionEvent().name() + ")");
while(connection.state != state)
try { wait(); } catch(InterruptedException e) {}
Log.d(TAG, "waitFor done: state=" + connection.state + ")");
Log.d(TAG, "waitFor done: state=" + connection.state.getConnectionEvent().name() + ")");
return reason;
}

Expand All @@ -314,11 +314,11 @@ public synchronized ErrorInfo waitFor(ConnectionState state) {
* @param count
*/
public synchronized void waitFor(ConnectionState state, int count) {
Log.d(TAG, "waitFor(state=" + state + ", count=" + count + ")");
Log.d(TAG, "waitFor(state=" + state.getConnectionEvent().name() + ", count=" + count + ")");

while(getStateCount(state) < count)
try { wait(); } catch(InterruptedException e) {}
Log.d(TAG, "waitFor done: state=" + connection.state + ", count=" + getStateCount(state) + ")");
Log.d(TAG, "waitFor done: state=" + connection.state.getConnectionEvent().name() + ", count=" + getStateCount(state) + ")");
}

/**
Expand All @@ -330,15 +330,15 @@ public synchronized void waitFor(ConnectionState state, int count) {
* @return true if state was reached
*/
public synchronized boolean waitFor(ConnectionState state, int count, long time) {
Log.d(TAG, "waitFor(state=" + state + ", count=" + count + ", time=" + time + ")");
Log.d(TAG, "waitFor(state=" + state.getConnectionEvent().name() + ", count=" + count + ", time=" + time + ")");
long targetTime = System.currentTimeMillis() + time;
long remaining = time;
while(getStateCount(state) < count && remaining > 0) {
try { wait(remaining); } catch(InterruptedException e) {}
remaining = targetTime - System.currentTimeMillis();
}
int stateCount = getStateCount(state);
Log.d(TAG, "waitFor done: state=" + connection.state +
Log.d(TAG, "waitFor done: state=" + connection.state.getConnectionEvent().name() +
", count=" + Integer.toString(stateCount)+ ")");
return stateCount >= count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
import org.mockito.Mockito;

import io.ably.lib.realtime.AblyRealtime;
Expand All @@ -45,6 +47,10 @@
* Created by gokhanbarisaker on 3/9/16.
*/
public class ConnectionManagerTest extends ParameterizedTest {

@Rule
public Timeout testTimeout = Timeout.seconds(60);

/**
* <p>
* Verifies that ably connects to default host,
Expand Down Expand Up @@ -602,15 +608,15 @@ public void onConnectionStateChanged(ConnectionStateChange state) {
final String firstConnectionId = ably.connection.id;

/* Prepare channels */
final Channel attachedChannel = ably.channels.get("test-reattach-after-ttl");
final Channel attachedChannel = ably.channels.get("test-reattach-after-ttl" + testParams.name);
ChannelWaiter attachedChannelWaiter = new Helpers.ChannelWaiter(attachedChannel);
attachedChannel.on(new ChannelStateListener() {
@Override
public void onChannelStateChanged(ChannelStateChange stateChange) {
attachedChannelHistory.add(stateChange.current.name());
}
});
final Channel suspendedChannel = ably.channels.get("test-reattach-suspended-after-ttl");
final Channel suspendedChannel = ably.channels.get("test-reattach-suspended-after-ttl" + testParams.name);
suspendedChannel.state = ChannelState.suspended;
ChannelWaiter suspendedChannelWaiter = new Helpers.ChannelWaiter(suspendedChannel);
suspendedChannel.on(new ChannelStateListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import java.util.HashMap;
import java.util.Locale;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.*;
import org.junit.rules.Timeout;

import io.ably.lib.realtime.AblyRealtime;
Expand Down Expand Up @@ -277,25 +274,25 @@ public void channelhistory_second_channel() {
ClientOptions rxOpts = createOptions(testVars.keys[0].keyStr);
rxAbly = new AblyRealtime(rxOpts);
String channelName = "persisted:channelhistory_second_channel_" + testParams.name;

/* create a channel */
final Channel txChannel = txAbly.channels.get(channelName);
final Channel rxChannel = rxAbly.channels.get(channelName);

/* attach sender */
txChannel.attach();
(new ChannelWaiter(txChannel)).waitFor(ChannelState.attached);
assertEquals("Verify attached state reached", txChannel.state, ChannelState.attached);

/* publish to the channel */
String messageText = "Test message (channelhistory_second_channel)";
CompletionWaiter msgComplete = new CompletionWaiter();
txChannel.publish("test_event", messageText, msgComplete);

/* wait for the publish callback to be called */
msgComplete.waitFor();
assertTrue("Verify success callback was called", msgComplete.success);

/* attach receiver */
rxChannel.attach();
(new ChannelWaiter(rxChannel)).waitFor(ChannelState.attached);
Expand All @@ -308,7 +305,7 @@ public void channelhistory_second_channel() {

/* verify message contents */
assertEquals("Expect correct message text", messages.items()[0].data, messageText);

} catch (AblyException e) {
e.printStackTrace();
fail("init0: Unexpected exception instantiating library");
Expand Down Expand Up @@ -671,15 +668,15 @@ public void channelhistory_time_f() {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
ably = new AblyRealtime(opts);
String channelName = "persisted:channelhistory_time_f_" + testParams.name;

/* create a channel */
final Channel channel = ably.channels.get(channelName);

/* attach */
channel.attach();
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
assertEquals("Verify attached state reached", channel.state, ChannelState.attached);

/* send batches of messages with shprt inter-message delay */
CompletionSet msgComplete = new CompletionSet();
for(int i = 0; i < 20; i++) {
Expand Down Expand Up @@ -742,15 +739,15 @@ public void channelhistory_time_b() {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
ably = new AblyRealtime(opts);
String channelName = "persisted:channelhistory_time_b_" + testParams.name;

/* create a channel */
final Channel channel = ably.channels.get(channelName);

/* attach */
channel.attach();
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
assertEquals("Verify attached state reached", channel.state, ChannelState.attached);

/* send batches of messages with shprt inter-message delay */
CompletionSet msgComplete = new CompletionSet();
for(int i = 0; i < 20; i++) {
Expand Down Expand Up @@ -1160,6 +1157,7 @@ public void channelhistory_paginate_first_b() {
* history up to the point of attachment can be obtained.
*/
@Test
@Ignore("Fails due to issues in sandbox. See https://github.com/ably/realtime/issues/1834 for details.")
public void channelhistory_from_attach() {
AblyRealtime txAbly = null, rxAbly = null;
try {
Expand Down Expand Up @@ -1218,7 +1216,9 @@ public void run() {
/* wait for the publisher thread to complete */
try {
publisherThread.join();
} catch (InterruptedException e) {}
} catch (InterruptedException e) {
fail("channelhistory_from_attach: exception in publisher thread");
}

/* get the history for this channel */
PaginatedResult<Message> messages = rxChannel.history(new Param[] { new Param("from_serial", rxChannel.properties.attachSerial)});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import io.ably.lib.types.ClientOptions;
import io.ably.lib.types.Param;
import io.ably.lib.types.ProtocolMessage;
import org.junit.rules.Timeout;

public class RealtimeConnectTest extends ParameterizedTest {

public Timeout testTimeout = Timeout.seconds(30);

/**
* Perform a simple connect to the service and confirm that the connected state is reached.
* Also confirm that we did not get token authorization, as we did not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

public class RealtimeCryptoTest extends ParameterizedTest {

@Rule
public Timeout testTimeout = Timeout.seconds(30);

/**
* Connect to the service
* and publish an encrypted message on that channel using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ public void onConnectionStateChanged(ConnectionStateChange stateChange) {
ablyRealtime.close();
}
});
connectionWaiter.waitFor(ConnectionState.disconnected);
connectionWaiter.waitFor(ConnectionState.closed);
} catch (AblyException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;
Expand Down Expand Up @@ -1035,6 +1036,7 @@ public void presencehistory_paginate_first_b() {
* history up to the point of attachment can be obtained.
*/
@Test
@Ignore("Fails due to issues in sandbox. See https://github.com/ably/realtime/issues/1845 for details.")
public void presencehistory_from_attach() {
AblyRealtime txAbly = null, rxAbly = null;
try {
Expand Down Expand Up @@ -1084,7 +1086,9 @@ public void run() {
/* wait 2 seconds */
try {
Thread.sleep(2000L);
} catch(InterruptedException ie) {}
} catch(InterruptedException ie) {
fail("presencehistory_from_attach: exception in publisher thread");
}

/* subscribe; this will trigger the attach */
PresenceWaiter presenceWaiter = new PresenceWaiter(rxChannel);
Expand Down
Loading