Package com.hazelcast.core

Examples of com.hazelcast.core.ISemaphore


    @Test(timeout = 120000)
    public void testSemaphoreWithFailures() throws InterruptedException {
        final int k = 4;
        final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k + 1);
        final HazelcastInstance[] instances = factory.newInstances();
        final ISemaphore semaphore = instances[k].getSemaphore("test");
        int initialPermits = 20;

        semaphore.init(initialPermits);

        for (int i = 0; i < k; i++) {
            int rand = (int) (Math.random() * 5) + 1;

            semaphore.acquire(rand);
            initialPermits -= rand;
            assertEquals(initialPermits, semaphore.availablePermits());
            semaphore.release(rand);
            initialPermits += rand;
            assertEquals(initialPermits, semaphore.availablePermits());

            instances[i].shutdown();

            semaphore.acquire(rand);
            initialPermits -= rand;
            assertEquals(initialPermits, semaphore.availablePermits());
            semaphore.release(rand);
            initialPermits += rand;
            assertEquals(initialPermits, semaphore.availablePermits());
        }
    }
View Full Code Here


    @Test(timeout = 120000)
    public void testSemaphoreWithFailuresAndJoin() {
        final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
        final HazelcastInstance instance1 = factory.newHazelcastInstance();
        final HazelcastInstance instance2 = factory.newHazelcastInstance();
        final ISemaphore semaphore = instance1.getSemaphore("test");
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        assertTrue(semaphore.init(0));

        final Thread thread = new Thread() {
            public void run() {
                for (int i = 0; i < 2; i++) {
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                countDownLatch.countDown();
            }
        };
        thread.start();

        instance2.shutdown();
        semaphore.release();

        HazelcastInstance instance3 = factory.newHazelcastInstance();
        ISemaphore semaphore1 = instance3.getSemaphore("test");
        semaphore1.release();

        try {
            assertTrue(countDownLatch.await(15, TimeUnit.SECONDS));
        } catch (InterruptedException e) {
            e.printStackTrace();
View Full Code Here

        final Counter counter = new Counter();

        assertTrue(instances[0].getSemaphore("test").init(1));

        for (int i = 0; i < threadCount; i++) {
            final ISemaphore semaphore = instances[i].getSemaphore("test");
            new Thread() {
                public void run() {
                    for (int j = 0; j < loopCount; j++) {
                        try {
                            semaphore.acquire();
                            sleepMillis((int) (Math.random() * 3));
                            counter.inc();
                        } catch (InterruptedException e) {
                            return;
                        } finally {
                            semaphore.release();
                        }
                    }
                    latch.countDown();
                }
            }.start();
View Full Code Here

        return new Config();
    }

    @Before
    public void start() throws IOException {
        ISemaphore s = getInstance().getSemaphore(name);
        s.reducePermits(100);
        assertEquals(0, s.availablePermits());
    }
View Full Code Here

        assertEquals(0, s.availablePermits());
    }

    @After
    public void clear() throws IOException {
        ISemaphore s = getInstance().getSemaphore(name);
        s.reducePermits(100);
        assertEquals(0, s.availablePermits());
    }
View Full Code Here

        assertEquals(0, s.availablePermits());
    }

    @Test
    public void testAcquire() throws Exception {
        ISemaphore s = getInstance().getSemaphore(name);
        assertTrue(s.init(10));

        getClient().send(new AcquireRequest(name, 3, 0));
        boolean result = (Boolean) getClient().receive();
        assertTrue(result);
        assertEquals(7, s.availablePermits());

        getClient().send(new AcquireRequest(name, 8, 6 * 1000));
        assertEquals(7, s.availablePermits());

        Thread.sleep(2 * 1000);

        s.release(1);

        result = (Boolean) getClient().receive();
        assertTrue(result);
        assertEquals(0, s.availablePermits());

        getClient().send(new AcquireRequest(name, 4, 2 * 1000));
        result = (Boolean) getClient().receive();
        assertFalse(result);

View Full Code Here

    public void testAvailable() throws Exception {
        getClient().send(new AvailableRequest(name));
        int result = (Integer) getClient().receive();
        assertEquals(0, result);

        ISemaphore s = getInstance().getSemaphore(name);
        s.release(5);

        getClient().send(new AvailableRequest(name));
        result = (Integer) getClient().receive();
        assertEquals(5, result);
    }
View Full Code Here

        assertEquals(5, result);
    }

    @Test
    public void testDrain() throws Exception {
        ISemaphore s = getInstance().getSemaphore(name);
        assertTrue(s.init(10));

        getClient().send(new DrainRequest(name));
        int result = (Integer) getClient().receive();
        assertEquals(10, result);

        s.release(4);

        getClient().send(new DrainRequest(name));
        result = (Integer) getClient().receive();
        assertEquals(4, result);
    }
View Full Code Here

        assertEquals(4, result);
    }

    @Test
    public void testInit() throws Exception {
        ISemaphore s = getInstance().getSemaphore(name);

        getClient().send(new InitRequest(name, 10));
        boolean result = (Boolean) getClient().receive();
        assertTrue(result);
        assertEquals(10, s.availablePermits());

        getClient().send(new InitRequest(name, 20));
        result = (Boolean) getClient().receive();
        assertFalse(result);
        assertEquals(10, s.availablePermits());
    }
View Full Code Here

        assertEquals(10, s.availablePermits());
    }

    @Test
    public void testReduce() throws Exception {
        ISemaphore s = getInstance().getSemaphore(name);
        assertTrue(s.init(10));

        getClient().send(new ReduceRequest(name, 4));
        boolean result = (Boolean) getClient().receive();
        assertTrue(result);
        assertEquals(6, s.availablePermits());
    }
View Full Code Here

TOP

Related Classes of com.hazelcast.core.ISemaphore

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.