/*
 * Copyright (c) 2008-2010, Hazel Ltd. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.hazelcast.client;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ClientRunnableTest {
    @Test
    public void testRun() throws Exception {
        final AtomicInteger counter = new AtomicInteger(0);
        final ClientRunnable clientRunnable = new ClientRunnable() {
            @Override
            protected void customRun() throws InterruptedException {
                counter.incrementAndGet();
            }
        };
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch waitLatch = new CountDownLatch(1);
        new Thread(new Runnable() {
            public void run() {
                try {
                    waitLatch.countDown();
                    Thread.sleep(100);
                    clientRunnable.running = false;
                    synchronized (clientRunnable.monitor) {
                        clientRunnable.monitor.wait();
                    }
                    latch.countDown();
                } catch (InterruptedException e) {
                }
            }
        }).start();
        clientRunnable.run();
        assertTrue(waitLatch.await(10, TimeUnit.SECONDS));
        assertTrue(counter.get() > 1);
        assertTrue("Not notified", latch.await(10, TimeUnit.SECONDS));
    }

    @Test
    public void testShutdown() throws Exception {
        final ClientRunnable clientRunnable = new ClientRunnable() {
            @Override
            protected void customRun() throws InterruptedException {
            }
        };
        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        final CountDownLatch latchShutDown = new CountDownLatch(1);
        new Thread(new Runnable() {
            public void run() {
                try {
                    latch1.await();
                    latch2.countDown();
                    clientRunnable.shutdown();
                    latchShutDown.countDown();
                } catch (InterruptedException e) {
                }
            }
        }).start();
        latch1.countDown();
        latch2.await();
        Thread.sleep(10);
        clientRunnable.notifyMonitor();
        assertTrue(latchShutDown.await(5, TimeUnit.SECONDS));
        assertFalse(clientRunnable.running);
    }
}

@@ -1001,51 +1001,55 @@
 nal 
-AtomicInteger counter = new AtomicInteger(0
+CountDownLatch waitLatch = new CountDownLatch(1
 );%0A 
@@ -1227,31 +1227,27 @@
     
-counter.incrementAndGet
+waitLatch.countDown
 ();%0A
@@ -1335,72 +1335,8 @@
 1);%0A
-        final CountDownLatch waitLatch = new CountDownLatch(1);%0A
     
@@ -1447,34 +1447,50 @@
   waitLatch.
-countDown(
+await(10, TimeUnit.SECONDS
 );%0A         
@@ -1520,63 +1520,12 @@
 (100
+0
 );%0A
-                    clientRunnable.running = false;%0A
     
@@ -1576,24 +1576,80 @@
 .monitor) %7B%0A
+                        clientRunnable.running = false;%0A
             
@@ -1847,32 +1847,32 @@
     %7D).start();%0A
+
         clientRu
@@ -1889,106 +1889,8 @@
 ();%0A
-        assertTrue(waitLatch.await(10, TimeUnit.SECONDS));%0A        assertTrue(counter.get() %3E 1);%0A
     

Test fix.