<commit_before>/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * 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 net.openhft.chronicle.map;

import net.openhft.chronicle.hash.replication.LateUpdateException;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.map.jsr166.JSR166TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

/**
 * @author <NAME>.
 */
public class TimeBasedReplicationTest extends JSR166TestCase {

    public static final byte IDENTIFIER = 1;

    private static File getPersistenceFile() {
        String TMP = System.getProperty("java.io.tmpdir");
        File file = new File(TMP + "/chm-test" + System.nanoTime());
        file.deleteOnExit();
        return file;
    }

    Set<Thread> threads;

    @Before
    public void sampleThreads() {
        threads = Thread.getAllStackTraces().keySet();
    }

    @After
    public void checkThreadsShutdown() {
        StatelessClientTest.checkThreadsShutdown(threads);
    }

    @Test
    public void testIgnoreALatePut() throws IOException {

        final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
        try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
                .entries(10)
                .timeProvider(timeProvider)
                .replication((byte) 1)
                .create()) {
            current(timeProvider);

            // we do a put at the current time
            map.put("key-1", "value-1");
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");

            // now test assume that we receive a late update to the map, the following update should be ignored
            late(timeProvider);

            assertThrows(LateUpdateException.class, new Runnable() {
                @Override
                public void run() {
                    map.put("key-1", "value-2");
                }
            });

            // we'll now flip the time back to the current in order to do the read the result
            current(timeProvider);
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");
        }
    }

    @Test
    public void testIgnoreALatePutIfAbsent() throws IOException {

        final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
        try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
                .entries(10)
                .timeProvider(timeProvider)
                .replication((byte) 1)
                .create()) {
            current(timeProvider);

            // we do a put at the current time
            map.put("key-1", "value-1");
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");

            // now test assume that we receive a late update to the map, the following update
            // should be ignored
            late(timeProvider);

            // should NOT throw LateUpdateException because the value is present and it is just
            // returned.
            assertEquals(map.putIfAbsent("key-1", "value-2"), null);

            // we'll now flip the time back to the current in order to do the read the result
            current(timeProvider);
            assertEquals(1, map.size());<commit_msg>Fixed TimeBasedRepliationTest<commit_after>/*
 * Copyright 2014 Higher Frequency Trading
 *
 * http://www.higherfrequencytrading.com
 *
 * 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 net.openhft.chronicle.map;

import net.openhft.chronicle.hash.replication.LateUpdateException;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.map.jsr166.JSR166TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

/**
 * @author <NAME>.
 */
public class TimeBasedReplicationTest extends JSR166TestCase {

    public static final byte IDENTIFIER = 1;

    private static File getPersistenceFile() {
        String TMP = System.getProperty("java.io.tmpdir");
        File file = new File(TMP + "/chm-test" + System.nanoTime());
        file.deleteOnExit();
        return file;
    }

    Set<Thread> threads;

    @Before
    public void sampleThreads() {
        threads = Thread.getAllStackTraces().keySet();
    }

    @After
    public void checkThreadsShutdown() {
        StatelessClientTest.checkThreadsShutdown(threads);
    }

    @Test
    public void testIgnoreALatePut() throws IOException {

        final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
        try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
                .entries(10)
                .timeProvider(timeProvider)
                .replication((byte) 1)
                .create()) {
            current(timeProvider);

            // we do a put at the current time
            map.put("key-1", "value-1");
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");

            // now test assume that we receive a late update to the map, the following update should be ignored
            late(timeProvider);

            assertThrows(LateUpdateException.class, new Runnable() {
                @Override
                public void run() {
                    map.put("key-1", "value-2");
                }
            });

            // we'll now flip the time back to the current in order to do the read the result
            current(timeProvider);
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");
        }
    }

    @Test
    public void testIgnoreALatePutIfAbsent() throws IOException {

        final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
        try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
                .entries(10)
                .timeProvider(timeProvider)
                .replication((byte) 1)
                .create()) {
            current(timeProvider);

            // we do a put at the current time
            map.put("key-1", "value-1");
            assertEquals(map.size(), 1);
            assertEquals(map.get("key-1"), "value-1");

            // now test assume that we receive a late update to the map, the following update
            // should be ignored
            late(timeProvider);

            // should NOT throw LateUpdateException because the value is present and it is just
            // returned.
            assertEquals("value-1", map.putIfAbsent("key-1", "value-2"));

            // we'll now flip the time back to the current in order to do the read the result
            current(timeProvider);
            assertEquals(1, map.size());
{'fraction_non_alphanumeric': 0.10045050529648118, 'fraction_numerical': 0.010349446000243517, 'mean_word_length': 2.351285189718482, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3751791', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2122, 'n_words': 660}