PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-client/src/test/java/com/hazelcast/client/ClientRunnableTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 85 lines | 64 code | 6 blank | 15 comment | 0 complexity | 2b1483c47177911d4572d597807d0cae MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.client;
18
19import org.junit.Test;
20import org.junit.runner.RunWith;
21
22import java.util.concurrent.CountDownLatch;
23import java.util.concurrent.TimeUnit;
24
25import static org.junit.Assert.assertFalse;
26import static org.junit.Assert.assertTrue;
27
28@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
29public class ClientRunnableTest {
30    @Test
31    public void testRun() throws Exception {
32        final CountDownLatch waitLatch = new CountDownLatch(1);
33        final ClientRunnable clientRunnable = new ClientRunnable() {
34            @Override
35            protected void customRun() throws InterruptedException {
36                waitLatch.countDown();
37            }
38        };
39        final CountDownLatch latch = new CountDownLatch(1);
40        new Thread(new Runnable() {
41            public void run() {
42                try {
43                    waitLatch.await(10, TimeUnit.SECONDS);
44                    synchronized (clientRunnable.monitor) {
45                        clientRunnable.running = false;
46                        clientRunnable.monitor.wait();
47                    }
48                    latch.countDown();
49                } catch (InterruptedException e) {
50                }
51            }
52        }).start();
53        clientRunnable.run();
54        assertTrue("Not notified", latch.await(10, TimeUnit.SECONDS));
55    }
56
57    @Test
58    public void testShutdown() throws Exception {
59        final ClientRunnable clientRunnable = new ClientRunnable() {
60            @Override
61            protected void customRun() throws InterruptedException {
62            }
63        };
64        final CountDownLatch latch1 = new CountDownLatch(1);
65        final CountDownLatch latch2 = new CountDownLatch(1);
66        final CountDownLatch latchShutDown = new CountDownLatch(1);
67        new Thread(new Runnable() {
68            public void run() {
69                try {
70                    latch1.await();
71                    latch2.countDown();
72                    clientRunnable.shutdown();
73                    latchShutDown.countDown();
74                } catch (InterruptedException e) {
75                }
76            }
77        }).start();
78        latch1.countDown();
79        latch2.await();
80        Thread.sleep(10);
81        clientRunnable.terminate();
82        assertTrue(latchShutDown.await(5, TimeUnit.SECONDS));
83        assertFalse(clientRunnable.running);
84    }
85}