<commit_before>/**
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.impl.util;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Test;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertTrue;
import static org.neo4j.kernel.impl.util.JobScheduler.Group.indexPopulation;

public class Neo4jJobSchedulerTest
{
    private Neo4jJobScheduler scheduler;

    @After
    public void stopScheduler()
    {
        scheduler.stop();
    }

    @Test
    public void shouldRunRecurringJob() throws Exception
    {
        // Given
        scheduler = new Neo4jJobScheduler( StringLogger.DEV_NULL );
        final AtomicInteger invocations = new AtomicInteger( 0 );

        // When
        scheduler.start();
        scheduler.scheduleRecurring( indexPopulation, new Runnable()
        {
            public void run()
            {
                invocations.incrementAndGet();
            }
        }, 500, MILLISECONDS );
        sleep( 1500 );
        scheduler.stop();

        // Then
        int actualInvocations = invocations.get();
        assertTrue( actualInvocations >= 2); // <-- Dunno how to better assert that this works correctly :/
        assertTrue( actualInvocations < 6);

        sleep( 1000 );
        assertThat( invocations.get(), equalTo(actualInvocations) );
    }
<commit_msg>Unflaked test Neo4jJobSchedulerTest<commit_after>/**
 * Copyright (c) 2002-2014 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.impl.util;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.Test;

import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThan;

import static org.neo4j.helpers.Exceptions.launderedException;
import static org.neo4j.kernel.impl.util.JobScheduler.Group.indexPopulation;

public class Neo4jJobSchedulerTest
{
    private Neo4jJobScheduler scheduler;
    private AtomicInteger invocations = new AtomicInteger( 0 );

    private Runnable countInvocationsJob = new Runnable()
    {
        public void run()
        {
            try
            {
                invocations.incrementAndGet();
            }
            catch ( Throwable e )
            {
                e.printStackTrace();
                throw launderedException( e );
            }
        }
    };

    @After
    public void stopScheduler()
    {
        scheduler.stop();
    }

    @Test
    public void shouldRunRecurringJob() throws Exception
    {
        // Given
        long period = 10;
        scheduler = new Neo4jJobScheduler( StringLogger.DEV_NULL );

        // When
        scheduler.start();
        scheduler.scheduleRecurring( indexPopulation, countInvocationsJob, period, MILLISECONDS );
        awaitFirstInvocation();
        sleep( period*2 );
        scheduler.stop();

        // Then
        int actualInvocations = invocations.get();
        assertThat( actualInvocations, greaterThanOrEqualTo( 2 ) ); // <-- Dunno how to better assert that this works correctly :/
        assertThat( actualInvocations, lessThan( 10 ) );

        sleep( period );
        assertThat( invocations.get(), equalTo(actualInvocations) );
    }

    private void awaitFirstInvocation()
    {
        while ( invocations.get() == 0 )
        {   // Wait for the job to start running
            Thread.yield();
        }
    }
{'fraction_non_alphanumeric': 0.08625954198473282, 'fraction_numerical': 0.010687022900763359, 'mean_word_length': 2.7786589762076424, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, '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': '3320114', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1415, 'n_words': 462}