Пример #1
0
  @Test
  public void testBlockingChannelSendingThread() throws Exception {
    assumeThat(policy, is(OverflowPolicy.BLOCK));
    final Channel<Integer> ch = newChannel();

    Fiber<Integer> fib =
        new Fiber<Integer>(
                fjPool,
                new SuspendableCallable<Integer>() {
                  @Override
                  public Integer run() throws SuspendExecution, InterruptedException {
                    int i = 0;
                    while (ch.receive() != null) {
                      i++;
                      Fiber.sleep(50);
                    }
                    return i;
                  }
                })
            .start();

    for (int i = 0; i < 10; i++) ch.send(i);
    ch.close();

    assertThat(fib.get(), is(10));
  }
Пример #2
0
  @Ignore
  @Test
  public void whenReceiveNotCalledFromOwnerThenThrowException4() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();

    Thread thread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  ch.receive();
                } catch (InterruptedException ex) {
                  throw new AssertionError(ex);
                } catch (SuspendExecution e) {
                  throw new AssertionError(e);
                }
              }
            });
    thread.start();

    Thread.sleep(100);
    ch.send("a message");

    boolean thrown = false;
    try {
      ch.receive();
    } catch (Throwable e) {
      thrown = true;
    }
    assertTrue(thrown);

    thread.join();
  }
Пример #3
0
  @Ignore
  @Test
  public void whenReceiveNotCalledFromOwnerThenThrowException3() throws Exception {
    assumeTrue(Debug.isAssertionsEnabled());
    final Channel<String> ch = newChannel();

    Fiber fib =
        new Fiber(
                "fiber",
                fjPool,
                new SuspendableRunnable() {
                  @Override
                  public void run() throws SuspendExecution, InterruptedException {
                    Fiber.sleep(100);

                    ch.send("a message");

                    boolean thrown = false;
                    try {
                      ch.receive();
                    } catch (Throwable e) {
                      thrown = true;
                    }
                    assertTrue(thrown);
                  }
                })
            .start();

    String m = ch.receive();

    assertThat(m, equalTo("a message"));

    fib.join();
  }
Пример #4
0
  @Test
  public void sendMessageFromThreadToThread() throws Exception {
    final Channel<String> ch = newChannel();

    Thread thread =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  Thread.sleep(100);

                  ch.send("a message");
                } catch (InterruptedException | SuspendExecution ex) {
                  throw new AssertionError(ex);
                }
              }
            });
    thread.start();

    String m = ch.receive();

    assertThat(m, equalTo("a message"));

    thread.join();
  }
Пример #5
0
  @Test
  public void whenChannelOverflowsThrowException() throws Exception {
    assumeThat(policy, is(OverflowPolicy.THROW));
    assumeThat(mailboxSize, greaterThan(0));

    final Channel<Integer> ch = newChannel();

    int i = 0;
    try {
      for (i = 0; i < 10; i++) ch.send(i);
      fail();
    } catch (QueueCapacityExceededException e) {
      System.out.println("i = " + i);
    }
  }
Пример #6
0
  @Test
  public void testChannelGroupReceive() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();

    final ReceivePortGroup<String> group =
        new ReceivePortGroup<String>(channel1, channel2, channel3);

    Fiber fib =
        new Fiber(
                "fiber",
                fjPool,
                new SuspendableRunnable() {
                  @Override
                  public void run() throws SuspendExecution, InterruptedException {
                    String m1 = group.receive();
                    String m2 = channel2.receive();

                    assertThat(m1, equalTo("hello"));
                    assertThat(m2, equalTo("world!"));
                  }
                })
            .start();

    Thread.sleep(100);
    channel3.send("hello");
    Thread.sleep(100);
    if (policy != OverflowPolicy.BLOCK) {
      channel1.send("goodbye"); // TransferChannel will block here
      Thread.sleep(100);
    }
    channel2.send("world!");
    fib.join();
  }
Пример #7
0
  @Test
  public void whenChannelClosedThenBlockedSendsComplete() throws Exception {
    assumeThat(policy, is(OverflowPolicy.BLOCK));
    final Channel<Integer> ch = newChannel();

    final SuspendableRunnable r =
        new SuspendableRunnable() {
          @Override
          public void run() throws SuspendExecution, InterruptedException {
            for (int i = 1; i <= 100; i++) {
              ch.send(i);
            }
          }
        };
    Fiber fib1 = new Fiber("fiber", fjPool, r).start();
    Fiber fib2 = new Fiber("fiber", fjPool, r).start();

    Thread.sleep(500);

    ch.close();
    fib1.join();
    fib2.join();
  }
Пример #8
0
  @Test
  public void sendMessageFromThreadToFiber() throws Exception {
    final Channel<String> ch = newChannel();

    Fiber fib =
        new Fiber(
                "fiber",
                fjPool,
                new SuspendableRunnable() {
                  @Override
                  public void run() throws SuspendExecution, InterruptedException {
                    String m = ch.receive();

                    assertThat(m, equalTo("a message"));
                  }
                })
            .start();

    Thread.sleep(50);
    ch.send("a message");

    fib.join();
  }
Пример #9
0
  @Test
  public void testChannelCloseWithSleep() throws Exception {
    final Channel<Integer> ch = newChannel();

    Fiber fib =
        new Fiber(
                "fiber",
                fjPool,
                new SuspendableRunnable() {
                  @Override
                  public void run() throws SuspendExecution, InterruptedException {
                    for (int i = 1; i <= 5; i++) {
                      Integer m = ch.receive();

                      assertThat(m, equalTo(i));
                    }

                    Integer m = ch.receive();

                    assertThat(m, nullValue());
                    assertTrue(ch.isClosed());
                  }
                })
            .start();

    Thread.sleep(50);
    ch.send(1);
    ch.send(2);
    ch.send(3);
    ch.send(4);
    ch.send(5);

    Thread.sleep(50);
    ch.close();

    ch.send(6);
    ch.send(7);

    fib.join();
  }
Пример #10
0
  @Test
  public void testChannelGroupReceiveWithTimeout() throws Exception {
    final Channel<String> channel1 = newChannel();
    final Channel<String> channel2 = newChannel();
    final Channel<String> channel3 = newChannel();

    final ReceivePortGroup<String> group =
        new ReceivePortGroup<String>(channel1, channel2, channel3);

    Fiber fib =
        new Fiber(
                "fiber",
                fjPool,
                new SuspendableRunnable() {
                  @Override
                  public void run() throws SuspendExecution, InterruptedException {
                    String m1 = group.receive();
                    String m2 = channel2.receive();
                    String m3 = group.receive(10, TimeUnit.MILLISECONDS);
                    String m4 = group.receive(200, TimeUnit.MILLISECONDS);

                    assertThat(m1, equalTo("hello"));
                    assertThat(m2, equalTo("world!"));
                    assertThat(m3, nullValue());
                    assertThat(m4, equalTo("foo"));
                  }
                })
            .start();

    Thread.sleep(100);
    channel3.send("hello");
    Thread.sleep(100);
    channel2.send("world!");
    Thread.sleep(100);
    channel1.send("foo");
    fib.join();
  }