-
Notifications
You must be signed in to change notification settings - Fork 182
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 incorrect test parallelWritesWithoutTransaction. #829
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
import com.pushtorefresh.storio2.sqlite.BuildConfig; | ||
import com.pushtorefresh.storio2.sqlite.Changes; | ||
import com.pushtorefresh.storio2.sqlite.StorIOSQLite; | ||
import com.pushtorefresh.storio2.test.Repeat; | ||
import com.pushtorefresh.storio2.test.RepeatRule; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
@@ -28,6 +31,9 @@ | |
@Config(constants = BuildConfig.class, sdk = 21) | ||
public class NotifyAboutChangesTest extends BaseTest { | ||
|
||
@Rule | ||
public RepeatRule repeat = new RepeatRule(); | ||
|
||
@Test | ||
public void notifyAboutChange() { | ||
TestSubscriber<Changes> testObserver = new TestSubscriber<Changes>(); | ||
|
@@ -197,11 +203,12 @@ public void run() { | |
} | ||
|
||
@Test | ||
@Repeat(times = 20) | ||
public void shouldReceiveOneNotificationWithAllAffectedTablesInTransactionWithMultipleThreads() throws InterruptedException { | ||
final String table1 = "test_table1"; | ||
final String table2 = "test_table2"; | ||
|
||
final int numberOfThreads = 100; | ||
final int numberOfThreads = 50; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100 might be a problem for Travis since it has limited amount of resources |
||
|
||
final TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>(); | ||
|
||
|
@@ -239,14 +246,15 @@ public void run() { | |
// Steady! | ||
startAllThreadsLock.countDown(); // Go! | ||
|
||
assertThat(allThreadsFinishedLock.await(20, SECONDS)).isTrue(); | ||
assertThat(allThreadsFinishedLock.await(25, SECONDS)).isTrue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't really matter, but since 30 seconds is now global limit for test duration, increasing this closer to 30 kinda makes sense :) |
||
|
||
// While we in transaction, no changes should be sent. | ||
assertThat(testSubscriber.getOnNextEvents()).hasSize(0); | ||
|
||
lowLevel.endTransaction(); | ||
|
||
testSubscriber.assertNoErrors(); | ||
|
||
List<Changes> actualChanges = testSubscriber.getOnNextEvents(); | ||
assertThat(actualChanges).hasSize(1); | ||
assertThat(actualChanges.get(0).affectedTables()).containsOnly("test_table1", "test_table2"); | ||
|
@@ -298,4 +306,4 @@ public void run() { | |
testSubscriber.assertNoErrors(); | ||
testSubscriber.assertNoValues(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,10 @@ | |
import com.pushtorefresh.storio2.sqlite.Changes; | ||
import com.pushtorefresh.storio2.sqlite.queries.Query; | ||
import com.pushtorefresh.storio2.test.AbstractEmissionChecker; | ||
import com.pushtorefresh.storio2.test.Repeat; | ||
import com.pushtorefresh.storio2.test.RepeatRule; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
@@ -55,6 +58,9 @@ public void call(List<User> users) { | |
} | ||
} | ||
|
||
@Rule | ||
public RepeatRule repeat = new RepeatRule(); | ||
|
||
@Test | ||
public void insertEmission() { | ||
final List<User> initialUsers = putUsersBlocking(10); | ||
|
@@ -151,19 +157,20 @@ public void deleteEmission() { | |
} | ||
|
||
@Test | ||
public void parallelWritesWithoutTransaction() { | ||
final int numberOfParallelWorkers = 50; | ||
@Repeat(times = 20) | ||
public void parallelPutWithoutGlobalTransaction() { | ||
final int numberOfParallelPuts = 50; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just rename :) |
||
|
||
TestSubscriber<Changes> testSubscriber = new TestSubscriber<Changes>(); | ||
|
||
storIOSQLite | ||
.observeChangesInTable(TweetTableMeta.TABLE) | ||
.take(numberOfParallelWorkers) | ||
.take(numberOfParallelPuts) | ||
.subscribe(testSubscriber); | ||
|
||
final CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
for (int i = 0; i < numberOfParallelWorkers; i++) { | ||
for (int i = 0; i < numberOfParallelPuts; i++) { | ||
final int copyOfCurrentI = i; | ||
new Thread(new Runnable() { | ||
@Override | ||
|
@@ -188,7 +195,15 @@ public void run() { | |
|
||
testSubscriber.awaitTerminalEvent(); | ||
testSubscriber.assertNoErrors(); | ||
assertThat(testSubscriber.getOnNextEvents()).hasSize(numberOfParallelWorkers); | ||
|
||
// Put operation creates short-term transaction which might result in merge of some notifications. | ||
// So we have two extreme cases: | ||
// - no merged notifications → isEqualTo(numberOfParallelPuts) | ||
// - all notifications merged → isEqualTo(1) | ||
// Obviously truth is somewhere between those (depends on CPU of machine that runs test). | ||
assertThat(testSubscriber.getOnNextEvents().size()) | ||
.isLessThanOrEqualTo(numberOfParallelPuts) | ||
.isGreaterThanOrEqualTo(1); | ||
} | ||
|
||
@Test | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.pushtorefresh.storio2.test; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Retention(RUNTIME) | ||
@Target(METHOD) | ||
public @interface Repeat { | ||
int times(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.pushtorefresh.storio2.test; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
public class RepeatRule implements TestRule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
|
||
@Override | ||
@NonNull | ||
public Statement apply(@NonNull final Statement test, @NonNull final Description description) { | ||
final Repeat repeat = description.getAnnotation(Repeat.class); | ||
|
||
if (repeat != null) { | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
final int times = repeat.times(); | ||
|
||
if (times < 1) { | ||
throw new IllegalArgumentException("Repeat times should be >= 1, times = " + times); | ||
} | ||
|
||
for (int i = 0; i < times; i++) { | ||
System.out.println(description.getMethodName() + " iteration " + (i + 1)); | ||
test.evaluate(); | ||
} | ||
} | ||
}; | ||
} else { | ||
return test; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.pushtorefresh.storio2.test; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
import static org.assertj.core.api.Java6Assertions.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class RepeatRuleTest { | ||
|
||
private Statement test = mock(Statement.class); | ||
private Description description = mock(Description.class); | ||
private Repeat repeat = mock(Repeat.class); | ||
|
||
@Test | ||
public void repeats10Times() throws Throwable { | ||
when(description.getAnnotation(Repeat.class)).thenReturn(repeat); | ||
when(repeat.times()).thenReturn(10); | ||
|
||
new RepeatRule().apply(test, description).evaluate(); | ||
|
||
verify(test, times(10)).evaluate(); | ||
} | ||
|
||
@Test | ||
public void noAnnotation() throws Throwable { | ||
when(description.getAnnotation(Repeat.class)).thenReturn(null); | ||
|
||
new RepeatRule().apply(test, description).evaluate(); | ||
|
||
verify(test).evaluate(); | ||
} | ||
|
||
@Test | ||
public void lessThan1Time() throws Throwable { | ||
when(description.getAnnotation(Repeat.class)).thenReturn(repeat); | ||
when(repeat.times()).thenReturn(0); | ||
|
||
try { | ||
new RepeatRule().apply(test, description).evaluate(); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
assertThat(expected).hasMessage("Repeat times should be >= 1, times = 0"); | ||
} | ||
} | ||
|
||
@Test | ||
public void lazyWithoutAnnotation() throws Throwable { | ||
when(description.getAnnotation(Repeat.class)).thenReturn(null); | ||
|
||
new RepeatRule().apply(test, description); | ||
|
||
verify(test, never()).evaluate(); | ||
} | ||
|
||
@Test | ||
public void lazyWithAnnotation() throws Throwable { | ||
when(description.getAnnotation(Repeat.class)).thenReturn(repeat); | ||
when(repeat.times()).thenReturn(10); | ||
|
||
new RepeatRule().apply(test, description); | ||
|
||
verify(test, never()).evaluate(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've seen this test failed on #827 PR build, but I don't think I ever saw it fail before, so I just added
@Repeat
to make sure it's good, I've read it carefully and still think it's a good test as is :)