<commit_before>//Prevayler(TM) - The Free-Software Prevalence Layer.
//Copyright (C) 2001-2003 <NAME>
//This library 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.

package org.prevayler.implementation;

import java.io.IOException;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;

public class ReplicationTest extends PrevalenceTest {

	private Prevayler _server;
	private Prevayler _client;
	private Prevayler _clientWithServer;

	public void testServerFirst() throws Exception {
		serverCrashRecover(0);
		clientCrashRecover(0);
		serverAppend("a", "a");
		serverAppend("b", "ab");
		clientAppend("c", "abc");
		clientAppend("d", "abcd");
		serverAppend("e", "abcde");

		clientCrashRecover(0);
		clientAppend("f", "abcdef");
		serverAppend("g", "abcdefg");
	}

	public void testClientFirst() throws Exception {
		serverCrashRecover(1);
		clientCrashRecover(1);
		clientAppend("a", "a");
		clientAppend("b", "ab");
		serverAppend("c", "abc");
	}

/* TODO Test replication chaining.
	public void testChaining() throws Exception {
		serverCrashRecover(2);
		clientWithServerCrashRecover(2, 3);
		clientCrashRecover(3);
		clientAppend("a", "a");
		clientAppend("b", "ab");
		serverAppend("c", "abc");
	}
*/


	private void serverAppend(String appendix, String expectedResult) {
		append(_server, appendix, expectedResult);
	}

	private void clientAppend(String appendix, String expectedResult) {
		append(_client, appendix, expectedResult);
	}

	private void append(Prevayler prevayler, String appendix, String expectedResult) {
		prevayler.execute(new Appendix(appendix));
		try { Thread.sleep(10);	} catch (InterruptedException ignored) { }
		assertEquals(expectedResult, serverValue());
		assertEquals(expectedResult, clientValue());
	}

	private void serverCrashRecover(int portOffset) throws Exception {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + "\\server");
		factory.configureTransientMode(true);
		_server = factory.create();
	}
	
	private void clientCrashRecover(int portOffset) throws Exception {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationClient("localhost", PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + "\\client");
		_client = factory.create();
	}

	private void clientWithServerCrashRecover(int remoteServerPortOffset, int serverPortOffset) throws IOException, ClassNotFoundException {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationClient("localhost", PrevaylerFactory.DEFAULT_REPLICATION_PORT + remoteServerPortOffset);
		factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT + serverPortOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + "\\clientWithServer");
		_clientWithServer = factory.create();
	}

    protected void tearDown() throws Exception {
		_server = null;
		_client = null;
		_clientWithServer = null;
		super.tearDown();
    }

	private String serverValue() {
		return ((AppendingSystem)_server.prevalentSystem()).value();
	}

	private String clientValue() {
		try {
			Thread.sleep(100);  //The client is notified assynchronously.
		} catch (InterruptedException e) {
			e.printStackTrace();<commit_msg>removing non-unix-friendly backslashes<commit_after>//Prevayler(TM) - The Free-Software Prevalence Layer.
//Copyright (C) 2001-2003 <NAME>
//This library 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.

package org.prevayler.implementation;

import java.io.IOException;
import java.io.File;

import org.prevayler.Prevayler;
import org.prevayler.PrevaylerFactory;

public class ReplicationTest extends PrevalenceTest {

	private Prevayler _server;
	private Prevayler _client;
	private Prevayler _clientWithServer;

	public void testServerFirst() throws Exception {
		serverCrashRecover(0);
		clientCrashRecover(0);
		serverAppend("a", "a");
		serverAppend("b", "ab");
		clientAppend("c", "abc");
		clientAppend("d", "abcd");
		serverAppend("e", "abcde");

		clientCrashRecover(0);
		clientAppend("f", "abcdef");
		serverAppend("g", "abcdefg");
	}

	public void testClientFirst() throws Exception {
		serverCrashRecover(1);
		clientCrashRecover(1);
		clientAppend("a", "a");
		clientAppend("b", "ab");
		serverAppend("c", "abc");
	}

/* TODO Test replication chaining.
	public void testChaining() throws Exception {
		serverCrashRecover(2);
		clientWithServerCrashRecover(2, 3);
		clientCrashRecover(3);
		clientAppend("a", "a");
		clientAppend("b", "ab");
		serverAppend("c", "abc");
	}
*/


	private void serverAppend(String appendix, String expectedResult) {
		append(_server, appendix, expectedResult);
	}

	private void clientAppend(String appendix, String expectedResult) {
		append(_client, appendix, expectedResult);
	}

	private void append(Prevayler prevayler, String appendix, String expectedResult) {
		prevayler.execute(new Appendix(appendix));
		try { Thread.sleep(10);	} catch (InterruptedException ignored) { }
		assertEquals(expectedResult, serverValue());
		assertEquals(expectedResult, clientValue());
	}

	private void serverCrashRecover(int portOffset) throws Exception {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + "\\server");
		factory.configureTransientMode(true);
		_server = factory.create();
	}
	
	private void clientCrashRecover(int portOffset) throws Exception {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationClient("localhost", PrevaylerFactory.DEFAULT_REPLICATION_PORT + portOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + File.separator + "client");
		_client = factory.create();
	}

	private void clientWithServerCrashRecover(int remoteServerPortOffset, int serverPortOffset) throws IOException, ClassNotFoundException {
		PrevaylerFactory factory = new PrevaylerFactory();
		factory.configureReplicationClient("localhost", PrevaylerFactory.DEFAULT_REPLICATION_PORT + remoteServerPortOffset);
		factory.configureReplicationServer(PrevaylerFactory.DEFAULT_REPLICATION_PORT + serverPortOffset);
		factory.configurePrevalentSystem(new AppendingSystem());
		factory.configurePrevalenceBase(_testDirectory + File.separator + "clientWithServer");
		_clientWithServer = factory.create();
	}

    protected void tearDown() throws Exception {
		_server = null;
		_client = null;
		_clientWithServer = null;
		super.tearDown();
    }

	private String serverValue() {
		return ((AppendingSystem)_server.prevalentSystem()).value();
	}

	private String clientValue() {
		try {
			Thread.sleep(100);  //The client is notified assynchronously.
		} catch (InterruptedException e) {
			e.printStackTrace();
{'fraction_non_alphanumeric': 0.10972334139135106, 'fraction_numerical': 0.0059092130002686, 'mean_word_length': 6.223084384093114, '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': '5524851', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2114, 'n_words': 493}