Package io.netty.channel.local

Examples of io.netty.channel.local.LocalChannel$LocalUnsafe


        // start the eventloops
        loopA.execute(NOOP);
        loopB.execute(NOOP);

        LocalChannel channel = new LocalChannel();
        ChannelPromise registerPromise = channel.newPromise();
        channel.unsafe().register(loopA, registerPromise);
        registerPromise.sync();

        for (int i = 0; i < numtasks; i++) {
            Queue<Long> timestamps = new LinkedBlockingQueue<Long>();
            timestampsPerTask.add(timestamps);
            scheduledFutures.add(channel.eventLoop()
                    .scheduleAtFixedRate(new TimestampsRunnable(timestamps), 0, 100, TimeUnit.MILLISECONDS));
        }

        // Each task should be executed every 100ms.
        // Will give them additional 50ms to execute ten times.
        Thread.sleep(50 + 10 * 100);

        // Deregister must stop future execution of scheduled tasks.
        assertTrue(channel.deregister().sync().isSuccess());

        for (Queue<Long> timestamps : timestampsPerTask) {
            String message = String.format("size: %d, minSize: 10", timestamps.size());
            assertTrue(message, timestamps.size() >= 10);
            verifyTimestampDeltas(timestamps, TimeUnit.MICROSECONDS.toNanos(90));
            timestamps.clear();
        }

        // Because the Channel is deregistered no task must have executed since then.
        Thread.sleep(200);
        for (Queue<Long> timestamps : timestampsPerTask) {
            String message = String.format("size: %d, expected 0", timestamps.size());
            assertTrue(message, timestamps.isEmpty());
        }

        registerPromise = channel.newPromise();
        channel.unsafe().register(loopB, registerPromise);
        registerPromise.sync();

        // After the channel was registered with another eventloop the scheduled tasks should start executing again.
        // Same as above.
        Thread.sleep(50 + 10 * 100);
 
View Full Code Here


        // start the eventloops
        loopA.execute(NOOP);
        loopB.execute(NOOP);

        LocalChannel channel = new LocalChannel();
        boolean firstRun = true;
        ScheduledFuture<?> f = null;
        while (i.incrementAndGet() < 4) {
            ChannelPromise registerPromise = channel.newPromise();
            channel.unsafe().register(i.intValue() % 2 == 0 ? loopA : loopB, registerPromise);
            registerPromise.sync();

            if (firstRun) {
                f = channel.eventLoop().scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        assertTrue((i.intValue() % 2 == 0 ? loopA : loopB).inEventLoop());
                        timestamps.add(System.nanoTime());
                    }
                }, 0, 100, TimeUnit.MILLISECONDS);
                firstRun = false;
            }

            Thread.sleep(250);

            assertTrue(channel.deregister().sync().isSuccess());

            String message = String.format("size: %d, minSize: 2", timestamps.size());
            assertTrue(message, timestamps.size() >= 2);
            verifyTimestampDeltas(timestamps, TimeUnit.MILLISECONDS.toNanos(90));
            timestamps.clear();
        }

        // cancel while the channel is deregistered
        assertFalse(channel.isRegistered());
        assertTrue(f.cancel(true));
        String message = String.format("size: %d, expected 0", timestamps.size());
        assertTrue(message, timestamps.isEmpty());

        // register again and check that it's not executed again.
        ChannelPromise registerPromise = channel.newPromise();
        channel.unsafe().register(loopA, registerPromise);
        registerPromise.sync();

        Thread.sleep(200);

        message = String.format("size: %d, expected 0", timestamps.size());
View Full Code Here

        // start the eventloops
        loopA.execute(NOOP);
        loopB.execute(NOOP);

        LocalChannel channel = new LocalChannel();
        ChannelPromise registerPromise = channel.newPromise();
        channel.unsafe().register(loopA, registerPromise);
        registerPromise.sync();

        assertThat(channel.eventLoop(), instanceOf(PausableEventExecutor.class));
        assertSame(loopA, channel.eventLoop().unwrap());

        ScheduledFuture<?> scheduleFuture;
        if (PeriodicScheduleMethod.FIXED_RATE == method) {
            scheduleFuture = channel.eventLoop().scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    assertTrue(loopB.inEventLoop());
                    timestamps.add(System.nanoTime());
                }
            }, 100, 200, TimeUnit.MILLISECONDS);
        } else {
            scheduleFuture = channel.eventLoop().scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    assertTrue(loopB.inEventLoop());
                    timestamps.add(System.nanoTime());
                }
            }, 100, 200, TimeUnit.MILLISECONDS);
        }

        assertTrue(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());
        ChannelFuture deregisterFuture = channel.deregister();
        assertFalse(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());

        assertTrue(deregisterFuture.sync().isSuccess());

        timestamps.clear();
        Thread.sleep(1000);

        // no scheduled tasks must be executed after deregistration.
        String message = String.format("size: %d, expected 0", timestamps.size());
        assertTrue(message, timestamps.isEmpty());

        assertFalse(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());
        registerPromise = channel.newPromise();
        channel.unsafe().register(loopB,  registerPromise);
        assertTrue(registerPromise.sync().isSuccess());
        assertTrue(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());

        assertThat(channel.eventLoop(), instanceOf(PausableEventExecutor.class));
        assertSame(loopB, channel.eventLoop().unwrap());

        // 100ms internal delay + 1 second. Should be able to execute 5 tasks in that time.
        Thread.sleep(1150);
        assertTrue(scheduleFuture.cancel(true));
View Full Code Here

        // start the eventloops
        loopA.execute(NOOP);
        loopB.execute(NOOP);

        LocalChannel channel = new LocalChannel();
        ChannelPromise registerPromise = channel.newPromise();
        channel.unsafe().register(loopA, registerPromise);
        registerPromise.sync();

        assertThat(channel.eventLoop(), instanceOf(PausableEventExecutor.class));
        assertSame(loopA, ((PausableEventExecutor) channel.eventLoop()).unwrap());

        io.netty.util.concurrent.ScheduledFuture<?> scheduleFuture = channel.eventLoop().schedule(new Runnable() {
            @Override
            public void run() {
                oneTimeScheduledTaskExecuted.set(true);
                assertTrue(loopB.inEventLoop());
            }
        }, 1, TimeUnit.SECONDS);

        assertTrue(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());
        ChannelFuture deregisterFuture = channel.deregister();
        assertFalse(((PausableEventExecutor) channel.eventLoop()).isAcceptingNewTasks());

        assertTrue(deregisterFuture.sync().isSuccess());

        Thread.sleep(1000);

        registerPromise = channel.newPromise();
        assertFalse(oneTimeScheduledTaskExecuted.get());
        channel.unsafe().register(loopB, registerPromise);
        registerPromise.sync();

        assertThat(channel.eventLoop(), instanceOf(PausableEventExecutor.class));
        assertSame(loopB, ((PausableEventExecutor) channel.eventLoop()).unwrap());

        assertTrue(scheduleFuture.sync().isSuccess());
        assertTrue(oneTimeScheduledTaskExecuted.get());
    }
View Full Code Here

        }
    }

    @Test
    public void testRemoveChannelHandler() {
        ChannelPipeline pipeline = new LocalChannel().pipeline();

        ChannelHandler handler1 = newHandler();
        ChannelHandler handler2 = newHandler();
        ChannelHandler handler3 = newHandler();
View Full Code Here

        assertNull(pipeline.get("handler3"));
    }

    @Test
    public void testReplaceChannelHandler() {
        ChannelPipeline pipeline = new LocalChannel().pipeline();

        ChannelHandler handler1 = newHandler();
        pipeline.addLast("handler1", handler1);
        pipeline.addLast("handler2", handler1);
        pipeline.addLast("handler3", handler1);
View Full Code Here

        assertSame(pipeline.get("handler2"), newHandler2);
    }

    @Test
    public void testChannelHandlerContextNavigation() {
        ChannelPipeline pipeline = new LocalChannel().pipeline();

        final int HANDLER_ARRAY_LEN = 5;
        ChannelHandler[] firstHandlers = newHandlers(HANDLER_ARRAY_LEN);
        ChannelHandler[] lastHandlers = newHandlers(HANDLER_ARRAY_LEN);
View Full Code Here

    }

    @Test
    public void testFireChannelRegistered() throws Exception {
        final CountDownLatch latch = new CountDownLatch(1);
        ChannelPipeline pipeline = new LocalChannel().pipeline();
        pipeline.addLast(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ch.pipeline().addLast(new ChannelHandlerAdapter() {
                    @Override
View Full Code Here

        assertTrue(latch.await(2, TimeUnit.SECONDS));
    }

    @Test
    public void testPipelineOperation() {
        ChannelPipeline pipeline = new LocalChannel().pipeline();

        final int handlerNum = 5;
        ChannelHandler[] handlers1 = newHandlers(handlerNum);
        ChannelHandler[] handlers2 = newHandlers(handlerNum);
View Full Code Here

        verifyContextNumber(pipeline, handlerNum * 2);
    }

    @Test
    public void testChannelHandlerContextOrder() {
        ChannelPipeline pipeline = new LocalChannel().pipeline();

        pipeline.addFirst("1", newHandler());
        pipeline.addLast("10", newHandler());

        pipeline.addBefore("10", "5", newHandler());
View Full Code Here

TOP

Related Classes of io.netty.channel.local.LocalChannel$LocalUnsafe

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.