<commit_before>/*
 *
 * (c) 2003-2009 ThoughtWorks Ltd
 *
 * See license.txt for license details
 */
package com.thoughtworks.proxy.toys.future;

import com.thoughtworks.proxy.AbstractProxyTest;
import static com.thoughtworks.proxy.toys.future.Future.typedFuture;
import static com.thoughtworks.proxy.toys.future.Future.future;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import org.junit.Test;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author <NAME>&oslash;y
 * @version $Revision: 1.3 $
 */
public class FutureTest extends AbstractProxyTest {
    public static interface Service {
        List getList();
        void methodReturnsVoid();
    }

    public static class SlowService implements Service {
        private final CountDownLatch latch;

        public SlowService(CountDownLatch latch) {
            this.latch = latch;
        }

        public List getList() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage());
            }
            return Collections.singletonList("yo");
        }

        public void methodReturnsVoid() {
        }
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        assertEquals(0, stuff.size());
        Thread.sleep(100);
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = typedFuture(Service.class).with(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        assertEquals(0, stuff.size());
        Thread.sleep(100);
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldHandleVoidMethodsWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }

    @Test
    public void testShouldHandleVoidMethodsWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService =  typedFuture(Service.class).with(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }
}<commit_msg>Fix FutureToy tests.<commit_after>/*
 *
 * (c) 2003-2009 ThoughtWorks Ltd
 *
 * See license.txt for license details
 */
package com.thoughtworks.proxy.toys.future;

import static com.thoughtworks.proxy.toys.future.Future.future;
import static com.thoughtworks.proxy.toys.future.Future.typedFuture;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import com.thoughtworks.proxy.AbstractProxyTest;

import org.junit.Test;

/**
 * @author <NAME>&oslash;y
 * @version $Revision: 1.3 $
 */
public class FutureTest extends AbstractProxyTest {
    public static interface Service {
        List getList();
        void methodReturnsVoid();
    }

    public static class SlowService implements Service {
        private final CountDownLatch latch;

        public SlowService(CountDownLatch latch) {
            this.latch = latch;
        }

        public List getList() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e.getMessage());
            }
            return Collections.singletonList("yo");
        }

        public void methodReturnsVoid() {
        }
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        Thread.sleep(100);
        assertEquals(1, stuff.size());
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldReturnNullObjectAsIntermediateResultAndSwapWhenMethodCompletesWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = typedFuture(Service.class).with(slowService).build(getFactory());

        List stuff = fastService.getList();
        assertTrue(stuff.isEmpty());
        // let slowService.getStuff() proceed!
        latch.countDown();
        Thread.sleep(100);
        assertEquals(1, stuff.size());
        assertEquals("yo", stuff.get(0));
    }

    @Test
    public void testShouldHandleVoidMethodsWithCast() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService = (Service) future(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }

    @Test
    public void testShouldHandleVoidMethodsWithGenerics() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(1);
        Service slowService = new SlowService(latch);
        Service fastService =  typedFuture(Service.class).with(slowService).build(getFactory());
        fastService.methodReturnsVoid();
    }
}
{'fraction_non_alphanumeric': 0.09592362479163509, 'fraction_numerical': 0.007273829368086074, 'mean_word_length': 2.767123287671233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406844', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1701, 'n_words': 364}