示例#1
0
  @Test
  public void testFlatMapMaxConcurrent() {
    final int m = 4;
    final AtomicInteger subscriptionCount = new AtomicInteger();
    Observable<Integer> source =
        Observable.range(1, 10)
            .flatMap(
                new Func1<Integer, Observable<Integer>>() {
                  @Override
                  public Observable<Integer> call(Integer t1) {
                    return compose(Observable.range(t1 * 10, 2), subscriptionCount, m)
                        .subscribeOn(Schedulers.computation());
                  }
                },
                m);

    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();

    source.subscribe(ts);

    ts.awaitTerminalEvent();
    ts.assertNoErrors();
    Set<Integer> expected =
        new HashSet<Integer>(
            Arrays.asList(
                10, 11, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61, 70, 71, 80, 81, 90, 91, 100, 101));
    Assert.assertEquals(expected.size(), ts.getOnNextEvents().size());
    Assert.assertTrue(expected.containsAll(ts.getOnNextEvents()));
  }
 @Test
 public void studentInformationIsPrintedOutCorrectly() {
   GsonReader mockGson = mock(GsonReader.class);
   String studentNr = "13865669";
   Palautukset palautukset = new Palautukset();
   ArrayList<Palautus> palautusLista = new ArrayList<Palautus>();
   Palautus p = new Palautus();
   p.setEtunimi("Tony");
   p.setSukunimi("Kovanen");
   p.setGithubtunnus("rase-");
   p.setOpiskelijanumero("13865669");
   p.setTehtavat("1,2,3,4,5");
   p.setTehtavia(5);
   p.setTunteja(2);
   p.setViikko(1);
   palautusLista.add(p);
   Palautus b = new Palautus();
   b.setEtunimi("Bottleneck");
   b.setSukunimi("");
   b.setGithubtunnus("");
   b.setOpiskelijanumero("13865669");
   b.setTehtavat("Bottleneck");
   b.setTehtavia(0);
   b.setTunteja(0);
   b.setViikko(0);
   palautusLista.add(b);
   palautukset.setPalautukset(palautusLista);
   when(mockGson.read(studentNr)).thenReturn(palautukset);
   StudentInformation info = new StudentInformation(studentNr, mockGson);
   System.out.println(info);
   Assert.assertEquals(
       "Tony Kovanen 13865669\n\nminiprojekti: Bottleneck\n\nviikko 1: 5 tehtävää 1,2,3,4,5 aikaa kului 2 tuntia.\n\nyhteensä: 5 tehtävää 2 tuntia",
       info.toString());
 }
 @Test
 public void testEncodedMemoryCacheGetSuccessful() {
   setupEncodedMemoryCacheGetSuccess();
   when(mProducerContext.getLowestPermittedRequestLevel())
       .thenReturn(ImageRequest.RequestLevel.ENCODED_MEMORY_CACHE);
   mEncodedMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
   ArgumentCaptor<EncodedImage> argumentCaptor = ArgumentCaptor.forClass(EncodedImage.class);
   verify(mConsumer).onNewResult(argumentCaptor.capture(), eq(true));
   EncodedImage encodedImage = argumentCaptor.getValue();
   Assert.assertSame(
       mFinalEncodedImage.getUnderlyingReferenceTestOnly(),
       encodedImage.getUnderlyingReferenceTestOnly());
   verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
   Map<String, String> extraMap = ImmutableMap.of(EncodedMemoryCacheProducer.VALUE_FOUND, "true");
   verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
   Assert.assertFalse(mFinalImageReference.isValid());
 }
 @Test
 public void returningNullvaluedStudentReturnsASpecialToString() {
   GsonReader mockGson = mock(GsonReader.class);
   String studentNr = "13865669";
   when(mockGson.read(studentNr)).thenReturn(new Palautukset());
   StudentInformation info = new StudentInformation(studentNr, mockGson);
   Assert.assertEquals("Käyttäjää ei löytynyt", info.toString());
 }
  @Test
  public void shouldReturnUnparsableClassIfErrorOccursWhileParsing() {
    ClassParser parser = mock(ClassParser.class);
    when(parser.getClass("MyClassName")).thenThrow(new RuntimeException(new NotFoundException("")));

    builder = new JavaClassBuilder(parser);

    Assert.assertThat(builder.createClass("MyClassName"), instanceOf(UnparsableClass.class));
  }
 @Test
 public void testEncodedMemoryCacheGetNotFoundNextProducerSuccess() {
   setupEncodedMemoryCacheGetNotFound();
   setupNextProducerStreamingSuccess();
   mEncodedMemoryCacheProducer.produceResults(mConsumer, mProducerContext);
   verify(mMemoryCache, never()).cache(mCacheKey, mIntermediateImageReference);
   ArgumentCaptor<CloseableReference> argumentCaptor =
       ArgumentCaptor.forClass(CloseableReference.class);
   verify(mMemoryCache).cache(eq(mCacheKey), argumentCaptor.capture());
   CloseableReference<PooledByteBuffer> capturedRef =
       (CloseableReference<PooledByteBuffer>) argumentCaptor.getValue();
   Assert.assertSame(
       mFinalImageReference.getUnderlyingReferenceTestOnly(),
       capturedRef.getUnderlyingReferenceTestOnly());
   verify(mConsumer).onNewResult(mIntermediateEncodedImage, false);
   verify(mConsumer).onNewResult(mFinalEncodedImage, true);
   Assert.assertTrue(EncodedImage.isValid(mFinalEncodedImageClone));
   verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
   Map<String, String> extraMap = ImmutableMap.of(EncodedMemoryCacheProducer.VALUE_FOUND, "false");
   verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
 }
 @Test
 public void testBitmapMemoryCacheGetSuccessful() {
   setupBitmapCacheGetSuccess();
   when(mProducerContext.getLowestPermittedRequestLevel())
       .thenReturn(ImageRequest.RequestLevel.BITMAP_MEMORY_CACHE);
   mBitmapMemoryCacheGetProducer.produceResults(mConsumer, mProducerContext);
   verify(mConsumer).onNewResult(mFinalImageReference, true);
   verify(mProducerListener).onProducerStart(mRequestId, PRODUCER_NAME);
   Map<String, String> extraMap =
       ImmutableMap.of(BitmapMemoryCacheProducer.EXTRA_CACHED_VALUE_FOUND, "true");
   verify(mProducerListener).onProducerFinishWithSuccess(mRequestId, PRODUCER_NAME, extraMap);
   Assert.assertTrue(!mFinalImageReference.isValid());
 }
示例#8
0
  @Ignore // don't care for any reordering
  @Test(timeout = 10000)
  public void flatMapRangeAsyncLoop() {
    for (int i = 0; i < 2000; i++) {
      if (i % 10 == 0) {
        System.out.println("flatMapRangeAsyncLoop > " + i);
      }
      TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
      Observable.range(0, 1000)
          .flatMap(
              new Func1<Integer, Observable<Integer>>() {
                @Override
                public Observable<Integer> call(Integer t) {
                  return Observable.just(t);
                }
              })
          .observeOn(Schedulers.computation())
          .subscribe(ts);

      ts.awaitTerminalEvent(2500, TimeUnit.MILLISECONDS);
      if (ts.getOnCompletedEvents().isEmpty()) {
        System.out.println(ts.getOnNextEvents().size());
      }
      ts.assertTerminalEvent();
      ts.assertNoErrors();
      List<Integer> list = ts.getOnNextEvents();
      assertEquals(1000, list.size());
      boolean f = false;
      for (int j = 0; j < list.size(); j++) {
        if (list.get(j) != j) {
          System.out.println(j + " " + list.get(j));
          f = true;
        }
      }
      if (f) {
        Assert.fail("Results are out of order!");
      }
    }
  }