<commit_before>/*
 * Copyright (C) 2016 <NAME>
 * <p/>
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.anthonycr.bonsai;

import android.os.Looper;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;

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

public class SchedulersUnitTest extends BaseUnitTest {

    @Test
    public void testIsFinalClass() throws Exception {
        Utils.testNonInstantiableClass(Schedulers.class);
    }

    @Test
    public void testMainScheduler_isCorrect() throws Exception {
        final AtomicReference<Boolean> mainScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Schedulers.main().execute(new Runnable() {
            @Override
            public void run() {
                mainScheduler.set(Looper.myLooper() == Looper.getMainLooper());
                latch.countDown();
            }
        });
        latch.await();
        assertTrue(mainScheduler.get());
    }

    @Test
    public void testImmediateScheduler_isSynchronous() throws Exception {
        final AtomicReference<Boolean> executedBoolean = new AtomicReference<>(false);
        final AtomicReference<Looper> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);

        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();

        Schedulers.immediate().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper());
                executedBoolean.set(true);
                latch.countDown();
            }
        });

        assertTrue(executedBoolean.get());

        latch.await();
        assertEquals(currentScheduler.get(), currentLooper);
    }

    @Test
    public void testIoScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.io().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testWorkerScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.worker().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testNewSingleThreadScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.newSingleThreadedScheduler().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testFromScheduler_isOnRightThread() throws Exception {
        final AtomicReference<Looper> executorAssertion = new AtomicReference<>();
        final AtomicReference<Looper> schedulerAssertion = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(2);
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                executorAssertion.set(Looper.myLooper());<commit_msg>Test the new from handler scheduler<commit_after>/*
 * Copyright (C) 2016 <NAME>
 * <p/>
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.anthonycr.bonsai;

import android.os.Handler;
import android.os.Looper;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;

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

public class SchedulersUnitTest extends BaseUnitTest {

    @Test
    public void testIsFinalClass() throws Exception {
        Utils.testNonInstantiableClass(Schedulers.class);
    }

    @Test
    public void testMainScheduler_isCorrect() throws Exception {
        final AtomicReference<Boolean> mainScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Schedulers.main().execute(new Runnable() {
            @Override
            public void run() {
                mainScheduler.set(Looper.myLooper() == Looper.getMainLooper());
                latch.countDown();
            }
        });
        latch.await();
        assertTrue(mainScheduler.get());
    }

    @Test
    public void testImmediateScheduler_isSynchronous() throws Exception {
        final AtomicReference<Boolean> executedBoolean = new AtomicReference<>(false);
        final AtomicReference<Looper> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);

        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();

        Schedulers.immediate().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper());
                executedBoolean.set(true);
                latch.countDown();
            }
        });

        assertTrue(executedBoolean.get());

        latch.await();
        assertEquals(currentScheduler.get(), currentLooper);
    }

    @Test
    public void testIoScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.io().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testWorkerScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.worker().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testNewSingleThreadScheduler_isOnDifferentThread() throws Exception {
        final AtomicReference<Boolean> currentScheduler = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(1);
        Utils.prepareLooper();
        final Looper currentLooper = Looper.myLooper();
        Schedulers.newSingleThreadedScheduler().execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                currentScheduler.set(Looper.myLooper() == currentLooper);
                latch.countDown();
            }
        });

        latch.await();
        assertFalse(currentScheduler.get());
    }

    @Test
    public void testFromHandler_isOnRightThread() throws Exception {
        final AtomicReference<String> executorThread = new AtomicReference<>(null);
        final AtomicReference<String> schedulerThread = new AtomicReference<>(null);
        final CountDownLatch latch = new CountDownLatch(1);
        Executors.newSingleThreadExecutor()
            .execute(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Handler handler = new Handler(Looper.myLooper());
                    executorThread.set(Thread.currentThread().toString());
                    Schedulers.from(handler)
                        .execute(new Runnable() {
                            @Override
                            public void run() {
                                schedulerThread.set(Thread.currentThread().toString());
                                latch.countDown();
                            }
                        });
                }
            });

        latch.await();

        assertNotNull(schedulerThread.get());
        assertNotNull(executorThread.get());
        assertEquals(schedulerThread.get(), executorThread.get());
    }

    @Test
    public void testFromScheduler_isOnRightThread() throws Exception {
        final AtomicReference<Looper> executorAssertion = new AtomicReference<>();
        final AtomicReference<Looper> schedulerAssertion = new AtomicReference<>();
        final CountDownLatch latch = new CountDownLatch(2);
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            public void run() {
                Utils.prepareLooper();
                executorAssertion.set(Looper.myLooper());
{'fraction_non_alphanumeric': 0.09471185330713817, 'fraction_numerical': 0.0023739358218729536, 'mean_word_length': 2.467783139369855, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7249910', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3031, 'n_words': 763}