Skip to content

Commit

Permalink
Qualities to test cancellation, closes #132 (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmfs authored Dec 25, 2023
1 parent c5d0998 commit 7853662
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

package org.saynotobugs.confidence.rxjava3;

import org.reactivestreams.Subscriber;

import io.reactivex.rxjava3.core.CompletableObserver;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.SingleObserver;
import org.reactivestreams.Subscriber;


/**
Expand All @@ -39,4 +38,6 @@ public interface RxSubjectAdapter<T>
void onComplete();

void onError(Throwable error);

boolean hasSubscribers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

package org.saynotobugs.confidence.rxjava3;

import java.util.Collection;

import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.subscribers.TestSubscriber;

import java.util.Collection;


/**
* A generalization of {@link TestObserver} and {@link TestSubscriber}. That provides some means to "acknowledge" already seen emissions.
Expand All @@ -48,4 +48,6 @@ public interface RxTestAdapter<T>
Iterable<Throwable> errors();

boolean isCancelled();

void cancel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;

import io.reactivex.rxjava3.subjects.CompletableSubject;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


/**
Expand Down Expand Up @@ -58,4 +57,10 @@ public void onError(Throwable error)
{
mDelegate.onError(error);
}

@Override
public boolean hasSubscribers()
{
return mDelegate.hasObservers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;

import io.reactivex.rxjava3.subjects.MaybeSubject;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


public final class MaybeSubjectAdapter<T> implements RxSubjectAdapter<T>
Expand Down Expand Up @@ -53,4 +52,10 @@ public void onError(Throwable error)
{
mDelegate.onError(error);
}

@Override
public boolean hasSubscribers()
{
return mDelegate.hasObservers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;

import io.reactivex.rxjava3.processors.PublishProcessor;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


/**
Expand Down Expand Up @@ -56,4 +55,10 @@ public void onError(Throwable error)
{
mDelegate.onError(error);
}

@Override
public boolean hasSubscribers()
{
return mDelegate.hasSubscribers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;

import io.reactivex.rxjava3.subjects.PublishSubject;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


public final class PublishSubjectAdapter<T> implements RxSubjectAdapter<T>
Expand Down Expand Up @@ -53,4 +52,10 @@ public void onError(Throwable error)
{
mDelegate.onError(error);
}

@Override
public boolean hasSubscribers()
{
return mDelegate.hasObservers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import io.reactivex.rxjava3.observers.TestObserver;
import org.saynotobugs.confidence.rxjava3.RxTestAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import io.reactivex.rxjava3.observers.TestObserver;


public final class RxTestObserver<T> extends TestObserver<T> implements RxTestAdapter<T>
{
Expand Down Expand Up @@ -76,4 +75,10 @@ public boolean isCancelled()
{
return super.isDisposed();
}

@Override
public void cancel()
{
dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import io.reactivex.rxjava3.subscribers.TestSubscriber;
import org.saynotobugs.confidence.rxjava3.RxTestAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import io.reactivex.rxjava3.subscribers.TestSubscriber;


public final class RxTestSubscriber<T> extends TestSubscriber<T> implements RxTestAdapter<T>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

package org.saynotobugs.confidence.rxjava3.adapters;

import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;

import io.reactivex.rxjava3.subjects.SingleSubject;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


/**
Expand Down Expand Up @@ -52,10 +51,15 @@ public void onComplete()
throw new UnsupportedOperationException("SingleSubjectAdapter.onComplete() called, but Singles require a value");
}


@Override
public void onError(Throwable error)
{
mDelegate.onError(error);
}

@Override
public boolean hasSubscribers()
{
return mDelegate.hasObservers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.rxjava3.procedure;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.QualityComposition;
import org.saynotobugs.confidence.quality.object.Satisfies;
import org.saynotobugs.confidence.rxjava3.RxSubjectAdapter;


@StaticFactories(value = "RxJava3", packageName = "org.saynotobugs.confidence.rxjava3")
public final class HasSubscribers extends QualityComposition<RxSubjectAdapter<?>>
{
public HasSubscribers()
{
super(new Satisfies<>(RxSubjectAdapter::hasSubscribers, __ -> new Text("had no subscribers"), new Text("has subscribers")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2022 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.rxjava3.rxexpectation;

import org.dmfs.srcless.annotations.staticfactory.StaticFactories;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.object.Successfully;
import org.saynotobugs.confidence.rxjava3.RxExpectation;
import org.saynotobugs.confidence.rxjava3.RxExpectationComposition;
import org.saynotobugs.confidence.rxjava3.RxTestAdapter;


@StaticFactories(value = "RxJava3", packageName = "org.saynotobugs.confidence.rxjava3")
public final class Disposal<T> extends RxExpectationComposition<T>
{
/**
* Creates an {@link RxExpectation} that disposes the subscription.
*/
public Disposal()
{
super(testScheduFler -> new Successfully<>(new Text("disposal"), new Text("disposal"), RxTestAdapter::cancel));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.saynotobugs.confidence.rxjava3;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.schedulers.TestScheduler;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.schedulers.TestScheduler;

import static java.time.Duration.ofSeconds;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.saynotobugs.confidence.Assertion.assertThat;
import static org.saynotobugs.confidence.quality.Core.*;
import static org.saynotobugs.confidence.rxjava3.RxJava3.when;
import static org.saynotobugs.confidence.rxjava3.RxJava3.*;


Expand Down Expand Up @@ -191,4 +191,17 @@ void testCompletableTransformer()
downstream(immediately(errors(IOException.class))))));
}


@Test
void testCancelledFlowableTransformer()
{
assertThat((Flowable<Integer> upstream) -> upstream,
unscheduled(transformsFlowable(
upstream(emit(1, 2, 3)),
downstream(emits(1, 2, 3)),
downstream(disposal()),
upstream(not(hasSubscribers()))
)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 dmfs GmbH
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.saynotobugs.confidence.rxjava3.adapters;

import io.reactivex.rxjava3.subjects.PublishSubject;
import org.junit.jupiter.api.Test;
import org.saynotobugs.confidence.description.Text;
import org.saynotobugs.confidence.quality.composite.AllOf;
import org.saynotobugs.confidence.quality.composite.Not;
import org.saynotobugs.confidence.quality.function.MutatesArgument;
import org.saynotobugs.confidence.quality.grammar.SoIt;
import org.saynotobugs.confidence.quality.grammar.When;
import org.saynotobugs.confidence.quality.object.Satisfies;
import org.saynotobugs.confidence.quality.object.Successfully;
import org.saynotobugs.confidence.quality.trivial.Anything;

import static org.saynotobugs.confidence.Assertion.assertThat;

class RxTestObserverTest
{
@Test
void testDispose()
{
assertThat((PublishSubject<Object> observable) -> {
RxTestObserver<Object> testObserver = new RxTestObserver<>();
observable.subscribeWith(testObserver);
return testObserver;
},
new AllOf<>(
// first assert the function above actually subscribes
new MutatesArgument<>(
PublishSubject::create,
new SoIt<>(new Satisfies<>(PublishSubject::hasObservers, new Text("hasObserver"))),
new When<>(new Anything())),
new MutatesArgument<>(
PublishSubject::create,
new SoIt<>(new Not<>(new Satisfies<>(PublishSubject::hasObservers, new Text("hasObserver")))),
new When<>(new Successfully<>(new Text("Cancelled"), new Text("Cancelled"), (RxTestObserver<Object> o) -> o.cancel()))
))
);
}

}
Loading

0 comments on commit 7853662

Please sign in to comment.